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>
301 inline RefPtr<T_CppObject>::RefPtr(T_CppObject *cpp_object)
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>
356 RefPtr<T_CppObject>::swap(RefPtr<T_CppObject> &other)
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>