Fawkes API
Fawkes Development Version
|
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>
306 template <
class T_CppObject>
313 template <
class T_CppObject>
318 template <
class T_CppObject>
321 if (ref_count_ && ref_mutex_) {
326 if (*ref_count_ == 0) {
338 ref_mutex_->unlock();
343 template <
class T_CppObject>
345 : cpp_object_(cpp_object), obj_mutex_(0), ref_count_(0), ref_mutex_(0)
348 ref_count_ =
new int;
356 template <
class T_CppObject>
361 : cpp_object_(cpp_object), obj_mutex_(objmutex), ref_count_(refcount), ref_mutex_(refmutex)
363 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
370 template <
class T_CppObject>
372 : cpp_object_(src.cpp_object_),
373 obj_mutex_(src.obj_mutex_),
374 ref_count_(src.ref_count_),
375 ref_mutex_(src.ref_mutex_)
377 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
387 template <
class T_CppObject>
388 template <
class T_CastFrom>
393 cpp_object_(src.operator->()),
394 obj_mutex_(src.objmutex_ptr()),
395 ref_count_(src.refcount_ptr()),
396 ref_mutex_(src.refmutex_ptr())
398 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
405 template <
class T_CppObject>
409 T_CppObject *
const temp = cpp_object_;
410 int * temp_count = ref_count_;
411 Mutex * temp_ref_mutex = ref_mutex_;
412 Mutex * temp_obj_mutex = obj_mutex_;
414 cpp_object_ = other.cpp_object_;
415 obj_mutex_ = other.obj_mutex_;
416 ref_count_ = other.ref_count_;
417 ref_mutex_ = other.ref_mutex_;
419 other.cpp_object_ = temp;
420 other.ref_count_ = temp_count;
421 other.ref_mutex_ = temp_ref_mutex;
422 other.obj_mutex_ = temp_obj_mutex;
425 template <
class T_CppObject>
458 template <
class T_CppObject>
467 template <
class T_CppObject>
468 template <
class T_CastFrom>
477 template <
class T_CppObject>
481 return (cpp_object_ == src.cpp_object_);
484 template <
class T_CppObject>
488 return (cpp_object_ != src.cpp_object_);
491 template <
class T_CppObject>
494 return (cpp_object_ != 0);
497 template <
class T_CppObject>
510 template <
class T_CppObject>
void lock()
Lock this mutex.
Mutex * refmutex_ptr() const
For use only in the internal implementation of sharedptr.
LockPtr< T_CppObject > & operator=(T_CppObject *ptr)
Assign object and claim ownership.
@ RECURSIVE
A thread attempting to relock this mutex without first unlocking it shall succeed in locking the mute...
T_CppObject * operator*() const
Get underlying pointer.
LockPtr<> is a reference-counting shared lockable smartpointer.
Mutex mutual exclusion lock.
void swap(LockPtr< T_CppObject > &other)
Swap the contents of two LockPtr<>.
bool try_lock() const
Try to acquire lock for the encapsulated object.
int refcount() const
Get current refcount.
LockPtr(T_CppObject *cpp_object, bool recursive_mutex=false)
Constructor that takes ownership.
Mutex * objmutex_ptr() const
Get object mutex.
void unlock()
Unlock the mutex.
int * refcount_ptr() const
For use only in the internal implementation of sharedptr.
T_CppObject * operator->() const
Dereferencing.
~LockPtr()
Destructor - decrements reference count.
void unlock() const
Unlock object mutex.
Fawkes library namespace.
void clear()
Set underlying instance to 0, decrementing reference count of existing instance appropriately.
LockPtr(const LockPtr< T_CppObject > &src)
Copy constructor This increments the shared reference count.
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.
@ NORMAL
This type of mutex does not detect deadlock.
LockPtr< T_CppObject > & operator=(const LockPtr< T_CppObject > &src)
Copy from another LockPtr.
LockPtr(const LockPtr< T_CastFrom > &src)
Copy constructor (from different, but castable type).
bool try_lock()
Tries to lock the mutex.
bool operator==(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> point to the same underlying instance.
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.
static LockPtr< T_CppObject > cast_dynamic(const LockPtr< T_CastFrom > &src)
Dynamic cast to derived class.
bool operator!=(const LockPtr< T_CppObject > &src) const
Tests whether the LockPtr<> do not point to the same underlying instance.
LockPtr(T_CppObject *cpp_object, Mutex *objmutex, int *refcount, Mutex *refmutex)
For use only in the internal implementation of LockPtr.
LockPtr()
Default constructor.
LockPtr< T_CppObject > & operator=(const LockPtr< T_CastFrom > &src)
Copy from different, but castable type).