26 #ifndef _CORE_UTILS_LOCKPTR_H_
27 #define _CORE_UTILS_LOCKPTR_H_
29 #include <core/threading/mutex.h>
30 #include <core/utils/refptr.h>
53 template <
class T_CppObject>
76 explicit inline LockPtr(T_CppObject *cpp_object,
bool recursive_mutex =
false);
88 template <
class T_CastFrom>
110 template <
class T_CastFrom>
152 inline operator bool()
const;
166 template <
class T_CastFrom>
170 T_CppObject *
const cpp_object =
dynamic_cast<T_CppObject *
>(src.operator->());
188 template <
class T_CastFrom>
192 T_CppObject *
const cpp_object =
static_cast<T_CppObject *
>(src.operator->());
206 template <
class T_CastFrom>
210 T_CppObject *
const cpp_object =
const_cast<T_CppObject *
>(src.operator->());
290 T_CppObject * cpp_object_;
291 mutable Mutex *obj_mutex_;
292 mutable int * ref_count_;
293 mutable Mutex *ref_mutex_;
299 template <
class T_CppObject>
305 template <
class T_CppObject>
311 template <
class T_CppObject>
316 template <
class T_CppObject>
319 if (ref_count_ && ref_mutex_) {
324 if (*ref_count_ == 0) {
336 ref_mutex_->unlock();
341 template <
class T_CppObject>
342 inline LockPtr<T_CppObject>::LockPtr(T_CppObject *cpp_object,
bool recursive_mutex)
343 : cpp_object_(cpp_object), obj_mutex_(0), ref_count_(0), ref_mutex_(0)
346 ref_count_ =
new int;
347 ref_mutex_ =
new Mutex(Mutex::RECURSIVE);
348 obj_mutex_ =
new Mutex(recursive_mutex ? Mutex::RECURSIVE : Mutex::NORMAL);
354 template <
class T_CppObject>
355 inline LockPtr<T_CppObject>::LockPtr(T_CppObject *cpp_object,
359 : cpp_object_(cpp_object), obj_mutex_(objmutex), ref_count_(refcount), ref_mutex_(refmutex)
361 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
364 ref_mutex_->unlock();
368 template <
class T_CppObject>
369 inline LockPtr<T_CppObject>::LockPtr(
const LockPtr<T_CppObject> &src)
370 : cpp_object_(src.cpp_object_),
371 obj_mutex_(src.obj_mutex_),
372 ref_count_(src.ref_count_),
373 ref_mutex_(src.ref_mutex_)
375 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
378 ref_mutex_->unlock();
385 template <
class T_CppObject>
386 template <
class T_CastFrom>
387 inline LockPtr<T_CppObject>::LockPtr(
const LockPtr<T_CastFrom> &src)
391 cpp_object_(src.operator->()),
392 obj_mutex_(src.objmutex_ptr()),
393 ref_count_(src.refcount_ptr()),
394 ref_mutex_(src.refmutex_ptr())
396 if (cpp_object_ && obj_mutex_ && ref_count_ && ref_mutex_) {
399 ref_mutex_->unlock();
403 template <
class T_CppObject>
407 T_CppObject *
const temp = cpp_object_;
408 int * temp_count = ref_count_;
409 Mutex * temp_ref_mutex = ref_mutex_;
410 Mutex * temp_obj_mutex = obj_mutex_;
412 cpp_object_ = other.cpp_object_;
413 obj_mutex_ = other.obj_mutex_;
414 ref_count_ = other.ref_count_;
415 ref_mutex_ = other.ref_mutex_;
417 other.cpp_object_ = temp;
418 other.ref_count_ = temp_count;
419 other.ref_mutex_ = temp_ref_mutex;
420 other.obj_mutex_ = temp_obj_mutex;
423 template <
class T_CppObject>
456 template <
class T_CppObject>
465 template <
class T_CppObject>
466 template <
class T_CastFrom>
475 template <
class T_CppObject>
479 return (cpp_object_ == src.cpp_object_);
482 template <
class T_CppObject>
486 return (cpp_object_ != src.cpp_object_);
489 template <
class T_CppObject>
492 return (cpp_object_ != 0);
495 template <
class T_CppObject>
508 template <
class T_CppObject>