libzypp  17.7.0
ReferenceCounted.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_REFERENCECOUNTED_H
13 #define ZYPP_BASE_REFERENCECOUNTED_H
14 
15 #include <iosfwd>
16 
17 #include "zypp/base/PtrTypes.h"
18 
20 namespace zypp
21 {
22  namespace base
24  {
25 
27  //
28  // CLASS NAME : ReferenceCounted
29  //
34  {
36  friend std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj );
37 
38  public:
43 
47  ReferenceCounted( const ReferenceCounted & rhs );
48 
52  virtual ~ReferenceCounted();
53 
58  { return *this; }
59 
60  public:
62  unsigned refCount() const
63  { return _counter; }
64 
66  void ref() const
67  { ref_to( ++_counter ); }
68 
73  void unref() const
74  {
75  if ( !_counter )
76  unrefException(); // will throw!
77  if ( --_counter )
78  unref_to( _counter );
79  else
80  delete this;
81  }
82 
86  static void add_ref( const ReferenceCounted * ptr_r )
87  { if( ptr_r ) ptr_r->ref(); }
88 
92  static void release( const ReferenceCounted * ptr_r )
93  { if( ptr_r ) ptr_r->unref(); }
94 
95  protected:
97  virtual std::ostream & dumpOn( std::ostream & str ) const;
98 
100  virtual void ref_to( unsigned /* rep_cnt_r */ ) const {}
101 
106  virtual void unref_to( unsigned /* rep_cnt_r */ ) const {}
107 
108  private:
110  mutable unsigned _counter;
111 
113  void unrefException() const;
114  };
116 
118  inline void intrusive_ptr_add_ref( const ReferenceCounted * ptr_r )
119  { ReferenceCounted::add_ref( ptr_r ); }
120 
122  inline void intrusive_ptr_release( const ReferenceCounted * ptr_r )
123  { ReferenceCounted::release( ptr_r ); }
124 
126  inline std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj )
127  { return obj.dumpOn( str ); }
128 
130  } // namespace base
133 } // namespace zypp
135 
136 #define IMPL_PTR_TYPE(NAME) \
137 void intrusive_ptr_add_ref( const NAME * ptr_r ) \
138 { zypp::base::ReferenceCounted::add_ref( ptr_r ); } \
139 void intrusive_ptr_release( const NAME * ptr_r ) \
140 { zypp::base::ReferenceCounted::release( ptr_r ); }
141 
143 #endif // ZYPP_BASE_REFERENCECOUNTED_H
void unrefException() const
Throws Exception on unref.
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
String related utilities and Regular expression matching.
unsigned refCount() const
Return reference counter value.
virtual void ref_to(unsigned) const
Trigger derived classes after refCount was increased.
void intrusive_ptr_add_ref(const ReferenceCounted *ptr_r)
static void release(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
void intrusive_ptr_release(const ReferenceCounted *ptr_r)
unsigned _counter
The reference counter.
Base class for reference counted objects.
ReferenceCounted & operator=(const ReferenceCounted &)
Assignment.
void unref() const
Release a reference.
virtual void unref_to(unsigned) const
Trigger derived classes after refCount was decreased.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static void add_ref(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
friend std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Stream output via dumpOn.
void ref() const
Add a reference.
std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)