libzypp 17.31.23
ReferenceCounted.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_CORE_BASE_REFERENCECOUNTED_H
13#define ZYPP_CORE_BASE_REFERENCECOUNTED_H
14
15#include <iosfwd>
16
17#include <zypp-core/base/PtrTypes.h>
18
20namespace 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:
43
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 )
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) \
137void intrusive_ptr_add_ref( const NAME * ptr_r ) \
138{ zypp::base::ReferenceCounted::add_ref( ptr_r ); } \
139void intrusive_ptr_release( const NAME * ptr_r ) \
140{ zypp::base::ReferenceCounted::release( ptr_r ); }
141
143#endif // ZYPP_CORE_BASE_REFERENCECOUNTED_H
void ref() const
Add a reference.
virtual void unref_to(unsigned) const
Trigger derived classes after refCount was decreased.
virtual std::ostream & dumpOn(std::ostream &str) const
Overload to realize std::ostream & operator<<.
void unrefException() const
Throws Exception on unref.
friend std::ostream & operator<<(std::ostream &str, const ReferenceCounted &obj)
Stream output via dumpOn.
void unref() const
Release a reference.
unsigned _counter
The reference counter.
static void add_ref(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
unsigned refCount() const
Return reference counter value.
virtual void ref_to(unsigned) const
Trigger derived classes after refCount was increased.
ReferenceCounted & operator=(const ReferenceCounted &)
Assignment.
static void release(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const Flags< TEnum > &obj)
Definition: Flags.h:166
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2