39 #ifndef CGU_SHARED_PTR_H
40 #define CGU_SHARED_PTR_H
54 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
116 virtual const char*
what()
const throw() {
return "SharedPtrError\n";}
129 namespace SharedPtrAllocFail {
201 #ifndef DOXYGEN_PARSING
203 unsigned int* ref_count_p;
209 if (!ref_items.ref_count_p)
return;
210 --(*ref_items.ref_count_p);
211 if (*ref_items.ref_count_p == 0) {
212 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
213 g_slice_free(
unsigned int, ref_items.ref_count_p);
215 delete ref_items.ref_count_p;
217 delete ref_items.obj_p;
222 if (!ref_items.ref_count_p)
return;
223 ++(*ref_items.ref_count_p);
246 if ((ref_items.obj_p = ptr)) {
247 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
248 ref_items.ref_count_p = g_slice_new(
unsigned int);
249 *ref_items.ref_count_p = 1;
252 ref_items.ref_count_p =
new unsigned int(1);
262 else ref_items.ref_count_p = 0;
294 if ((ref_items.obj_p = ptr)) {
295 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
296 ref_items.ref_count_p = g_slice_new(
unsigned int);
297 *ref_items.ref_count_p = 1;
300 ref_items.ref_count_p =
new unsigned int(1);
302 catch (std::bad_alloc&) {
307 else ref_items.ref_count_p = 0;
382 ref_items = sh_ptr.ref_items;
391 ref_items = sh_ptr.ref_items;
392 sh_ptr.ref_items.ref_count_p = 0;
393 sh_ptr.ref_items.obj_p = 0;
409 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
410 ref_items.obj_p = sh_ptr.ref_items.obj_p;
425 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
426 ref_items.obj_p = sh_ptr.ref_items.obj_p;
427 sh_ptr.ref_items.ref_count_p = 0;
428 sh_ptr.ref_items.obj_p = 0;
477 T*
get()
const {
return ref_items.obj_p;}
497 unsigned int get_refcount()
const {
return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
613 #ifndef DOXYGEN_PARSING
615 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
617 unsigned int* ref_count_p;
633 if (!ref_items.ref_count_p)
return;
634 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
635 ref_items.mutex_p->
lock();
636 --(*ref_items.ref_count_p);
637 if (*ref_items.ref_count_p == 0) {
638 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
639 g_slice_free(
unsigned int, ref_items.ref_count_p);
641 delete ref_items.ref_count_p;
643 ref_items.mutex_p->unlock();
644 delete ref_items.mutex_p;
645 delete ref_items.obj_p;
647 else ref_items.mutex_p->unlock();
649 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
650 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
651 g_slice_free(gint, ref_items.ref_count_p);
653 delete ref_items.ref_count_p;
655 delete ref_items.obj_p;
667 if (!ref_items.ref_count_p)
return;
668 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
670 ++(*ref_items.ref_count_p);
672 g_atomic_int_inc(ref_items.ref_count_p);
705 if ((ref_items.obj_p = ptr)) {
706 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
716 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
717 ref_items.ref_count_p = g_slice_new(
unsigned int);
718 *ref_items.ref_count_p = 1;
721 ref_items.ref_count_p =
new unsigned int(1);
724 delete ref_items.mutex_p;
730 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
731 ref_items.ref_count_p = g_slice_new(gint);
732 *ref_items.ref_count_p = 1;
735 ref_items.ref_count_p =
new gint(1);
747 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
748 ref_items.mutex_p = 0;
750 ref_items.ref_count_p = 0;
793 if ((ref_items.obj_p = ptr)) {
794 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
798 catch (std::bad_alloc&) {
804 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
805 ref_items.ref_count_p = g_slice_new(
unsigned int);
806 *ref_items.ref_count_p = 1;
809 ref_items.ref_count_p =
new unsigned int(1);
811 catch (std::bad_alloc&) {
812 delete ref_items.mutex_p;
817 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
818 ref_items.ref_count_p = g_slice_new(gint);
819 *ref_items.ref_count_p = 1;
822 ref_items.ref_count_p =
new gint(1);
824 catch (std::bad_alloc&) {
831 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
832 ref_items.mutex_p = 0;
834 ref_items.ref_count_p = 0;
943 ref_items = sh_ptr.ref_items;
952 ref_items = sh_ptr.ref_items;
953 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
954 sh_ptr.ref_items.mutex_p = 0;
956 sh_ptr.ref_items.ref_count_p = 0;
957 sh_ptr.ref_items.obj_p = 0;
973 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
974 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
976 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
977 ref_items.obj_p = sh_ptr.ref_items.obj_p;
992 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
993 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
995 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
996 ref_items.obj_p = sh_ptr.ref_items.obj_p;
998 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
999 sh_ptr.ref_items.mutex_p = 0;
1001 sh_ptr.ref_items.ref_count_p = 0;
1002 sh_ptr.ref_items.obj_p = 0;
1051 T*
get()
const {
return ref_items.obj_p;}
1076 if (!ref_items.ref_count_p)
return 0;
1077 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1079 return *ref_items.ref_count_p;
1081 return g_atomic_int_get(ref_items.ref_count_p);
1092 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1106 return (s1.
get() == s2.
get());
1137 return std::less<T*>()(s1.get(), s2.get());
1150 return (s1.
get() == s2.
get());
1176 return std::less<T*>()(s1.get(), s2.get());
1179 #endif // CGU_USE_SMART_PTR_COMPARISON
1186 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1191 struct hash<Cgu::SharedPtr<T>> {
1192 typedef std::size_t result_type;
1194 result_type operator()(
const argument_type& s)
const {
1202 struct hash<Cgu::SharedLockPtr<T>> {
1203 typedef std::size_t result_type;
1205 result_type operator()(
const argument_type& s)
const {
1213 #endif // CGU_USE_SMART_PTR_COMPARISON