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>
265 template <
class T_CppObject>
271 template <
class T_CppObject>
276 template <
class T_CppObject>
279 if (ref_count_ && ref_mutex_) {
284 if (*ref_count_ == 0) {
295 ref_mutex_->unlock();
300 template <
class T_CppObject>
302 : cpp_object_(cpp_object), ref_count_(0), ref_mutex_(0)
305 ref_count_ =
new int;
306 ref_mutex_ =
new Mutex();
312 template <
class T_CppObject>
314 : cpp_object_(cpp_object), ref_count_(refcount), ref_mutex_(refmutex)
316 if (cpp_object_ && ref_count_ && ref_mutex_) {
319 ref_mutex_->unlock();
323 template <
class T_CppObject>
325 : cpp_object_(src.cpp_object_), ref_count_(src.ref_count_), ref_mutex_(src.ref_mutex_)
327 if (cpp_object_ && ref_count_ && ref_mutex_) {
330 ref_mutex_->unlock();
337 template <
class T_CppObject>
338 template <
class T_CastFrom>
343 cpp_object_(src.operator->()),
344 ref_count_(src.refcount_ptr()),
345 ref_mutex_(src.refmutex_ptr())
347 if (cpp_object_ && ref_count_ && ref_mutex_) {
350 ref_mutex_->unlock();
354 template <
class T_CppObject>
358 T_CppObject *
const temp = cpp_object_;
359 int * temp_count = ref_count_;
360 Mutex * temp_mutex = ref_mutex_;
362 cpp_object_ = other.cpp_object_;
363 ref_count_ = other.ref_count_;
364 ref_mutex_ = other.ref_mutex_;
366 other.cpp_object_ = temp;
367 other.ref_count_ = temp_count;
368 other.ref_mutex_ = temp_mutex;
371 template <
class T_CppObject>
404 template <
class T_CppObject>
413 template <
class T_CppObject>
414 template <
class T_CastFrom>
423 template <
class T_CppObject>
427 return (cpp_object_ == src.cpp_object_);
430 template <
class T_CppObject>
434 return (cpp_object_ != src.cpp_object_);
437 template <
class T_CppObject>
440 return (cpp_object_ != 0);
443 template <
class T_CppObject>
451 template <
class T_CppObject>
464 template <
class T_CppObject>
static RefPtr< T_CppObject > cast_dynamic(const RefPtr< T_CastFrom > &src)
Dynamic cast to derived class.
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.
Fawkes library namespace.
T_CppObject * operator*() const
Get underlying 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.
void reset()
Reset pointer.
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<>.
static RefPtr< T_CppObject > cast_const(const RefPtr< T_CastFrom > &src)
Cast to non-const.
int use_count() const
Get current reference count.
~RefPtr()
Destructor - decrements reference count.
bool operator!=(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> do not point to the same underlying instance.
RefPtr<> is a reference-counting shared smartpointer.
void swap(LockPtr< T_CppObject > &lrp, LockPtr< T_CppObject > &rrp)
Swap refptr instances.
T_CppObject * operator->() const
Dereferencing.
Mutex mutual exclusion lock.
bool operator==(const RefPtr< T_CppObject > &src) const
Tests whether the RefPtr<> point to the same underlying instance.
RefPtr()
Default constructor.
static RefPtr< T_CppObject > cast_static(const RefPtr< T_CastFrom > &src)
Static cast to derived class.