]> granicus.if.org Git - icinga2/blob - lib/base/object.h
287172c313272cd6205a860af76280a7cbf5423e
[icinga2] / lib / base / object.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef OBJECT_H
21 #define OBJECT_H
22
23 #include "base/i2-base.h"
24 #include <boost/thread/mutex.hpp>
25 #include <boost/smart_ptr/shared_ptr.hpp>
26 #include <boost/smart_ptr/weak_ptr.hpp>
27 #include <boost/smart_ptr/enable_shared_from_this.hpp>
28
29 using boost::shared_ptr;
30 using boost::weak_ptr;
31 using boost::dynamic_pointer_cast;
32 using boost::static_pointer_cast;
33
34 namespace icinga
35 {
36
37 class SharedPtrHolder;
38
39 /**
40  * Base class for all heap-allocated objects. At least one of its methods
41  * has to be virtual for RTTI to work.
42  *
43  * @ingroup base
44  */
45 class I2_BASE_API Object : public boost::enable_shared_from_this<Object>
46 {
47 public:
48         typedef shared_ptr<Object> Ptr;
49         typedef weak_ptr<Object> WeakPtr;
50
51         Object(void);
52         virtual ~Object(void);
53
54         /**
55          * Holds a shared pointer and provides support for implicit upcasts.
56          *
57          * @ingroup base
58          */
59         class SharedPtrHolder
60         {
61         public:
62                 /**
63                  * Constructor for the SharedPtrHolder class.
64                  *
65                  * @param object The shared pointer that should be used to
66                  *               construct this shared pointer holder.
67                  */
68                 explicit SharedPtrHolder(const Object::Ptr& object)
69                         : m_Object(object)
70                 { }
71
72                 /**
73                  * Retrieves a shared pointer for the object that is associated
74                  * this holder instance.
75                  *
76                  * @returns A shared pointer.
77                  */
78                 template<typename T>
79                 operator shared_ptr<T>(void) const
80                 {
81 #ifdef _DEBUG
82                         shared_ptr<T> other = dynamic_pointer_cast<T>(m_Object);
83                         ASSERT(other);
84 #else /* _DEBUG */
85                         shared_ptr<T> other = static_pointer_cast<T>(m_Object);
86 #endif /* _DEBUG */
87
88                         return other;
89                 }
90
91                 /**
92                  * Retrieves a weak pointer for the object that is associated
93                  * with this holder instance.
94                  *
95                  * @returns A weak pointer.
96                  */
97                 template<typename T>
98                 operator weak_ptr<T>(void) const
99                 {
100                         return static_cast<shared_ptr<T> >(*this);
101                 }
102
103         private:
104                 Object::Ptr m_Object; /**< The object that belongs to this
105                                            holder instance */
106         };
107
108 #ifdef _DEBUG
109         bool OwnsLock(void) const;
110 #endif /* _DEBUG */
111
112 protected:
113         SharedPtrHolder GetSelf(void);
114
115 private:
116         Object(const Object& other);
117         Object& operator=(const Object& rhs);
118
119 #ifndef _DEBUG
120         typedef boost::mutex MutexType;
121 #else /* _DEBUG */
122         typedef boost::recursive_mutex MutexType;
123
124         static boost::mutex m_DebugMutex;
125         mutable bool m_Locked;
126         mutable boost::thread::id m_LockOwner;
127 #endif /* _DEBUG */
128
129         mutable MutexType m_Mutex;
130
131         friend struct ObjectLock;
132 };
133
134 /**
135  * Compares a weak pointer with a raw pointer.
136  *
137  * @ingroup base
138  */
139 template<class T>
140 struct WeakPtrEqual
141 {
142 private:
143         const void *m_Ref; /**< The object. */
144
145 public:
146         /**
147          * Constructor for the WeakPtrEqual class.
148          *
149          * @param ref The object that should be compared with the weak pointer.
150          */
151         WeakPtrEqual(const void *ref) : m_Ref(ref) { }
152
153         /**
154          * Compares the two pointers.
155          *
156          * @param wref The weak pointer.
157          * @returns true if the pointers point to the same object, false otherwise.
158          */
159         bool operator()(const weak_ptr<T>& wref) const
160         {
161                 return (wref.lock().get() == static_cast<const T *>(m_Ref));
162         }
163 };
164
165 }
166
167 #endif /* OBJECT_H */