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;}
644 #ifndef DOXYGEN_PARSING
646 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
648 unsigned int* ref_count_p;
664 if (!ref_items.ref_count_p)
return;
665 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
666 ref_items.mutex_p->
lock();
667 --(*ref_items.ref_count_p);
668 if (*ref_items.ref_count_p == 0) {
669 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
670 g_slice_free(
unsigned int, ref_items.ref_count_p);
672 delete ref_items.ref_count_p;
674 ref_items.mutex_p->unlock();
675 delete ref_items.mutex_p;
676 delete ref_items.obj_p;
678 else ref_items.mutex_p->unlock();
680 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
681 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
682 g_slice_free(gint, ref_items.ref_count_p);
684 delete ref_items.ref_count_p;
686 delete ref_items.obj_p;
698 if (!ref_items.ref_count_p)
return;
699 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
701 ++(*ref_items.ref_count_p);
703 g_atomic_int_inc(ref_items.ref_count_p);
736 if ((ref_items.obj_p = ptr)) {
737 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
747 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
748 ref_items.ref_count_p = g_slice_new(
unsigned int);
749 *ref_items.ref_count_p = 1;
752 ref_items.ref_count_p =
new unsigned int(1);
755 delete ref_items.mutex_p;
761 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
762 ref_items.ref_count_p = g_slice_new(gint);
763 *ref_items.ref_count_p = 1;
766 ref_items.ref_count_p =
new gint(1);
778 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
779 ref_items.mutex_p = 0;
781 ref_items.ref_count_p = 0;
824 if ((ref_items.obj_p = ptr)) {
825 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
829 catch (std::bad_alloc&) {
835 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
836 ref_items.ref_count_p = g_slice_new(
unsigned int);
837 *ref_items.ref_count_p = 1;
840 ref_items.ref_count_p =
new unsigned int(1);
842 catch (std::bad_alloc&) {
843 delete ref_items.mutex_p;
848 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
849 ref_items.ref_count_p = g_slice_new(gint);
850 *ref_items.ref_count_p = 1;
853 ref_items.ref_count_p =
new gint(1);
855 catch (std::bad_alloc&) {
862 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
863 ref_items.mutex_p = 0;
865 ref_items.ref_count_p = 0;
974 ref_items = sh_ptr.ref_items;
983 ref_items = sh_ptr.ref_items;
984 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
985 sh_ptr.ref_items.mutex_p = 0;
987 sh_ptr.ref_items.ref_count_p = 0;
988 sh_ptr.ref_items.obj_p = 0;
1004 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1005 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1007 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1008 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1023 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1024 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1026 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1027 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1029 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1030 sh_ptr.ref_items.mutex_p = 0;
1032 sh_ptr.ref_items.ref_count_p = 0;
1033 sh_ptr.ref_items.obj_p = 0;
1082 T*
get()
const {
return ref_items.obj_p;}
1107 if (!ref_items.ref_count_p)
return 0;
1108 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1110 return *ref_items.ref_count_p;
1112 return g_atomic_int_get(ref_items.ref_count_p);
1123 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1137 return (s1.
get() == s2.
get());
1168 return std::less<T*>()(s1.get(), s2.get());
1181 return (s1.
get() == s2.
get());
1207 return std::less<T*>()(s1.get(), s2.get());
1210 #endif // CGU_USE_SMART_PTR_COMPARISON
1217 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1222 struct hash<Cgu::SharedPtr<T>> {
1223 typedef std::size_t result_type;
1225 result_type operator()(
const argument_type& s)
const {
1233 struct hash<Cgu::SharedLockPtr<T>> {
1234 typedef std::size_t result_type;
1236 result_type operator()(
const argument_type& s)
const {
1244 #endif // CGU_USE_SMART_PTR_COMPARISON