libcamgm
ReferenceCounted.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef CA_MGM_REFERENCECOUNTED_H
13 #define CA_MGM_REFERENCECOUNTED_H
14 
15 #include <iosfwd>
16 
17 #include <ca-mgm/Logger.hpp>
18 #include <ca-mgm/PtrTypes.hpp>
19 
21 namespace ca_mgm
22 {
23 
25  //
26  // CLASS NAME : ReferenceCounted
27  //
32  {
34  friend std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj );
35 
36  public:
41 
45  ReferenceCounted( const ReferenceCounted & rhs );
46 
50  virtual ~ReferenceCounted();
51 
56  { return *this; }
57 
58  public:
60  unsigned refCount() const
61  { return _counter; }
62 
64  void ref() const
65  {
66  ref_to( ++_counter );
67  //DBG << "refcounter increment to: " << _counter << std::endl;
68  }
69 
74  void unref() const
75  {
76  if ( !_counter )
77  unrefException(); // will throw!
78  if ( --_counter )
79  {
80  unref_to( _counter );
81  //DBG << "refcounter decrement to: " << _counter << std::endl;
82  }
83  else
84  {
85  //DBG << "refcounter 0 ; delete this" << std::endl;
86  delete this;
87  }
88  }
89 
93  static void add_ref( const ReferenceCounted * ptr_r )
94  { if( ptr_r ) ptr_r->ref(); }
95 
99  static void release( const ReferenceCounted * ptr_r )
100  { if( ptr_r ) ptr_r->unref(); }
101 
102  protected:
104  virtual std::ostream & dumpOn( std::ostream & str ) const;
105 
107  virtual void ref_to( unsigned /* rep_cnt_r */ ) const {}
108 
113  virtual void unref_to( unsigned /* rep_cnt_r */ ) const {}
114 
115  private:
117  mutable unsigned _counter;
118 
120  void unrefException() const;
121  };
123 
125  inline void intrusive_ptr_add_ref( const ReferenceCounted * ptr_r )
126  { ReferenceCounted::add_ref( ptr_r ); }
127 
129  inline void intrusive_ptr_release( const ReferenceCounted * ptr_r )
130  { ReferenceCounted::release( ptr_r ); }
131 
133  inline std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj )
134  { return obj.dumpOn( str ); }
135 
137 } // namespace ca_mgm
139 
140 #define IMPL_PTR_TYPE(NAME) \
141 void intrusive_ptr_add_ref( const NAME * ptr_r ) \
142 { ca_mgm::ReferenceCounted::add_ref( ptr_r ); } \
143 void intrusive_ptr_release( const NAME * ptr_r ) \
144 { ca_mgm::ReferenceCounted::release( ptr_r ); }
145 
147 #endif // CA_MGM_REFERENCECOUNTED_H
void unrefException() const
friend std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
unsigned refCount() const
Definition: ReferenceCounted.hpp:60
virtual void ref_to(unsigned) const
Definition: ReferenceCounted.hpp:107
Definition: ReferenceCounted.hpp:31
unsigned _counter
Definition: ReferenceCounted.hpp:117
void intrusive_ptr_add_ref(const ReferenceCounted *ptr_r)
Definition: ReferenceCounted.hpp:125
ReferenceCounted & operator=(const ReferenceCounted &)
Definition: ReferenceCounted.hpp:55
void ref() const
Definition: ReferenceCounted.hpp:64
void unref() const
Definition: ReferenceCounted.hpp:74
virtual std::ostream & dumpOn(std::ostream &str) const
void intrusive_ptr_release(const ReferenceCounted *ptr_r)
Definition: ReferenceCounted.hpp:129
static void release(const ReferenceCounted *ptr_r)
Definition: ReferenceCounted.hpp:99
std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Definition: ReferenceCounted.hpp:133
virtual void unref_to(unsigned) const
Definition: ReferenceCounted.hpp:113
static void add_ref(const ReferenceCounted *ptr_r)
Definition: ReferenceCounted.hpp:93