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>
75 explicit inline LockPtr(T_CppObject* cpp_object);
87 template <
class T_CastFrom>
109 template <
class T_CastFrom>
136 inline T_CppObject* operator->()
const;
142 inline T_CppObject* operator*()
const;
152 inline operator bool()
const;
167 template <
class T_CastFrom>
179 template <
class T_CastFrom>
191 template <
class T_CastFrom>
200 explicit inline LockPtr(T_CppObject *cpp_object,
Mutex *objmutex,
201 int *refcount,
Mutex *refmutex);
207 inline int refcount()
const {
return *__ref_count; }
225 void lock()
const { __obj_mutex->lock(); };
230 bool try_lock()
const {
return __obj_mutex->try_lock(); }
233 void unlock()
const { __obj_mutex->unlock(); }
243 T_CppObject *__cpp_object;
244 mutable Mutex *__obj_mutex;
245 mutable int *__ref_count;
246 mutable Mutex *__ref_mutex;
253 template <
class T_CppObject>
inline
259 template <
class T_CppObject>
inline
265 template <
class T_CppObject>
inline
275 template <
class T_CppObject>
inline
278 if(__ref_count && __ref_mutex)
284 if(*__ref_count == 0)
298 __ref_mutex->unlock();
304 template <
class T_CppObject>
inline
306 : __cpp_object(cpp_object),
313 __ref_count =
new int;
314 __ref_mutex =
new Mutex();
315 __obj_mutex =
new Mutex();
321 template <
class T_CppObject>
inline
323 int* refcount,
Mutex *refmutex)
324 : __cpp_object(cpp_object),
325 __obj_mutex(objmutex),
326 __ref_count(refcount),
327 __ref_mutex(refmutex)
329 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex) {
336 template <
class T_CppObject>
inline
339 __cpp_object(src.__cpp_object),
340 __obj_mutex(src.__obj_mutex),
341 __ref_count(src.__ref_count),
342 __ref_mutex(src.__ref_mutex)
344 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex)
355 template <
class T_CppObject>
356 template <
class T_CastFrom>
363 __cpp_object(src.operator->()),
364 __obj_mutex(src.objmutex_ptr()),
365 __ref_count(src.refcount_ptr()),
366 __ref_mutex(src.refmutex_ptr())
368 if(__cpp_object && __obj_mutex && __ref_count && __ref_mutex) {
375 template <
class T_CppObject>
inline
379 T_CppObject *
const temp = __cpp_object;
380 int *temp_count = __ref_count;
381 Mutex *temp_ref_mutex = __ref_mutex;
382 Mutex *temp_obj_mutex = __obj_mutex;
384 __cpp_object = other.__cpp_object;
385 __obj_mutex = other.__obj_mutex;
386 __ref_count = other.__ref_count;
387 __ref_mutex = other.__ref_mutex;
389 other.__cpp_object = temp;
390 other.__ref_count = temp_count;
391 other.__ref_mutex = temp_ref_mutex;
392 other.__obj_mutex = temp_obj_mutex;
395 template <
class T_CppObject>
inline
428 template <
class T_CppObject>
inline
438 template <
class T_CppObject>
439 template <
class T_CastFrom>
449 template <
class T_CppObject>
inline
453 return (__cpp_object == src.__cpp_object);
456 template <
class T_CppObject>
inline
460 return (__cpp_object != src.__cpp_object);
463 template <
class T_CppObject>
inline
466 return (__cpp_object != 0);
469 template <
class T_CppObject>
inline
476 template <
class T_CppObject>
477 template <
class T_CastFrom>
482 T_CppObject *
const cpp_object =
dynamic_cast<T_CppObject*
>(src.operator->());
490 template <
class T_CppObject>
491 template <
class T_CastFrom>
496 T_CppObject *
const cpp_object =
static_cast<T_CppObject*
>(src.operator->());
501 template <
class T_CppObject>
502 template <
class T_CastFrom>
507 T_CppObject *
const cpp_object =
const_cast<T_CppObject*
>(src.operator->());
518 template <
class T_CppObject>
inline
bool operator==(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> point to the same underlying instance.
bool operator!=(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> do not point to the same underlying instance.
void unlock()
Unlock the mutex.
Mutex * objmutex_ptr() const
Get object mutex.
void lock() const
Lock access to the encapsulated object.
static LockPtr< T_CppObject > cast_const(const LockPtr< T_CastFrom > &src)
Cast to non-const.
Mutex * refmutex_ptr() const
For use only in the internal implementation of sharedptr.
int refcount() const
Get current refcount.
LockPtr< T_CppObject > & operator=(const LockPtr< T_CppObject > &src)
Copy from another LockPtr.
LockPtr<> is a reference-counting shared lockable smartpointer.
static LockPtr< T_CppObject > cast_dynamic(const LockPtr< T_CastFrom > &src)
Dynamic cast to derived class.
static LockPtr< T_CppObject > cast_static(const LockPtr< T_CastFrom > &src)
Static cast to derived class.
bool try_lock() const
Try to acquire lock for the encapsulated object.
~LockPtr()
Destructor - decrements reference count.
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.
LockPtr()
Default constructor.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
void unlock() const
Unlock object mutex.
void swap(LockPtr< T_CppObject > &other)
Swap the contents of two LockPtr<>.
int * refcount_ptr() const
For use only in the internal implementation of sharedptr.