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>
145 inline operator bool()
const;
166 template <
class T_CastFrom>
178 template <
class T_CastFrom>
190 template <
class T_CastFrom>
198 explicit inline RefPtr(T_CppObject *cpp_object,
int *refcount,
Mutex *refmutex);
221 T_CppObject *__cpp_object;
222 mutable int *__ref_count;
223 mutable Mutex *__ref_mutex;
231 template <
class T_CppObject>
inline
238 template <
class T_CppObject>
inline
244 template <
class T_CppObject>
inline
252 template <
class T_CppObject>
inline
255 if(__ref_count && __ref_mutex)
261 if(*__ref_count == 0)
274 __ref_mutex->unlock();
280 template <
class T_CppObject>
inline
283 __cpp_object(cpp_object),
289 __ref_count =
new int;
290 __ref_mutex =
new Mutex();
296 template <
class T_CppObject>
inline
299 __cpp_object(cpp_object),
300 __ref_count(refcount),
301 __ref_mutex(refmutex)
303 if(__cpp_object && __ref_count && __ref_mutex) {
310 template <
class T_CppObject>
inline
313 __cpp_object (src.__cpp_object),
314 __ref_count(src.__ref_count),
315 __ref_mutex(src.__ref_mutex)
317 if(__cpp_object && __ref_count && __ref_mutex)
328 template <
class T_CppObject>
329 template <
class T_CastFrom>
336 __cpp_object (src.operator->()),
337 __ref_count(src.refcount_ptr()),
338 __ref_mutex(src.refmutex_ptr())
340 if(__cpp_object && __ref_count && __ref_mutex) {
347 template <
class T_CppObject>
inline
351 T_CppObject *
const temp = __cpp_object;
352 int *temp_count = __ref_count;
353 Mutex *temp_mutex = __ref_mutex;
355 __cpp_object = other.__cpp_object;
356 __ref_count = other.__ref_count;
357 __ref_mutex = other.__ref_mutex;
359 other.__cpp_object = temp;
360 other.__ref_count = temp_count;
361 other.__ref_mutex = temp_mutex;
364 template <
class T_CppObject>
inline
397 template <
class T_CppObject>
inline
407 template <
class T_CppObject>
408 template <
class T_CastFrom>
418 template <
class T_CppObject>
inline
422 return (__cpp_object == src.__cpp_object);
425 template <
class T_CppObject>
inline
429 return (__cpp_object != src.__cpp_object);
432 template <
class T_CppObject>
inline
435 return (__cpp_object != 0);
438 template <
class T_CppObject>
inline
445 template <
class T_CppObject>
inline
452 template <
class T_CppObject>
453 template <
class T_CastFrom>
458 T_CppObject *
const cpp_object =
dynamic_cast<T_CppObject*
>(src.operator->());
466 template <
class T_CppObject>
467 template <
class T_CastFrom>
472 T_CppObject *
const cpp_object =
static_cast<T_CppObject*
>(src.operator->());
477 template <
class T_CppObject>
478 template <
class T_CastFrom>
483 T_CppObject *
const cpp_object =
const_cast<T_CppObject*
>(src.operator->());
494 template <
class T_CppObject>
inline
void swap(RefPtr< T_CppObject > &lrp, RefPtr< T_CppObject > &rrp)
Swap refptr instances.
void unlock()
Unlock the mutex.
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.
static RefPtr< T_CppObject > cast_static(const RefPtr< T_CastFrom > &src)
Static cast to derived class.
RefPtr< T_CppObject > & operator=(const RefPtr< T_CppObject > &src)
Copy from another RefPtr.
T_CppObject * operator->() const
Dereferencing.
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<>.
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.
~RefPtr()
Destructor - decrements reference count.
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.
RefPtr<> is a reference-counting shared smartpointer.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
int use_count() const
Get current reference count.
RefPtr()
Default constructor.
T_CppObject * operator*() const
Get underlying pointer.