libzypp: zypp/base/ReferenceCounted.h Source File

libzypp 17.25.10

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 {
23 namespace base
24 {
25
27 //
28 // CLASS NAME : ReferenceCounted
29 //
33 class ReferenceCounted
34 {
36 friend std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj );
37
38 public:
42 ReferenceCounted();
43
47 ReferenceCounted( const ReferenceCounted & rhs );
48
52 virtual ~ReferenceCounted();
53
57 ReferenceCounted & operator=( const ReferenceCounted & )
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
PtrTypes.h
zypp::base::ReferenceCounted
Base class for reference counted objects.
Definition: ReferenceCounted.h:34
zypp::base::ReferenceCounted::~ReferenceCounted
virtual ~ReferenceCounted()
Dtor.
Definition: ReferenceCounted.cc:33
zypp::base::ReferenceCounted::ref
void ref() const
Add a reference.
Definition: ReferenceCounted.h:66
zypp::base::ReferenceCounted::unref_to
virtual void unref_to(unsigned) const
Trigger derived classes after refCount was decreased.
Definition: ReferenceCounted.h:106
zypp::base::ReferenceCounted::dumpOn
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
Definition: ReferenceCounted.cc:47
zypp::base::ReferenceCounted::unrefException
void unrefException() const
Throws Exception on unref.
Definition: ReferenceCounted.cc:41
zypp::base::ReferenceCounted::operator<<
std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Stream output.
Definition: ReferenceCounted.h:126
zypp::base::ReferenceCounted::unref
void unref() const
Release a reference.
Definition: ReferenceCounted.h:73
zypp::base::ReferenceCounted::_counter
unsigned _counter
The reference counter.
Definition: ReferenceCounted.h:110
zypp::base::ReferenceCounted::operator=
ReferenceCounted & operator=(const ReferenceCounted &)
Assignment.
Definition: ReferenceCounted.h:57
zypp::base::ReferenceCounted::add_ref
static void add_ref(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
Definition: ReferenceCounted.h:86
zypp::base::ReferenceCounted::refCount
unsigned refCount() const
Return reference counter value.
Definition: ReferenceCounted.h:62
zypp::base::ReferenceCounted::ref_to
virtual void ref_to(unsigned) const
Trigger derived classes after refCount was increased.
Definition: ReferenceCounted.h:100
zypp::base::ReferenceCounted::operator<<
friend std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Stream output via dumpOn.
zypp::base::ReferenceCounted::ReferenceCounted
ReferenceCounted()
Default ctor.
Definition: ReferenceCounted.cc:25
zypp::base::ReferenceCounted::intrusive_ptr_add_ref
void intrusive_ptr_add_ref(const ReferenceCounted *ptr_r)
intrusive_ptr hook to add_ref.
Definition: ReferenceCounted.h:118
zypp::base::ReferenceCounted::release
static void release(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
Definition: ReferenceCounted.h:92
zypp::base::ReferenceCounted::intrusive_ptr_release
void intrusive_ptr_release(const ReferenceCounted *ptr_r)
intrusive_ptr hook to release.
Definition: ReferenceCounted.h:122
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2