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 
  253     std::free(
const_cast<void*
>(obj));
 
  273     g_free(
const_cast<void*
>(obj));
 
  316     g_slice_free1(
sizeof(*obj), (
void*)obj);
 
  381   template <
class U> 
void destroy(U& obj) {obj.~U();}
 
  385     g_slice_free1(
sizeof(*obj), (
void*)obj);
 
  424     g_slice_free1(block_size, 
const_cast<void*
>(obj));
 
  527   virtual const char* 
what() 
const throw() {
return "SharedHandleError\n";}
 
  540 namespace SharedHandleAllocFail {
 
  544 template <
class T, 
class Dealloc = StandardArrayDelete<T>> 
class SharedHandle {
 
  548 #ifndef DOXYGEN_PARSING 
  550     unsigned int* ref_count_p;
 
  556     if (!ref_items.ref_count_p) 
return;
 
  557     --(*ref_items.ref_count_p);
 
  558     if (*ref_items.ref_count_p == 0) {
 
  559 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
  560       g_slice_free(
unsigned int, ref_items.ref_count_p);
 
  562       delete ref_items.ref_count_p;
 
  564       deleter(ref_items.obj);
 
  568   void reference() noexcept {
 
  569     if (!ref_items.ref_count_p) 
return;
 
  570     ++(*ref_items.ref_count_p);
 
  594     if ((ref_items.obj = ptr)) { 
 
  595 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
  596       ref_items.ref_count_p = g_slice_new(
unsigned int);
 
  597       *ref_items.ref_count_p = 1;
 
  600     ref_items.ref_count_p = 
new unsigned int(1);
 
  610     else ref_items.ref_count_p = 0;
 
  642     if ((ref_items.obj = ptr)) { 
 
  643 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
  644       ref_items.ref_count_p = g_slice_new(
unsigned int);
 
  645       *ref_items.ref_count_p = 1;
 
  648     ref_items.ref_count_p = 
new unsigned int(1);
 
  650       catch (std::bad_alloc&) { 
 
  655     else ref_items.ref_count_p = 0;
 
  731     ref_items = sh_hand.ref_items;
 
  740     ref_items = sh_hand.ref_items;
 
  741     sh_hand.ref_items.ref_count_p = 0;
 
  742     sh_hand.ref_items.obj = 0;
 
  765   T 
get() const noexcept {
return ref_items.obj;}
 
  772   operator T() const noexcept {
return ref_items.obj;}
 
  779   unsigned int get_refcount() const noexcept {
return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
 
  828 template <
class T, 
class Dealloc = StandardArrayDelete<T>> 
class ScopedHandle {
 
  864     reset(sc_hand.release());
 
  889     if (ptr) deleter(ptr); 
 
  898   T 
release() noexcept {T tmp = obj; obj = 0; 
return tmp;}
 
  905   T 
get() const noexcept {
return obj;}
 
  912   operator T() const noexcept {
return obj;}
 
 1063 #ifndef DOXYGEN_PARSING 
 1065 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1067     unsigned int* ref_count_p;
 
 1078   void unreference() {
 
 1083     if (!ref_items.ref_count_p) 
return;
 
 1084 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1085     ref_items.mutex_p->
lock();
 
 1086     --(*ref_items.ref_count_p);
 
 1087     if (*ref_items.ref_count_p == 0) {
 
 1088 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1089       g_slice_free(
unsigned int, ref_items.ref_count_p);
 
 1091       delete ref_items.ref_count_p;
 
 1093       ref_items.mutex_p->unlock();
 
 1094       delete ref_items.mutex_p;
 
 1095       deleter(ref_items.obj);
 
 1097     else ref_items.mutex_p->unlock();
 
 1099     if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
 
 1100 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1101       g_slice_free(gint, ref_items.ref_count_p);
 
 1103       delete ref_items.ref_count_p;
 
 1105       deleter(ref_items.obj);
 
 1112   void reference() noexcept {
 
 1117     if (!ref_items.ref_count_p) 
return;
 
 1118 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1120     ++(*ref_items.ref_count_p);
 
 1122     g_atomic_int_inc(ref_items.ref_count_p);
 
 1156     if ((ref_items.obj = ptr)) { 
 
 1157 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1167 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1168       ref_items.ref_count_p = g_slice_new(
unsigned int);
 
 1169       *ref_items.ref_count_p = 1;
 
 1172     ref_items.ref_count_p = 
new unsigned int(1);
 
 1175     delete ref_items.mutex_p;
 
 1181 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1182       ref_items.ref_count_p = g_slice_new(gint);
 
 1183       *ref_items.ref_count_p = 1;
 
 1186     ref_items.ref_count_p = 
new gint(1);
 
 1198 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1199       ref_items.mutex_p = 0;  
 
 1201       ref_items.ref_count_p = 0;
 
 1244     if ((ref_items.obj = ptr)) { 
 
 1245 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1249       catch (std::bad_alloc&) { 
 
 1255 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1256       ref_items.ref_count_p = g_slice_new(
unsigned int);
 
 1257       *ref_items.ref_count_p = 1;
 
 1260     ref_items.ref_count_p = 
new unsigned int(1);
 
 1262       catch (std::bad_alloc&) {
 
 1263     delete ref_items.mutex_p;
 
 1268 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT 
 1269       ref_items.ref_count_p = g_slice_new(gint);
 
 1270       *ref_items.ref_count_p = 1;
 
 1273     ref_items.ref_count_p = 
new gint(1);
 
 1275       catch (std::bad_alloc&) { 
 
 1282 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1283       ref_items.mutex_p = 0;  
 
 1285       ref_items.ref_count_p = 0;
 
 1393     ref_items = sh_hand.ref_items;
 
 1402     ref_items = sh_hand.ref_items;
 
 1403 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1404     sh_hand.ref_items.mutex_p = 0;  
 
 1406     sh_hand.ref_items.ref_count_p = 0;
 
 1407     sh_hand.ref_items.obj = 0;
 
 1420     std::swap(ref_items, sh_hand.ref_items);
 
 1429   T 
get() const noexcept {
return ref_items.obj;}
 
 1436   operator T() const noexcept {
return ref_items.obj;}
 
 1448     if (!ref_items.ref_count_p) 
return 0;
 
 1449 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX 
 1451     return *ref_items.ref_count_p;
 
 1453     return g_atomic_int_get(ref_items.ref_count_p);
 
 1464 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING) 
 1476 template <
class T, 
class Dealloc>
 
 1478   return (s1.get() == s2.get());
 
 1489 template <
class T, 
class Dealloc>
 
 1508 template <
class T, 
class Dealloc>
 
 1510   return std::less<T>()(s1.get(), s2.get());
 
 1521 template <
class T, 
class Dealloc>
 
 1523   return (s1.get() == s2.get());
 
 1534 template <
class T, 
class Dealloc>
 
 1548 template <
class T, 
class Dealloc>
 
 1550   return std::less<T>()(s1.
get(), s2.
get());
 
 1553 #endif // CGU_USE_SMART_PTR_COMPARISON 
 1560 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING) 
 1564 template <
class T, 
class Dealloc>
 
 1565 struct hash<
Cgu::SharedHandle<T, Dealloc>> {
 
 1566   typedef std::size_t result_type;
 
 1568   result_type operator()(
const argument_type& s)
 const {
 
 1575 template <
class T, 
class Dealloc>
 
 1576 struct hash<
Cgu::SharedLockHandle<T, Dealloc>> {
 
 1577   typedef std::size_t result_type;
 
 1579   result_type operator()(
const argument_type& s)
 const {
 
 1587 #endif // CGU_USE_SMART_PTR_COMPARISON 
  
SharedHandle< gchar *, GFree > GcharSharedHandle
A handle comprising a typed instance of the SharedHandle class for gchar* arrays and strings.
Definition: shared_handle.h:462
T get() const noexcept
Definition: shared_handle.h:1429
void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:1383
Definition: application.h:44
SharedHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:640
int lock() noexcept
Definition: mutex.h:147
This is an exception struct thrown as an alternative to deleting a managed object when internal memor...
Definition: shared_handle.h:525
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:250
T get() const noexcept
Definition: shared_handle.h:905
void operator()(T obj)
Definition: shared_handle.h:234
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:270
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:380
ScopedHandle< gchar *, GFree > GcharScopedHandle
A handle comprising a typed instance of the ScopedHandle class for gchar* arrays and strings.
Definition: shared_handle.h:472
void reset(T ptr=0)
Definition: shared_handle.h:682
~ScopedHandle()
Definition: shared_handle.h:918
unsigned int get_refcount() const noexcept
Definition: shared_handle.h:779
T release() noexcept
Definition: shared_handle.h:898
void operator()(T obj) noexcept
Definition: shared_handle.h:315
SharedHandle(const SharedHandle &sh_hand) noexcept
Definition: shared_handle.h:730
ScopedHandle(ScopedHandle &&sc_hand) noexcept
Definition: shared_handle.h:849
void operator()(const void *obj) noexcept
Definition: shared_handle.h:252
void operator()(const void *obj) noexcept
Definition: shared_handle.h:272
bool operator<(const GobjHandle< T > &h1, const GobjHandle< T > &h2)
Definition: gobj_handle.h:632
bool operator!=(const GobjHandle< T > &h1, const GobjHandle< T > &h2) noexcept
Definition: gobj_handle.h:613
SharedLockHandle & operator=(SharedLockHandle sh_hand)
Definition: shared_handle.h:1419
SharedLockHandle(SharedLockHandle &&sh_hand) noexcept
Definition: shared_handle.h:1401
SharedHandle(T ptr=0)
Definition: shared_handle.h:592
void swap(Cgu::AsyncQueue< T, Container > &q1, Cgu::AsyncQueue< T, Container > &q2)
Definition: async_queue.h:1493
SharedHandleError(T p)
Definition: shared_handle.h:528
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:421
@ leave
Definition: shared_handle.h:541
This is a generic class for managing the lifetime of objects allocated on freestore,...
Definition: shared_handle.h:1059
void operator()(const void *obj) noexcept
Definition: shared_handle.h:423
SharedHandle & operator=(SharedHandle sh_hand)
Definition: shared_handle.h:755
void operator()(T obj)
Definition: shared_handle.h:383
This is a generic class for managing the lifetime of objects allocated on freestore.
Definition: shared_handle.h:451
ScopedHandle(const ScopedHandle &)=delete
ScopedHandle(T ptr=0) noexcept
Definition: shared_handle.h:877
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:232
unsigned int get_refcount() const noexcept
Definition: shared_handle.h:1447
bool operator==(const GobjHandle< T > &h1, const GobjHandle< T > &h2) noexcept
Definition: gobj_handle.h:600
ScopedHandle & operator=(const ScopedHandle &)=delete
~SharedHandle()
Definition: shared_handle.h:785
A scoped locking class for exception safe Mutex locking.
Definition: mutex.h:207
SharedHandle(SharedHandle &&sh_hand) noexcept
Definition: shared_handle.h:739
void reset(T ptr=0)
Definition: shared_handle.h:887
A specialization of std::hash for Cgu::Callback::FunctorArg, Cgu::Callback::SafeFunctorArg,...
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle,...
Definition: shared_handle.h:313
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
ScopedHandle & operator=(ScopedHandle &&sc_hand)
Definition: shared_handle.h:863
virtual const char * what() const
Definition: shared_handle.h:527
~SharedLockHandle()
Definition: shared_handle.h:1461
This is a generic scoped class for managing the lifetime of objects allocated on freestore.
Definition: shared_handle.h:452
Leave
Definition: shared_handle.h:541
T obj
Definition: shared_handle.h:526
T get() const noexcept
Definition: shared_handle.h:765
SharedLockHandle(const SharedLockHandle &sh_hand) noexcept
Definition: shared_handle.h:1392
void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:721
SharedLockHandle(T ptr=0)
Definition: shared_handle.h:1154
A wrapper class for pthread mutexes.
Definition: mutex.h:117
SharedLockHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag)
Definition: shared_handle.h:1242
void reset(T ptr=0)
Definition: shared_handle.h:1327