Fawkes API
Fawkes Development Version
|
26 #ifndef _CORE_UTILS_REFPTR_H_
27 #define _CORE_UTILS_REFPTR_H_
29 #include <core/threading/mutex.h>
48 template <
class T_CppObject>
67 explicit inline RefPtr(T_CppObject *cpp_object);
79 template <
class T_CastFrom>
101 template <
class T_CastFrom>
143 inline operator bool()
const;
163 template <
class T_CastFrom>
167 T_CppObject *
const cpp_object =
dynamic_cast<T_CppObject *
>(src.operator->());
185 template <
class T_CastFrom>
189 T_CppObject *
const cpp_object =
static_cast<T_CppObject *
>(src.operator->());
203 template <
class T_CastFrom>
207 T_CppObject *
const cpp_object =
const_cast<T_CppObject *
>(src.operator->());
217 explicit inline RefPtr(T_CppObject *cpp_object,
int *refcount,
Mutex *refmutex);
251 T_CppObject * cpp_object_;
252 mutable int * ref_count_;
253 mutable Mutex *ref_mutex_;
259 template <
class T_CppObject>
266 template <
class T_CppObject>
273 template <
class T_CppObject>
278 template <
class T_CppObject>
281 if (ref_count_ && ref_mutex_) {
286 if (*ref_count_ == 0) {
297 ref_mutex_->unlock();
302 template <
class T_CppObject>
304 : cpp_object_(cpp_object), ref_count_(0), ref_mutex_(0)
307 ref_count_ =
new int;
308 ref_mutex_ =
new Mutex();
314 template <
class T_CppObject>
316 : cpp_object_(cpp_object), ref_count_(refcount), ref_mutex_(refmutex)
318 if (cpp_object_ && ref_count_ && ref_mutex_) {
325 template <
class T_CppObject>
327 : cpp_object_(src.cpp_object_), ref_count_(src.ref_count_), ref_mutex_(src.ref_mutex_)
329 if (cpp_object_ && ref_count_ && ref_mutex_) {
339 template <
class T_CppObject>
340 template <
class T_CastFrom>
345 cpp_object_(src.operator->()),
346 ref_count_(src.refcount_ptr()),
347 ref_mutex_(src.refmutex_ptr())
349 if (cpp_object_ && ref_count_ && ref_mutex_) {
356 template <
class T_CppObject>
360 T_CppObject *
const temp = cpp_object_;
361 int * temp_count = ref_count_;
362 Mutex * temp_mutex = ref_mutex_;
364 cpp_object_ = other.cpp_object_;
365 ref_count_ = other.ref_count_;
366 ref_mutex_ = other.ref_mutex_;
368 other.cpp_object_ = temp;
369 other.ref_count_ = temp_count;
370 other.ref_mutex_ = temp_mutex;
373 template <
class T_CppObject>
406 template <
class T_CppObject>
415 template <
class T_CppObject>
416 template <
class T_CastFrom>
425 template <
class T_CppObject>
429 return (cpp_object_ == src.cpp_object_);
432 template <
class T_CppObject>
436 return (cpp_object_ != src.cpp_object_);
439 template <
class T_CppObject>
442 return (cpp_object_ != 0);
445 template <
class T_CppObject>
453 template <
class T_CppObject>
466 template <
class T_CppObject>
void lock()
Lock this mutex.
~RefPtr()
Destructor - decrements reference count.
int use_count() const
Get current reference count.
Mutex mutual exclusion lock.
RefPtr<> is a reference-counting shared smartpointer.
RefPtr< T_CppObject > & operator=(const RefPtr< T_CastFrom > &src)
Copy from different, but castable type).
bool operator!=(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> do not point to the same underlying instance.
static RefPtr< T_CppObject > cast_static(const RefPtr< T_CastFrom > &src)
Static cast to derived class.
void unlock()
Unlock the mutex.
bool operator==(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> point to the same underlying instance.
T_CppObject * operator->() const
Dereferencing.
RefPtr(T_CppObject *cpp_object, int *refcount, Mutex *refmutex)
For use only in the internal implementation of sharedptr.
static RefPtr< T_CppObject > cast_dynamic(const RefPtr< T_CastFrom > &src)
Dynamic cast to derived class.
RefPtr(const RefPtr< T_CastFrom > &src)
Copy constructor (from different, but castable type).
Fawkes library namespace.
RefPtr()
Default constructor.
RefPtr(T_CppObject *cpp_object)
Constructor that takes ownership.
void swap(RefPtr< T_CppObject > &lrp, RefPtr< T_CppObject > &rrp)
Swap refptr instances.
T_CppObject * operator*() const
Get underlying pointer.
int * refcount_ptr() const
For use only in the internal implementation of sharedptr.
RefPtr< T_CppObject > & operator=(T_CppObject *ptr)
Assign object and claim ownership.
void clear()
Set underlying instance to 0, decrementing reference count of existing instance appropriately.
void swap(RefPtr< T_CppObject > &other)
Swap the contents of two RefPtr<>.
RefPtr(const RefPtr< T_CppObject > &src)
Copy constructor This increments the shared reference count.
void reset()
Reset pointer.
RefPtr< T_CppObject > & operator=(const RefPtr< T_CppObject > &src)
Copy from another RefPtr.
Mutex * refmutex_ptr() const
For use only in the internal implementation of sharedptr.
static RefPtr< T_CppObject > cast_const(const RefPtr< T_CastFrom > &src)
Cast to non-const.