26#ifndef _CORE_UTILS_REFPTR_H_
27#define _CORE_UTILS_REFPTR_H_
29#include <core/threading/mutex.h>
48template <
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_;
259template <
class T_CppObject>
266template <
class T_CppObject>
273template <
class T_CppObject>
278template <
class T_CppObject>
281 if (ref_count_ && ref_mutex_) {
286 if (*ref_count_ == 0) {
297 ref_mutex_->unlock();
302template <
class T_CppObject>
304: cpp_object_(cpp_object), ref_count_(0), ref_mutex_(0)
307 ref_count_ =
new int;
308 ref_mutex_ =
new Mutex();
314template <
class T_CppObject>
316: cpp_object_(cpp_object), ref_count_(refcount), ref_mutex_(refmutex)
318 if (cpp_object_ && ref_count_ && ref_mutex_) {
325template <
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_) {
339template <
class T_CppObject>
340template <
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_) {
356template <
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;
373template <
class T_CppObject>
406template <
class T_CppObject>
415template <
class T_CppObject>
416template <
class T_CastFrom>
425template <
class T_CppObject>
429 return (cpp_object_ == src.cpp_object_);
432template <
class T_CppObject>
436 return (cpp_object_ != src.cpp_object_);
439template <
class T_CppObject>
442 return (cpp_object_ != 0);
445template <
class T_CppObject>
453template <
class T_CppObject>
466template <
class T_CppObject>
Mutex mutual exclusion lock.
void lock()
Lock this mutex.
void unlock()
Unlock the mutex.
RefPtr<> is a reference-counting shared smartpointer.
RefPtr< T_CppObject > & operator=(const RefPtr< T_CppObject > &src)
Copy from another RefPtr.
void clear()
Set underlying instance to 0, decrementing reference count of existing instance appropriately.
Mutex * refmutex_ptr() const
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.
T_CppObject * operator*() const
Get underlying pointer.
void swap(RefPtr< T_CppObject > &lrp, RefPtr< T_CppObject > &rrp)
Swap refptr instances.
int * refcount_ptr() const
For use only in the internal implementation of sharedptr.
bool operator!=(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> do not point to the same underlying instance.
RefPtr(const RefPtr< T_CppObject > &src)
Copy constructor This increments the shared reference count.
RefPtr()
Default constructor.
RefPtr(T_CppObject *cpp_object)
Constructor that takes ownership.
static RefPtr< T_CppObject > cast_static(const RefPtr< T_CastFrom > &src)
Static cast to derived class.
void reset()
Reset pointer.
RefPtr< T_CppObject > & operator=(const RefPtr< T_CastFrom > &src)
Copy from different, but castable type).
~RefPtr()
Destructor - decrements reference count.
RefPtr(const RefPtr< T_CastFrom > &src)
Copy constructor (from different, but castable type).
T_CppObject * operator->() const
Dereferencing.
int use_count() const
Get current reference count.
void swap(RefPtr< T_CppObject > &other)
Swap the contents of two RefPtr<>.
RefPtr(T_CppObject *cpp_object, int *refcount, Mutex *refmutex)
For use only in the internal implementation of sharedptr.
RefPtr< T_CppObject > & operator=(T_CppObject *ptr)
Assign object and claim ownership.
static RefPtr< T_CppObject > cast_const(const RefPtr< T_CastFrom > &src)
Cast to non-const.
bool operator==(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> point to the same underlying instance.
Fawkes library namespace.