KaRL  3.0.0
ThreadSafeRefcounter.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_UTILITY_THREADSAFE_REFCOUNTER_H_
3 #define _MADARA_UTILITY_THREADSAFE_REFCOUNTER_H_
4 
5 #include "ace/Guard_T.h"
6 #include "ace/Recursive_Thread_Mutex.h"
7 #include "madara/LockType.h"
8 
9 namespace madara
10 {
11  namespace utility
12  {
24  template <typename T>
26  {
27  public:
29  ThreadSafeRefcounter (void);
30 
32  ThreadSafeRefcounter (T * ptr,
33  bool increase_count = false, bool manage = true);
34 
37 
39  virtual ~ThreadSafeRefcounter (void);
40 
43  void operator= (T * ptr);
44 
46  void operator= (const ThreadSafeRefcounter & rhs);
47 
49  inline T & operator* (void);
50 
52  inline const T & operator* (void) const;
53 
55  inline T * operator-> (void);
56 
58  inline const T * operator-> (void) const;
59 
61  T * get_ptr (void);
62 
64  const T * get_ptr (void) const;
65 
67  T * get (void);
68 
70  const T * get (void) const;
71 
73  bool is_valid (void) const;
74 
75  private:
77  inline void increment (void);
78 
80  inline void decrement (void);
81 
82 
83 
86  struct Shim
87  {
89  Shim (T * t, bool manage = true);
90 
92  ~Shim (void);
93 
95  T * t_;
96 
98  mutable MADARA_LOCK_TYPE mutex_;
99 
101  volatile int refcount_;
102 
104  bool manage_;
105  };
106 
108  Shim * ptr_;
109  };
110  }
111 }
113 
114 #endif /* _MADARA_UTILITY_THREADSAFE_REFCOUNTER_H_ */
This template class provides transparent reference counting of its template parameter T...
T * get_ptr(void)
get the underlying pointer
void operator=(T *ptr)
assignment operator for times when you don't want the reference increased for incoming ptr ...
T * operator->(void)
mimic pointer dereferencing
volatile int refcount_
Current value of the reference count.
void increment(void)
implementation of the increment operation
virtual ~ThreadSafeRefcounter(void)
Dtor will delete pointer if refcount becomes 0.
A shim class that keeps track of the reference count and a pointer to the type T that's reference cou...
T * t_
Pointer to the object that's being reference counted.
Shim(T *t, bool manage=true)
Constructor.
T & operator*(void)
dereference operator
bool is_valid(void) const
checks to see if the underlying pointer is valid
MADARA_LOCK_TYPE mutex_
mutex for updating refcount_
void decrement(void)
implementation of the decrement operation