MADARA  3.4.1
Refcounter.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MADARA_UTILITY_REFCOUNTER_H_
3 #define _MADARA_UTILITY_REFCOUNTER_H_
4 
5 namespace madara
6 {
7 namespace utility
8 {
20 template<typename T>
22 {
23 public:
25  Refcounter(void);
26 
28  Refcounter(T* ptr, bool increase_count = false);
29 
31  Refcounter(const Refcounter& rhs);
32 
34  virtual ~Refcounter(void);
35 
38  void operator=(T* ptr);
39 
41  void operator=(const Refcounter& rhs);
42 
44  inline T& operator*(void);
45 
47  inline const T& operator*(void)const;
48 
50  inline T* operator->(void);
51 
53  inline const T* operator->(void)const;
54 
56  T* get_ptr(void);
57 
59  const T* get_ptr(void) const;
60 
62  T* get(void);
63 
65  const T* get(void) const;
66 
67 private:
69  inline void increment(void);
70 
72  inline void decrement(void);
73 
76  struct Shim
77  {
79  Shim(T* t);
80 
82  ~Shim(void);
83 
85  T* t_;
86 
88  int refcount_;
89  };
90 
93 };
94 }
95 }
97 
98 #endif /* _MADARA_UTILITY_REFCOUNTER_H_ */
This template class provides transparent reference counting of its template parameter T.
Definition: Refcounter.h:22
T * get(void)
get the underlying pointer
Definition: Refcounter.cpp:74
virtual ~Refcounter(void)
Dtor will delete pointer if refcount becomes 0.
Definition: Refcounter.cpp:32
T & operator*(void)
dereference operator
Definition: Refcounter.cpp:88
T * get_ptr(void)
get the underlying pointer
Definition: Refcounter.cpp:60
void decrement(void)
implementation of the decrement operation
Definition: Refcounter.cpp:124
void operator=(T *ptr)
assignment operator for times when you don't want the reference increased for incoming ptr
Definition: Refcounter.cpp:40
void increment(void)
implementation of the increment operation
Definition: Refcounter.cpp:116
Shim * ptr_
Pointer to the Shim.
Definition: Refcounter.h:92
T * operator->(void)
mimic pointer dereferencing
Definition: Refcounter.cpp:102
Refcounter(void)
default Ctor
Definition: Refcounter.cpp:9
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
Copyright(c) 2020 Galois.
A shim class that keeps track of the reference count and a pointer to the type T that's reference cou...
Definition: Refcounter.h:77
T * t_
Pointer to the object that's being reference counted.
Definition: Refcounter.h:85
int refcount_
Current value of the reference count.
Definition: Refcounter.h:88