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 {
214 #ifndef DOXYGEN_PARSING
216 unsigned int* ref_count_p;
222 if (!ref_items.ref_count_p)
return;
223 --(*ref_items.ref_count_p);
224 if (*ref_items.ref_count_p == 0) {
225 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
226 g_slice_free(
unsigned int, ref_items.ref_count_p);
228 delete ref_items.ref_count_p;
230 delete ref_items.obj_p;
235 if (!ref_items.ref_count_p)
return;
236 ++(*ref_items.ref_count_p);
259 if ((ref_items.obj_p = ptr)) {
260 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
261 ref_items.ref_count_p = g_slice_new(
unsigned int);
262 *ref_items.ref_count_p = 1;
265 ref_items.ref_count_p =
new unsigned int(1);
275 else ref_items.ref_count_p = 0;
307 if ((ref_items.obj_p = ptr)) {
308 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
309 ref_items.ref_count_p = g_slice_new(
unsigned int);
310 *ref_items.ref_count_p = 1;
313 ref_items.ref_count_p =
new unsigned int(1);
315 catch (std::bad_alloc&) {
320 else ref_items.ref_count_p = 0;
395 ref_items = sh_ptr.ref_items;
404 ref_items = sh_ptr.ref_items;
405 sh_ptr.ref_items.ref_count_p = 0;
406 sh_ptr.ref_items.obj_p = 0;
422 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
423 ref_items.obj_p = sh_ptr.ref_items.obj_p;
438 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
439 ref_items.obj_p = sh_ptr.ref_items.obj_p;
440 sh_ptr.ref_items.ref_count_p = 0;
441 sh_ptr.ref_items.obj_p = 0;
490 T*
get()
const {
return ref_items.obj_p;}
510 unsigned int get_refcount()
const {
return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
639 #ifndef DOXYGEN_PARSING
641 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
643 unsigned int* ref_count_p;
659 if (!ref_items.ref_count_p)
return;
660 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
661 ref_items.mutex_p->
lock();
662 --(*ref_items.ref_count_p);
663 if (*ref_items.ref_count_p == 0) {
664 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
665 g_slice_free(
unsigned int, ref_items.ref_count_p);
667 delete ref_items.ref_count_p;
669 ref_items.mutex_p->unlock();
670 delete ref_items.mutex_p;
671 delete ref_items.obj_p;
673 else ref_items.mutex_p->unlock();
675 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
676 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
677 g_slice_free(gint, ref_items.ref_count_p);
679 delete ref_items.ref_count_p;
681 delete ref_items.obj_p;
693 if (!ref_items.ref_count_p)
return;
694 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
696 ++(*ref_items.ref_count_p);
698 g_atomic_int_inc(ref_items.ref_count_p);
731 if ((ref_items.obj_p = ptr)) {
732 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
742 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
743 ref_items.ref_count_p = g_slice_new(
unsigned int);
744 *ref_items.ref_count_p = 1;
747 ref_items.ref_count_p =
new unsigned int(1);
750 delete ref_items.mutex_p;
756 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
757 ref_items.ref_count_p = g_slice_new(gint);
758 *ref_items.ref_count_p = 1;
761 ref_items.ref_count_p =
new gint(1);
773 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
774 ref_items.mutex_p = 0;
776 ref_items.ref_count_p = 0;
819 if ((ref_items.obj_p = ptr)) {
820 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
824 catch (std::bad_alloc&) {
830 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
831 ref_items.ref_count_p = g_slice_new(
unsigned int);
832 *ref_items.ref_count_p = 1;
835 ref_items.ref_count_p =
new unsigned int(1);
837 catch (std::bad_alloc&) {
838 delete ref_items.mutex_p;
843 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
844 ref_items.ref_count_p = g_slice_new(gint);
845 *ref_items.ref_count_p = 1;
848 ref_items.ref_count_p =
new gint(1);
850 catch (std::bad_alloc&) {
857 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
858 ref_items.mutex_p = 0;
860 ref_items.ref_count_p = 0;
969 ref_items = sh_ptr.ref_items;
978 ref_items = sh_ptr.ref_items;
979 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
980 sh_ptr.ref_items.mutex_p = 0;
982 sh_ptr.ref_items.ref_count_p = 0;
983 sh_ptr.ref_items.obj_p = 0;
999 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1000 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1002 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1003 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1018 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1019 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1021 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1022 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1024 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1025 sh_ptr.ref_items.mutex_p = 0;
1027 sh_ptr.ref_items.ref_count_p = 0;
1028 sh_ptr.ref_items.obj_p = 0;
1077 T*
get()
const {
return ref_items.obj_p;}
1102 if (!ref_items.ref_count_p)
return 0;
1103 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1105 return *ref_items.ref_count_p;
1107 return g_atomic_int_get(ref_items.ref_count_p);
1118 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1132 return (s1.
get() == s2.
get());
1163 return std::less<T*>()(s1.get(), s2.get());
1176 return (s1.
get() == s2.
get());
1202 return std::less<T*>()(s1.get(), s2.get());
1205 #endif // CGU_USE_SMART_PTR_COMPARISON
1212 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1217 struct hash<Cgu::SharedPtr<T>> {
1218 typedef std::size_t result_type;
1220 result_type operator()(
const argument_type& s)
const {
1228 struct hash<Cgu::SharedLockPtr<T>> {
1229 typedef std::size_t result_type;
1231 result_type operator()(
const argument_type& s)
const {
1239 #endif // CGU_USE_SMART_PTR_COMPARISON