1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
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>
29 using boost::shared_ptr;
30 using boost::weak_ptr;
31 using boost::dynamic_pointer_cast;
32 using boost::static_pointer_cast;
37 class SharedPtrHolder;
40 * Base class for all heap-allocated objects. At least one of its methods
41 * has to be virtual for RTTI to work.
45 class I2_BASE_API Object : public boost::enable_shared_from_this<Object>
48 typedef shared_ptr<Object> Ptr;
49 typedef weak_ptr<Object> WeakPtr;
52 virtual ~Object(void);
55 * Holds a shared pointer and provides support for implicit upcasts.
63 * Constructor for the SharedPtrHolder class.
65 * @param object The shared pointer that should be used to
66 * construct this shared pointer holder.
68 explicit SharedPtrHolder(const Object::Ptr& object)
73 * Retrieves a shared pointer for the object that is associated
74 * this holder instance.
76 * @returns A shared pointer.
79 operator shared_ptr<T>(void) const
82 shared_ptr<T> other = dynamic_pointer_cast<T>(m_Object);
85 shared_ptr<T> other = static_pointer_cast<T>(m_Object);
92 * Retrieves a weak pointer for the object that is associated
93 * with this holder instance.
95 * @returns A weak pointer.
98 operator weak_ptr<T>(void) const
100 return static_cast<shared_ptr<T> >(*this);
104 Object::Ptr m_Object; /**< The object that belongs to this
109 bool OwnsLock(void) const;
113 SharedPtrHolder GetSelf(void);
116 Object(const Object& other);
117 Object& operator=(const Object& rhs);
120 typedef boost::mutex MutexType;
122 typedef boost::recursive_mutex MutexType;
124 static boost::mutex m_DebugMutex;
125 mutable bool m_Locked;
126 mutable boost::thread::id m_LockOwner;
129 mutable MutexType m_Mutex;
131 friend struct ObjectLock;
135 * Compares a weak pointer with a raw pointer.
143 const void *m_Ref; /**< The object. */
147 * Constructor for the WeakPtrEqual class.
149 * @param ref The object that should be compared with the weak pointer.
151 WeakPtrEqual(const void *ref) : m_Ref(ref) { }
154 * Compares the two pointers.
156 * @param wref The weak pointer.
157 * @returns true if the pointers point to the same object, false otherwise.
159 bool operator()(const weak_ptr<T>& wref) const
161 return (wref.lock().get() == static_cast<const T *>(m_Ref));
167 #endif /* OBJECT_H */