Go to the documentation of this file.
39 #ifndef CGU_SHARED_PTR_H
40 #define CGU_SHARED_PTR_H
53 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
115 virtual const char*
what()
const throw() {
return "SharedPtrError\n";}
128 namespace SharedPtrAllocFail {
213 #ifndef DOXYGEN_PARSING
215 unsigned int* ref_count_p;
221 if (!ref_items.ref_count_p)
return;
222 --(*ref_items.ref_count_p);
223 if (*ref_items.ref_count_p == 0) {
224 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
225 g_slice_free(
unsigned int, ref_items.ref_count_p);
227 delete ref_items.ref_count_p;
229 delete ref_items.obj_p;
234 if (!ref_items.ref_count_p)
return;
235 ++(*ref_items.ref_count_p);
258 if ((ref_items.obj_p = ptr)) {
259 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
260 ref_items.ref_count_p = g_slice_new(
unsigned int);
261 *ref_items.ref_count_p = 1;
264 ref_items.ref_count_p =
new unsigned int(1);
274 else ref_items.ref_count_p = 0;
306 if ((ref_items.obj_p = ptr)) {
307 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
308 ref_items.ref_count_p = g_slice_new(
unsigned int);
309 *ref_items.ref_count_p = 1;
312 ref_items.ref_count_p =
new unsigned int(1);
314 catch (std::bad_alloc&) {
319 else ref_items.ref_count_p = 0;
394 ref_items = sh_ptr.ref_items;
403 ref_items = sh_ptr.ref_items;
404 sh_ptr.ref_items.ref_count_p = 0;
405 sh_ptr.ref_items.obj_p = 0;
421 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
422 ref_items.obj_p = sh_ptr.ref_items.obj_p;
437 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
438 ref_items.obj_p = sh_ptr.ref_items.obj_p;
439 sh_ptr.ref_items.ref_count_p = 0;
440 sh_ptr.ref_items.obj_p = 0;
489 T*
get()
const {
return ref_items.obj_p;}
509 unsigned int get_refcount()
const {
return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
646 #ifndef DOXYGEN_PARSING
648 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
650 unsigned int* ref_count_p;
666 if (!ref_items.ref_count_p)
return;
667 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
668 ref_items.mutex_p->
lock();
669 --(*ref_items.ref_count_p);
670 if (*ref_items.ref_count_p == 0) {
671 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
672 g_slice_free(
unsigned int, ref_items.ref_count_p);
674 delete ref_items.ref_count_p;
676 ref_items.mutex_p->unlock();
677 delete ref_items.mutex_p;
678 delete ref_items.obj_p;
680 else ref_items.mutex_p->unlock();
682 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
683 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
684 g_slice_free(gint, ref_items.ref_count_p);
686 delete ref_items.ref_count_p;
688 delete ref_items.obj_p;
700 if (!ref_items.ref_count_p)
return;
701 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
703 ++(*ref_items.ref_count_p);
705 g_atomic_int_inc(ref_items.ref_count_p);
738 if ((ref_items.obj_p = ptr)) {
739 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
749 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
750 ref_items.ref_count_p = g_slice_new(
unsigned int);
751 *ref_items.ref_count_p = 1;
754 ref_items.ref_count_p =
new unsigned int(1);
757 delete ref_items.mutex_p;
763 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
764 ref_items.ref_count_p = g_slice_new(gint);
765 *ref_items.ref_count_p = 1;
768 ref_items.ref_count_p =
new gint(1);
780 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
781 ref_items.mutex_p = 0;
783 ref_items.ref_count_p = 0;
826 if ((ref_items.obj_p = ptr)) {
827 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
831 catch (std::bad_alloc&) {
837 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
838 ref_items.ref_count_p = g_slice_new(
unsigned int);
839 *ref_items.ref_count_p = 1;
842 ref_items.ref_count_p =
new unsigned int(1);
844 catch (std::bad_alloc&) {
845 delete ref_items.mutex_p;
850 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
851 ref_items.ref_count_p = g_slice_new(gint);
852 *ref_items.ref_count_p = 1;
855 ref_items.ref_count_p =
new gint(1);
857 catch (std::bad_alloc&) {
864 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
865 ref_items.mutex_p = 0;
867 ref_items.ref_count_p = 0;
976 ref_items = sh_ptr.ref_items;
985 ref_items = sh_ptr.ref_items;
986 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
987 sh_ptr.ref_items.mutex_p = 0;
989 sh_ptr.ref_items.ref_count_p = 0;
990 sh_ptr.ref_items.obj_p = 0;
1006 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1007 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1009 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1010 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1025 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1026 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1028 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1029 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1031 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1032 sh_ptr.ref_items.mutex_p = 0;
1034 sh_ptr.ref_items.ref_count_p = 0;
1035 sh_ptr.ref_items.obj_p = 0;
1084 T*
get()
const {
return ref_items.obj_p;}
1109 if (!ref_items.ref_count_p)
return 0;
1110 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1112 return *ref_items.ref_count_p;
1114 return g_atomic_int_get(ref_items.ref_count_p);
1125 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1139 return (s1.
get() == s2.
get());
1171 return std::less<T*>()(s1.
get(), s2.
get());
1184 return (s1.
get() == s2.
get());
1211 return std::less<T*>()(s1.
get(), s2.
get());
1214 #endif // CGU_USE_SMART_PTR_COMPARISON
1221 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1226 struct hash<
Cgu::SharedPtr<T>> {
1227 typedef std::size_t result_type;
1229 result_type operator()(
const argument_type& s)
const {
1237 struct hash<
Cgu::SharedLockPtr<T>> {
1238 typedef std::size_t result_type;
1240 result_type operator()(
const argument_type& s)
const {
1248 #endif // CGU_USE_SMART_PTR_COMPARISON
This is an exception struct thrown as an alternative to deleting a managed object when internal memor...
Definition: shared_ptr.h:113
SharedLockPtr & operator=(SharedLockPtr sh_ptr)
Definition: shared_ptr.h:1048
SharedPtr & operator=(const SharedPtr< U > &sh_ptr)
Definition: shared_ptr.h:467
Definition: application.h:44
SharedLockPtr(T *ptr, Cgu::SharedPtrAllocFail::Leave tag)
Definition: shared_ptr.h:824
SharedLockPtr(const SharedLockPtr &sh_ptr)
Definition: shared_ptr.h:975
T * get() const
Definition: shared_ptr.h:1084
void reset(T *ptr, Cgu::SharedPtrAllocFail::Leave tag)
Definition: shared_ptr.h:966
T * operator->() const
Definition: shared_ptr.h:1097
void reset(T *ptr, Cgu::SharedPtrAllocFail::Leave tag)
Definition: shared_ptr.h:384
@ leave
Definition: shared_ptr.h:129
void reset(T *ptr=0)
Definition: shared_ptr.h:910
bool operator!=(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:613
unsigned int get_refcount() const
Definition: shared_ptr.h:509
SharedPtr(const SharedPtr &sh_ptr)
Definition: shared_ptr.h:393
T * obj
Definition: shared_ptr.h:114
SharedLockPtr(SharedLockPtr &&sh_ptr)
Definition: shared_ptr.h:984
SharedLockPtr & operator=(const SharedLockPtr< U > &sh_ptr)
Definition: shared_ptr.h:1062
bool operator<(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:632
SharedLockPtr(SharedLockPtr< U > &&sh_ptr)
Definition: shared_ptr.h:1021
void swap(Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2)
Definition: async_queue.h:1481
~SharedLockPtr()
Definition: shared_ptr.h:1122
SharedPtr & operator=(SharedPtr sh_ptr)
Definition: shared_ptr.h:453
SharedLockPtr & operator=(SharedLockPtr< U > &&sh_ptr)
Definition: shared_ptr.h:1075
This is a smart pointer for managing the lifetime of objects allocated on freestore.
Definition: shared_ptr.h:211
Leave
Definition: shared_ptr.h:129
T * get() const
Definition: shared_ptr.h:489
void reset(T *ptr=0)
Definition: shared_ptr.h:346
SharedPtr(T *ptr=0)
Definition: shared_ptr.h:256
SharedPtr & operator=(SharedPtr< U > &&sh_ptr)
Definition: shared_ptr.h:480
~SharedPtr()
Definition: shared_ptr.h:515
virtual const char * what() const
Definition: shared_ptr.h:115
int lock()
Definition: mutex.h:147
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:207
A specialization of std::hash for Cgu::Callback::FunctorArg, Cgu::Callback::SafeFunctorArg,...
friend class SharedLockPtr
Definition: shared_ptr.h:993
SharedLockPtr(T *ptr=0)
Definition: shared_ptr.h:736
T & operator*() const
Definition: shared_ptr.h:495
SharedPtr(T *ptr, Cgu::SharedPtrAllocFail::Leave tag)
Definition: shared_ptr.h:304
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
This is a smart pointer for managing the lifetime of objects allocated on freestore,...
Definition: shared_ptr.h:644
SharedPtr(const SharedPtr< U > &sh_ptr)
Definition: shared_ptr.h:417
bool operator==(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:600
T & operator*() const
Definition: shared_ptr.h:1090
friend class SharedPtr
Definition: shared_ptr.h:408
SharedPtr(SharedPtr &&sh_ptr)
Definition: shared_ptr.h:402
SharedPtr(SharedPtr< U > &&sh_ptr)
Definition: shared_ptr.h:433
T * operator->() const
Definition: shared_ptr.h:502
A wrapper class for pthread mutexes.
Definition: mutex.h:117
SharedLockPtr(const SharedLockPtr< U > &sh_ptr)
Definition: shared_ptr.h:1002
unsigned int get_refcount() const
Definition: shared_ptr.h:1108
SharedPtrError(T *p)
Definition: shared_ptr.h:116