26 #ifndef __CORE_UTILS_LOCKPTR_H_ 27 #define __CORE_UTILS_LOCKPTR_H_ 29 #include <core/utils/refptr.h> 30 #include <core/threading/mutex.h> 56 template <
class T_CppObject>
79 explicit inline LockPtr(T_CppObject* cpp_object,
bool recursive_mutex =
false);
91 template <
class T_CastFrom>
113 template <
class T_CastFrom>
156 inline operator bool()
const;
171 template <
class T_CastFrom>
174 T_CppObject *
const cpp_object =
dynamic_cast<T_CppObject*
>(src.operator->());
191 template <
class T_CastFrom>
194 T_CppObject *
const cpp_object =
static_cast<T_CppObject*
>(src.operator->());
208 template <
class T_CastFrom>
211 T_CppObject *
const cpp_object =
const_cast<T_CppObject*
>(src.operator->());
222 explicit inline LockPtr(T_CppObject *cpp_object,
Mutex *objmutex,
229 inline int refcount()
const {
return *__ref_count; }
265 T_CppObject *__cpp_object;
266 mutable Mutex *__obj_mutex;
267 mutable int *__ref_count;
268 mutable Mutex *__ref_mutex;
275 template <
class T_CppObject>
inline 281 template <
class T_CppObject>
inline 287 template <
class T_CppObject>
inline 297 template <
class T_CppObject>
inline 300 if(__ref_count && __ref_mutex)
306 if(*__ref_count == 0)
320 __ref_mutex->unlock();
326 template <
class T_CppObject>
inline 328 : __cpp_object(cpp_object),
335 __ref_count =
new int;
336 __ref_mutex =
new Mutex(Mutex::RECURSIVE);
337 __obj_mutex =
new Mutex(recursive_mutex ? Mutex::RECURSIVE : Mutex::NORMAL);
343 template <
class T_CppObject>
inline 345 int* refcount,
Mutex *refmutex)
346 : __cpp_object(cpp_object),
347 __obj_mutex(objmutex),
348 __ref_count(refcount),
349 __ref_mutex(refmutex)
351 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex) {
354 __ref_mutex->unlock();
358 template <
class T_CppObject>
inline 361 __cpp_object(src.__cpp_object),
362 __obj_mutex(src.__obj_mutex),
363 __ref_count(src.__ref_count),
364 __ref_mutex(src.__ref_mutex)
366 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex)
370 __ref_mutex->unlock();
377 template <
class T_CppObject>
378 template <
class T_CastFrom>
385 __cpp_object(src.operator->()),
386 __obj_mutex(src.objmutex_ptr()),
387 __ref_count(src.refcount_ptr()),
388 __ref_mutex(src.refmutex_ptr())
390 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex) {
393 __ref_mutex->unlock();
397 template <
class T_CppObject>
inline 401 T_CppObject *
const temp = __cpp_object;
402 int *temp_count = __ref_count;
403 Mutex *temp_ref_mutex = __ref_mutex;
404 Mutex *temp_obj_mutex = __obj_mutex;
406 __cpp_object = other.__cpp_object;
407 __obj_mutex = other.__obj_mutex;
408 __ref_count = other.__ref_count;
409 __ref_mutex = other.__ref_mutex;
411 other.__cpp_object = temp;
412 other.__ref_count = temp_count;
413 other.__ref_mutex = temp_ref_mutex;
414 other.__obj_mutex = temp_obj_mutex;
417 template <
class T_CppObject>
inline 450 template <
class T_CppObject>
inline 460 template <
class T_CppObject>
461 template <
class T_CastFrom>
471 template <
class T_CppObject>
inline 475 return (__cpp_object == src.__cpp_object);
478 template <
class T_CppObject>
inline 482 return (__cpp_object != src.__cpp_object);
485 template <
class T_CppObject>
inline 488 return (__cpp_object != 0);
491 template <
class T_CppObject>
inline 504 template <
class T_CppObject>
inline bool operator!=(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> do not point to the same underlying instance.
int * refcount_ptr() const
For use only in the internal implementation of sharedptr.
Mutex * refmutex_ptr() const
For use only in the internal implementation of sharedptr.
Fawkes library namespace.
void unlock()
Unlock the mutex.
T_CppObject * operator*() const
Get underlying pointer.
void unlock() const
Unlock object mutex.
T_CppObject * operator->() const
Dereferencing.
LockPtr< T_CppObject > & operator=(const LockPtr< T_CppObject > &src)
Copy from another LockPtr.
LockPtr<> is a reference-counting shared lockable smartpointer.
bool try_lock() const
Try to acquire lock for the encapsulated object.
Mutex * objmutex_ptr() const
Get object mutex.
bool operator==(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> point to the same underlying instance.
~LockPtr()
Destructor - decrements reference count.
static LockPtr< T_CppObject > cast_dynamic(const LockPtr< T_CastFrom > &src)
Dynamic cast to derived class.
bool try_lock()
Tries to lock the mutex.
void clear()
Set underlying instance to 0, decrementing reference count of existing instance appropriately.
void swap(LockPtr< T_CppObject > &lrp, LockPtr< T_CppObject > &rrp)
Swap refptr instances.
static LockPtr< T_CppObject > cast_const(const LockPtr< T_CastFrom > &src)
Cast to non-const.
LockPtr()
Default constructor.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
void swap(LockPtr< T_CppObject > &other)
Swap the contents of two LockPtr<>.
static LockPtr< T_CppObject > cast_static(const LockPtr< T_CastFrom > &src)
Static cast to derived class.
void lock() const
Lock access to the encapsulated object.
int refcount() const
Get current refcount.