Go to the documentation of this file.
39 #ifndef CGU_SHARED_HANDLE_H
40 #define CGU_SHARED_HANDLE_H
56 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
207 std::free(
const_cast<void*
>(obj));
226 g_free(
const_cast<void*
>(obj));
271 g_slice_free1(
sizeof(*obj), (
void*)obj);
338 template <
class U>
void destroy(U& obj) {obj.~U();}
342 g_slice_free1(
sizeof(*obj), (
void*)obj);
383 g_slice_free1(block_size,
const_cast<void*
>(obj));
486 virtual const char*
what()
const throw() {
return "SharedHandleError\n";}
499 namespace SharedHandleAllocFail {
503 template <
class T,
class Dealloc = StandardArrayDelete<T> >
class SharedHandle {
507 #ifndef DOXYGEN_PARSING
509 unsigned int* ref_count_p;
515 if (!ref_items.ref_count_p)
return;
516 --(*ref_items.ref_count_p);
517 if (*ref_items.ref_count_p == 0) {
518 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
519 g_slice_free(
unsigned int, ref_items.ref_count_p);
521 delete ref_items.ref_count_p;
523 deleter(ref_items.obj);
528 if (!ref_items.ref_count_p)
return;
529 ++(*ref_items.ref_count_p);
558 if ((ref_items.obj = ptr)) {
559 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
560 ref_items.ref_count_p = g_slice_new(
unsigned int);
561 *ref_items.ref_count_p = 1;
564 ref_items.ref_count_p =
new unsigned int(1);
574 else ref_items.ref_count_p = 0;
613 if ((ref_items.obj = ptr)) {
614 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
615 ref_items.ref_count_p = g_slice_new(
unsigned int);
616 *ref_items.ref_count_p = 1;
619 ref_items.ref_count_p =
new unsigned int(1);
621 catch (std::bad_alloc&) {
626 else ref_items.ref_count_p = 0;
706 ref_items = sh_hand.ref_items;
729 T
get()
const {
return ref_items.obj;}
736 operator T()
const {
return ref_items.obj;}
743 unsigned int get_refcount()
const {
return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
782 template <
class T,
class Dealloc = StandardArrayDelete<T> >
class ScopedHandle {
789 ScopedHandle(
const ScopedHandle&);
790 ScopedHandle& operator=(
const ScopedHandle&);
816 if (ptr) deleter(ptr);
827 T
release() {T tmp = obj; obj = 0;
return tmp;}
834 T
get()
const {
return obj;}
841 operator T()
const {
return obj;}
942 #ifndef DOXYGEN_PARSING
944 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
946 unsigned int* ref_count_p;
962 if (!ref_items.ref_count_p)
return;
963 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
964 ref_items.mutex_p->
lock();
965 --(*ref_items.ref_count_p);
966 if (*ref_items.ref_count_p == 0) {
967 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
968 g_slice_free(
unsigned int, ref_items.ref_count_p);
970 delete ref_items.ref_count_p;
972 ref_items.mutex_p->unlock();
973 delete ref_items.mutex_p;
974 deleter(ref_items.obj);
976 else ref_items.mutex_p->unlock();
978 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
979 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
980 g_slice_free(gint, ref_items.ref_count_p);
982 delete ref_items.ref_count_p;
984 deleter(ref_items.obj);
996 if (!ref_items.ref_count_p)
return;
997 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
999 ++(*ref_items.ref_count_p);
1001 g_atomic_int_inc(ref_items.ref_count_p);
1041 if ((ref_items.obj = ptr)) {
1042 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1052 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1053 ref_items.ref_count_p = g_slice_new(
unsigned int);
1054 *ref_items.ref_count_p = 1;
1057 ref_items.ref_count_p =
new unsigned int(1);
1060 delete ref_items.mutex_p;
1066 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1067 ref_items.ref_count_p = g_slice_new(gint);
1068 *ref_items.ref_count_p = 1;
1071 ref_items.ref_count_p =
new gint(1);
1083 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1084 ref_items.mutex_p = 0;
1086 ref_items.ref_count_p = 0;
1137 if ((ref_items.obj = ptr)) {
1138 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1142 catch (std::bad_alloc&) {
1148 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1149 ref_items.ref_count_p = g_slice_new(
unsigned int);
1150 *ref_items.ref_count_p = 1;
1153 ref_items.ref_count_p =
new unsigned int(1);
1155 catch (std::bad_alloc&) {
1156 delete ref_items.mutex_p;
1161 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1162 ref_items.ref_count_p = g_slice_new(gint);
1163 *ref_items.ref_count_p = 1;
1166 ref_items.ref_count_p =
new gint(1);
1168 catch (std::bad_alloc&) {
1175 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1176 ref_items.mutex_p = 0;
1178 ref_items.ref_count_p = 0;
1290 ref_items = sh_hand.ref_items;
1304 std::swap(ref_items, sh_hand.ref_items);
1313 T
get()
const {
return ref_items.obj;}
1320 operator T()
const {
return ref_items.obj;}
1332 if (!ref_items.ref_count_p)
return 0;
1333 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1335 return *ref_items.ref_count_p;
1337 return g_atomic_int_get(ref_items.ref_count_p);
1348 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1363 template <
class T,
class Dealloc>
1365 return (s1.get() == s2.get());
1379 template <
class T,
class Dealloc>
1402 template <
class T,
class Dealloc>
1404 return std::less<T>()(s1.get(), s2.get());
1418 template <
class T,
class Dealloc>
1420 return (s1.
get() == s2.
get());
1434 template <
class T,
class Dealloc>
1452 template <
class T,
class Dealloc>
1454 return std::less<T>()(s1.
get(), s2.
get());
1457 #endif // CGU_USE_SMART_PTR_COMPARISON
T get() const
Definition: shared_handle.h:834
SharedHandle< gchar *, GFree > GcharSharedHandle
A handle comprising a typed instance of the SharedHandle class for gchar* arrays and strings.
Definition: shared_handle.h:421
void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:1280
Definition: application.h:45
SharedHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:611
This is an exception struct thrown as an alternative to deleting a managed object when internal memor...
Definition: shared_handle.h:484
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:204
void operator()(T obj)
Definition: shared_handle.h:190
T release()
Definition: shared_handle.h:827
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:223
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:337
ScopedHandle< gchar *, GFree > GcharScopedHandle
A handle comprising a typed instance of the ScopedHandle class for gchar* arrays and strings.
Definition: shared_handle.h:431
bool operator!=(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:618
void reset(T ptr=0)
Definition: shared_handle.h:655
~ScopedHandle()
Definition: shared_handle.h:847
unsigned int get_refcount() const
Definition: shared_handle.h:743
T get() const
Definition: shared_handle.h:1313
void operator()(T obj)
Definition: shared_handle.h:270
bool operator<(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:641
SharedLockHandle & operator=(SharedLockHandle sh_hand)
Definition: shared_handle.h:1303
SharedHandle(T ptr=0)
Definition: shared_handle.h:556
void operator()(const void *obj)
Definition: shared_handle.h:206
void swap(Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2)
Definition: async_queue.h:784
SharedHandleError(T p)
Definition: shared_handle.h:487
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:380
unsigned int get_refcount() const
Definition: shared_handle.h:1331
@ leave
Definition: shared_handle.h:500
This is a generic class for managing the lifetime of objects allocated on freestore,...
Definition: shared_handle.h:938
SharedHandle(const SharedHandle &sh_hand)
Definition: shared_handle.h:705
SharedHandle & operator=(SharedHandle sh_hand)
Definition: shared_handle.h:719
void operator()(T obj)
Definition: shared_handle.h:340
This is a generic class for managing the lifetime of objects allocated on freestore.
Definition: shared_handle.h:410
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:188
~SharedHandle()
Definition: shared_handle.h:749
int lock()
Definition: mutex.h:132
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:192
SharedLockHandle(const SharedLockHandle &sh_hand)
Definition: shared_handle.h:1289
void reset(T ptr=0)
Definition: shared_handle.h:814
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:268
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
virtual const char * what() const
Definition: shared_handle.h:486
~SharedLockHandle()
Definition: shared_handle.h:1345
bool operator==(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:602
void operator()(const void *obj)
Definition: shared_handle.h:225
This is a generic scoped class for managing the lifetime of objects allocated on freestore.
Definition: shared_handle.h:411
Leave
Definition: shared_handle.h:500
T obj
Definition: shared_handle.h:485
void operator()(const void *obj)
Definition: shared_handle.h:382
ScopedHandle(T ptr=0)
Definition: shared_handle.h:802
void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:696
SharedLockHandle(T ptr=0)
Definition: shared_handle.h:1039
A wrapper class for pthread mutexes.
Definition: mutex.h:109
T get() const
Definition: shared_handle.h:729
SharedLockHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:1135
void reset(T ptr=0)
Definition: shared_handle.h:1222