PandA-2024.02
refcount.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
45 #ifndef REFCOUNT_HPP
46 #define REFCOUNT_HPP
47 
48 #include <boost/version.hpp>
49 
52 {
54  void operator()(void const*) const
55  {
56  }
57 };
58 
59 #if !defined(USE_REFCOUNT_POINTERS) && BOOST_VERSION >= 103300
60 #if __cplusplus > 199711L
61 #include <memory>
62 #define refcount std::shared_ptr
63 #define Wrefcount std::weak_ptr
64 #define RefcountCast std::dynamic_pointer_cast
65 
66 #else
67 #include <boost/smart_ptr/shared_ptr.hpp>
68 #include <boost/smart_ptr/weak_ptr.hpp>
69 #include <cstddef>
70 
71 #define refcount boost::shared_ptr
72 #define Wrefcount boost::weak_ptr
73 #define RefcountCast boost::dynamic_pointer_cast
74 #endif
75 #else
76 template <class T>
77 class Wrefcount;
78 
93 template <typename T>
94 struct refcount
95 {
96  private:
97  friend class Wrefcount<T>;
98  struct Ref
99  {
100  T* const ptr;
101  unsigned int count : 31;
102  unsigned int deleter : 1;
103  // cppcheck-suppress noExplicitConstructor
104  Ref(T* p) : ptr(p), count(1), deleter(1)
105  {
106  }
107  template <class null_deleter>
108  Ref(T* p, null_deleter& d) : ptr(p), count(1), deleter(0)
109  {
110  }
112  {
113  if(deleter)
114  delete ptr;
115  }
117  {
118  ++count;
119  return this;
120  }
121  bool decrement()
122  {
123  return (--count == 0);
124  }
125  } * ref;
126 
127  public:
128  explicit refcount(T* p = 0) : ref(p ? new Ref(p) : 0)
129  {
130  }
131  template <class null_deleter>
132  explicit refcount(T* p, null_deleter& d) : ref(p ? new Ref(p, d) : 0)
133  {
134  }
135  // cppcheck-suppress noExplicitConstructor
136  refcount(const refcount<T>& other) : ref(other.ref ? other.ref->increment() : 0)
137  {
138  }
140  {
141  if(ref && ref->decrement())
142  delete ref;
143  }
144  // cppcheck-suppress operatorEqToSelf
146  {
147  Ref* tmp = other.ref ? other.ref->increment() : 0;
148  if(ref && ref->decrement())
149  delete ref;
150  ref = tmp;
151  return *this;
152  }
153  bool operator==(const refcount<T>& other)
154  {
155  return this->get() == other.get();
156  }
157  operator T*() const
158  {
159  return ref ? ref->ptr : 0;
160  }
161  operator Wrefcount<T>() const
162  {
163  return Wrefcount<T>(get());
164  }
165  T* operator->() const
166  {
167  return ref ? ref->ptr : 0;
168  }
169  T* get() const
170  {
171  return ref ? ref->ptr : 0;
172  }
173  template <class newType>
174  operator refcount<newType>()
175  {
176  return refcount<newType>(ref);
177  }
178 };
179 
180 template <typename T>
181 // cppcheck-suppress copyCtorAndEqOperator
182 struct Wrefcount
183 {
184  private:
185  T* ptr;
186 
187  public:
188  Wrefcount() : ptr(0)
189  {
190  }
191  // cppcheck-suppress noExplicitConstructor
192  Wrefcount(Wrefcount<T> const& other) : ptr(other.ptr)
193  {
194  }
195  // cppcheck-suppress noExplicitConstructor
196  Wrefcount(refcount<T> const& other) : ptr(other.get())
197  {
198  }
200  {
201  }
203  {
204  ptr = other.ptr;
205  return *this;
206  }
208  {
209  ptr = other.get();
210  return *this;
211  }
213  {
214  null_deleter nullDel;
215  return refcount<T>(ptr, nullDel);
216  }
217 };
218 
219 #endif
220 
223 #define REF_FORWARD_DECL(obj) \
224  class obj; \
225  typedef refcount<obj> obj##Ref
226 #define CONSTREF_FORWARD_DECL(obj) \
227  class obj; \
228  typedef refcount<const obj> obj##ConstRef
229 
236 template <class T, class U>
237 inline T* GetPointer(const refcount<U>& t)
238 {
239  return dynamic_cast<T*>(t.get());
240 }
241 
248 template <class T, class U>
249 inline T* GetPointerS(const refcount<U>& t)
250 {
251  return static_cast<T*>(t.get());
252 }
253 
254 #include <functional>
255 #if !defined(USE_REFCOUNT_POINTERS) && BOOST_VERSION >= 103300 && __cplusplus > 199711L
256 #else
257 namespace std
258 {
259  template <typename T>
260  struct hash<refcount<T>> : public std::unary_function<refcount<T>, std::size_t>
261  {
263  {
264  std::hash<const void*> hasher;
265  return hasher(val.get());
266  }
267  };
268 } // namespace std
269 #endif
270 
274 template <typename T>
275 class RefCountOrder : std::binary_function<refcount<T>, refcount<T>, bool>
276 {
277  public:
281  RefCountOrder<T>() = default;
282 
286  bool operator()(const refcount<T> x, const refcount<T> y) const
287  {
288  return *(x.get()) < *(y.get());
289  }
290 };
291 #endif
refcount< T > & operator=(const refcount< T > &other)
Definition: refcount.hpp:145
T * operator->() const
Definition: refcount.hpp:165
bool operator==(const refcount< T > &other)
Definition: refcount.hpp:153
Wrefcount< T > & operator=(refcount< T > const &other)
Definition: refcount.hpp:207
refcount(T *p=0)
Definition: refcount.hpp:128
bool operator()(const refcount< T > x, const refcount< T > y) const
Compare of refcount object.
Definition: refcount.hpp:286
Definition of hash function for EdgeDescriptor.
Definition: graph.hpp:1321
bool decrement()
Definition: refcount.hpp:121
The key comparison function for refcount.
Definition: refcount.hpp:275
void operator()(void const *) const
deallocator
Definition: refcount.hpp:54
unsigned int count
Definition: refcount.hpp:101
Ref * increment()
Definition: refcount.hpp:116
refcount(T *p, null_deleter &d)
Definition: refcount.hpp:132
null deleter
Definition: refcount.hpp:51
refcount< T > lock() const
Definition: refcount.hpp:212
unsigned int deleter
Definition: refcount.hpp:102
T * GetPointer(const refcount< U > &t)
Template function used to hide dynamic_cast The template parameter T represents a type of an object h...
Definition: refcount.hpp:237
unsigned int size_t
Definition: test.c:1
T * GetPointerS(const refcount< U > &t)
Template function used to hide static_cast The template parameter T represents a type of an object ha...
Definition: refcount.hpp:249
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
std::size_t operator()(const refcount< T > &val) const
Definition: refcount.hpp:262
T * get() const
Definition: refcount.hpp:169
Wrefcount(refcount< T > const &other)
Definition: refcount.hpp:196
refcount(const refcount< T > &other)
Definition: refcount.hpp:136
x
Return the smallest n such that 2^n >= _x.
Wrefcount< T > & operator=(Wrefcount< T > const &other)
Definition: refcount.hpp:202
Wrefcount(Wrefcount< T > const &other)
Definition: refcount.hpp:192
T *const ptr
Definition: refcount.hpp:100
struct refcount::Ref * ref
Ref(T *p, null_deleter &d)
Definition: refcount.hpp:108

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13