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;}
642 #ifndef DOXYGEN_PARSING
644 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
646 unsigned int* ref_count_p;
662 if (!ref_items.ref_count_p)
return;
663 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
664 ref_items.mutex_p->
lock();
665 --(*ref_items.ref_count_p);
666 if (*ref_items.ref_count_p == 0) {
667 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
668 g_slice_free(
unsigned int, ref_items.ref_count_p);
670 delete ref_items.ref_count_p;
672 ref_items.mutex_p->unlock();
673 delete ref_items.mutex_p;
674 delete ref_items.obj_p;
676 else ref_items.mutex_p->unlock();
678 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
679 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
680 g_slice_free(gint, ref_items.ref_count_p);
682 delete ref_items.ref_count_p;
684 delete ref_items.obj_p;
696 if (!ref_items.ref_count_p)
return;
697 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
699 ++(*ref_items.ref_count_p);
701 g_atomic_int_inc(ref_items.ref_count_p);
734 if ((ref_items.obj_p = ptr)) {
735 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
745 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
746 ref_items.ref_count_p = g_slice_new(
unsigned int);
747 *ref_items.ref_count_p = 1;
750 ref_items.ref_count_p =
new unsigned int(1);
753 delete ref_items.mutex_p;
759 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
760 ref_items.ref_count_p = g_slice_new(gint);
761 *ref_items.ref_count_p = 1;
764 ref_items.ref_count_p =
new gint(1);
776 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
777 ref_items.mutex_p = 0;
779 ref_items.ref_count_p = 0;
822 if ((ref_items.obj_p = ptr)) {
823 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
827 catch (std::bad_alloc&) {
833 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
834 ref_items.ref_count_p = g_slice_new(
unsigned int);
835 *ref_items.ref_count_p = 1;
838 ref_items.ref_count_p =
new unsigned int(1);
840 catch (std::bad_alloc&) {
841 delete ref_items.mutex_p;
846 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
847 ref_items.ref_count_p = g_slice_new(gint);
848 *ref_items.ref_count_p = 1;
851 ref_items.ref_count_p =
new gint(1);
853 catch (std::bad_alloc&) {
860 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
861 ref_items.mutex_p = 0;
863 ref_items.ref_count_p = 0;
972 ref_items = sh_ptr.ref_items;
981 ref_items = sh_ptr.ref_items;
982 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
983 sh_ptr.ref_items.mutex_p = 0;
985 sh_ptr.ref_items.ref_count_p = 0;
986 sh_ptr.ref_items.obj_p = 0;
1002 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1003 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1005 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1006 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1021 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1022 ref_items.mutex_p = sh_ptr.ref_items.mutex_p;
1024 ref_items.ref_count_p = sh_ptr.ref_items.ref_count_p;
1025 ref_items.obj_p = sh_ptr.ref_items.obj_p;
1027 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1028 sh_ptr.ref_items.mutex_p = 0;
1030 sh_ptr.ref_items.ref_count_p = 0;
1031 sh_ptr.ref_items.obj_p = 0;
1080 T*
get()
const {
return ref_items.obj_p;}
1105 if (!ref_items.ref_count_p)
return 0;
1106 #ifdef CGU_SHARED_LOCK_PTR_USE_MUTEX
1108 return *ref_items.ref_count_p;
1110 return g_atomic_int_get(ref_items.ref_count_p);
1121 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1135 return (s1.
get() == s2.
get());
1166 return std::less<T*>()(s1.get(), s2.get());
1179 return (s1.
get() == s2.
get());
1205 return std::less<T*>()(s1.get(), s2.get());
1208 #endif // CGU_USE_SMART_PTR_COMPARISON
1215 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1220 struct hash<Cgu::SharedPtr<T>> {
1221 typedef std::size_t result_type;
1223 result_type operator()(
const argument_type& s)
const {
1231 struct hash<Cgu::SharedLockPtr<T>> {
1232 typedef std::size_t result_type;
1234 result_type operator()(
const argument_type& s)
const {
1242 #endif // CGU_USE_SMART_PTR_COMPARISON