26 #ifndef _CORE_UTILS_LOCKPTR_H_ 27 #define _CORE_UTILS_LOCKPTR_H_ 29 #include <core/threading/mutex.h> 30 #include <core/utils/refptr.h> 53 template <
class T_CppObject>
76 explicit inline LockPtr(T_CppObject *cpp_object,
bool recursive_mutex =
false);
88 template <
class T_CastFrom>
110 template <
class T_CastFrom>
152 inline operator bool()
const;
166 template <
class T_CastFrom>
170 T_CppObject *
const cpp_object = dynamic_cast<T_CppObject *>(src.operator->());
188 template <
class T_CastFrom>
192 T_CppObject *
const cpp_object = static_cast<T_CppObject *>(src.operator->());
206 template <
class T_CastFrom>
210 T_CppObject *
const cpp_object = const_cast<T_CppObject *>(src.operator->());
290 T_CppObject * cpp_object_;
291 mutable Mutex *obj_mutex_;
292 mutable int * ref_count_;
293 mutable Mutex *ref_mutex_;
299 template <
class T_CppObject>
305 template <
class T_CppObject>
311 template <
class T_CppObject>
316 template <
class T_CppObject>
319 if (ref_count_ && ref_mutex_) {
324 if (*ref_count_ == 0) {
336 ref_mutex_->unlock();
341 template <
class T_CppObject>
343 : cpp_object_(cpp_object), obj_mutex_(0), ref_count_(0), ref_mutex_(0)
346 ref_count_ =
new int;
347 ref_mutex_ =
new Mutex(Mutex::RECURSIVE);
348 obj_mutex_ =
new Mutex(recursive_mutex ? Mutex::RECURSIVE : Mutex::NORMAL);
354 template <
class T_CppObject>
359 : cpp_object_(cpp_object), obj_mutex_(objmutex), ref_count_(refcount), ref_mutex_(refmutex)
361 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
364 ref_mutex_->unlock();
368 template <
class T_CppObject>
370 : cpp_object_(src.cpp_object_),
371 obj_mutex_(src.obj_mutex_),
372 ref_count_(src.ref_count_),
373 ref_mutex_(src.ref_mutex_)
375 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
378 ref_mutex_->unlock();
385 template <
class T_CppObject>
386 template <
class T_CastFrom>
391 cpp_object_(src.operator->()),
392 obj_mutex_(src.objmutex_ptr()),
393 ref_count_(src.refcount_ptr()),
394 ref_mutex_(src.refmutex_ptr())
396 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
399 ref_mutex_->unlock();
403 template <
class T_CppObject>
407 T_CppObject *
const temp = cpp_object_;
408 int * temp_count = ref_count_;
409 Mutex * temp_ref_mutex = ref_mutex_;
410 Mutex * temp_obj_mutex = obj_mutex_;
412 cpp_object_ = other.cpp_object_;
413 obj_mutex_ = other.obj_mutex_;
414 ref_count_ = other.ref_count_;
415 ref_mutex_ = other.ref_mutex_;
417 other.cpp_object_ = temp;
418 other.ref_count_ = temp_count;
419 other.ref_mutex_ = temp_ref_mutex;
420 other.obj_mutex_ = temp_obj_mutex;
423 template <
class T_CppObject>
456 template <
class T_CppObject>
465 template <
class T_CppObject>
466 template <
class T_CastFrom>
475 template <
class T_CppObject>
479 return (cpp_object_ == src.cpp_object_);
482 template <
class T_CppObject>
486 return (cpp_object_ != src.cpp_object_);
489 template <
class T_CppObject>
492 return (cpp_object_ != 0);
495 template <
class T_CppObject>
508 template <
class T_CppObject>
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.