libzypp: zypp-core/base/ReferenceCounted.h Source File

libzypp 17.38.11

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/Globals.h>
18#include <zypp-core/base/PtrTypes.h>
19
21namespace zypp
22{
24 namespace base
25 {
26
28 //
29 // CLASS NAME : ReferenceCounted
30 //
34 class ZYPP_API ReferenceCounted
35 {
37 friend std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj );
38
39 public:
43 ReferenceCounted();
44
48 ReferenceCounted( const ReferenceCounted & rhs );
49
53 virtual ~ReferenceCounted();
54
58 ReferenceCounted & operator=( const ReferenceCounted & )
59 { return *this; }
60
61 public:
63 unsigned refCount() const
64 { return _counter; }
65
67 void ref() const
68 { ref_to( ++_counter ); }
69
74 void unref() const
75 {
76 if ( !_counter )
77 unrefException(); // will throw!
78 if ( --_counter )
79 unref_to( _counter );
80 else
81 delete this;
82 }
83
87 static void add_ref( const ReferenceCounted * ptr_r )
88 { if( ptr_r ) ptr_r->ref(); }
89
93 static void release( const ReferenceCounted * ptr_r )
94 { if( ptr_r ) ptr_r->unref(); }
95
96 protected:
98 virtual std::ostream & dumpOn( std::ostream & str ) const;
99
101 virtual void ref_to( unsigned /* rep_cnt_r */ ) const {}
102
107 virtual void unref_to( unsigned /* rep_cnt_r */ ) const {}
108
109 private:
111 mutable unsigned _counter;
112
114 void unrefException() const;
115 };
116
117
119 inline void intrusive_ptr_add_ref( const ReferenceCounted * ptr_r )
120 { ReferenceCounted::add_ref( ptr_r ); }
121
123 inline void intrusive_ptr_release( const ReferenceCounted * ptr_r )
124 { ReferenceCounted::release( ptr_r ); }
125
127 inline std::ostream & operator<<( std::ostream & str, const ReferenceCounted & obj )
128 { return obj.dumpOn( str ); }
129
131 } // namespace base
134} // namespace zypp
136
137#define IMPL_PTR_TYPE(NAME) \
138void intrusive_ptr_add_ref( const NAME * ptr_r ) \
139{ zypp::base::ReferenceCounted::add_ref( ptr_r ); } \
140void intrusive_ptr_release( const NAME * ptr_r ) \
141{ zypp::base::ReferenceCounted::release( ptr_r ); }
142
144#endif // ZYPP_CORE_BASE_REFERENCECOUNTED_H
Globals.h
Provides API related macros.
operator<<
std::ostream & operator<<(std::ostream &str, const zypp::sat::detail::CDataiterator *obj)
relates: zypp::sat::LookupAttr::iterator Stream output of the underlying iterator for debug.
Definition LookupAttr.cc:808
PtrTypes.h
zypp::base::ReferenceCounted
Base class for reference counted objects.
Definition ReferenceCounted.h:35
zypp::base::ReferenceCounted::ref
void ref() const
Add a reference.
Definition ReferenceCounted.h:67
zypp::base::ReferenceCounted::unref_to
virtual void unref_to(unsigned) const
Trigger derived classes after refCount was decreased.
Definition ReferenceCounted.h:107
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::unref
void unref() const
Release a reference.
Definition ReferenceCounted.h:74
zypp::base::ReferenceCounted::_counter
unsigned _counter
The reference counter.
Definition ReferenceCounted.h:111
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:87
zypp::base::ReferenceCounted::refCount
unsigned refCount() const
Return reference counter value.
Definition ReferenceCounted.h:63
zypp::base::ReferenceCounted::ref_to
virtual void ref_to(unsigned) const
Trigger derived classes after refCount was increased.
Definition ReferenceCounted.h:101
zypp::base::ReferenceCounted::operator=
ReferenceCounted & operator=(const ReferenceCounted &)
Assignment.
Definition ReferenceCounted.h:58
zypp::base::ReferenceCounted::ReferenceCounted
ReferenceCounted()
Default ctor.
Definition ReferenceCounted.cc:25
zypp::base::ReferenceCounted::release
static void release(const ReferenceCounted *ptr_r)
Called by zypp::intrusive_ptr to add a reference.
Definition ReferenceCounted.h:93
str
String related utilities and Regular expression matching.
zypp::base
Definition Random.cc:7
zypp::base::operator<<
std::ostream & operator<<(std::ostream &str, const SetTracker< TSet > &obj)
relates: SetTracker Stream output
Definition SetTracker.h:191
zypp::base::intrusive_ptr_add_ref
void intrusive_ptr_add_ref(const ReferenceCounted *ptr_r)
relates: ReferenceCounted intrusive_ptr hook to add_ref.
Definition ReferenceCounted.h:119
zypp::base::intrusive_ptr_release
void intrusive_ptr_release(const ReferenceCounted *ptr_r)
relates: ReferenceCounted intrusive_ptr hook to release.
Definition ReferenceCounted.h:123
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition Capability.cc:589