Carla Backend
CarlaPluginPtr.hpp
Go to the documentation of this file.
1 /*
2  * Carla Plugin Host
3  * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16  */
17 
18 #ifndef CARLA_CPP_COMPAT_HPP_INCLUDED
19 #define CARLA_CPP_COMPAT_HPP_INCLUDED
20 
21 #include "CarlaDefines.h"
22 
23 #ifdef CARLA_PROPER_CPP11_SUPPORT
24 # include <memory>
25 #else
26 # include <algorithm>
27 # include "CarlaUtils.hpp"
28 #endif
29 
30 // -----------------------------------------------------------------------
31 
32 #ifndef CARLA_PROPER_CPP11_SUPPORT
33 namespace std {
34 
35 /* This code is part of shared_ptr.hpp:
36  * minimal implementation of smart pointer, a subset of the C++11 std::shared_ptr or boost::shared_ptr.
37  * Copyright (c) 2013-2019 Sebastien Rombauts (sebastien.rombauts@gmail.com)
38  * Distributed under the MIT License (MIT)
39  */
41 {
42 public:
43  shared_ptr_count() : pn(nullptr) {}
44  shared_ptr_count(const shared_ptr_count& count) : pn(count.pn) {}
45  void swap(shared_ptr_count& lhs) noexcept { std::swap(pn, lhs.pn); }
46 
47  long use_count(void) const noexcept
48  {
49  long count = 0;
50  if (nullptr != pn)
51  {
52  count = *pn;
53  }
54  return count;
55  }
56 
57  template<class U>
58  void acquire(U* p)
59  {
60  if (nullptr != p)
61  {
62  if (nullptr == pn)
63  {
64  try
65  {
66  pn = new volatile long(1);
67  }
68  catch (std::bad_alloc&)
69  {
70  delete p;
71  throw;
72  }
73  }
74  else
75  {
76  ++(*pn);
77  }
78  }
79  }
80 
81  template<class U>
82  void release(U* p) noexcept
83  {
84  if (nullptr != pn)
85  {
86  --(*pn);
87  if (0 == *pn)
88  {
89  delete p;
90  delete pn;
91  }
92  pn = nullptr;
93  }
94  }
95 
96 public:
97  volatile long* pn;
98 };
99 
100 template<class T>
102 {
103 public:
104  typedef T element_type;
105 
106  shared_ptr(void) noexcept
107  : px(nullptr),
108  pn() {}
109 
110  /*explicit*/ shared_ptr(T* p)
111  : px(nullptr),
112  pn()
113  {
114  acquire(p);
115  }
116 
117  template <class U>
118  shared_ptr(const shared_ptr<U>& ptr, T* p) :
119  px(nullptr),
120  pn(ptr.pn)
121  {
122  acquire(p);
123  }
124 
125  template <class U>
126  shared_ptr(const shared_ptr<U>& ptr) noexcept :
127  px(nullptr),
128  pn(ptr.pn)
129  {
130  CARLA_SAFE_ASSERT_RETURN(nullptr == ptr.px || 0 != ptr.pn.use_count(),);
131  acquire(static_cast<typename shared_ptr<T>::element_type*>(ptr.px));
132  }
133 
134  shared_ptr(const shared_ptr& ptr) noexcept :
135  px(nullptr),
136  pn(ptr.pn)
137  {
138  CARLA_SAFE_ASSERT_RETURN(nullptr == ptr.px || 0 != ptr.pn.use_count(),);
139  acquire(ptr.px);
140  }
141 
143  {
144  swap(ptr);
145  return *this;
146  }
147 
148  ~shared_ptr(void) noexcept
149  {
150  release();
151  }
152 
153  void reset(void) noexcept
154  {
155  release();
156  }
157 
158  void swap(shared_ptr& lhs) noexcept
159  {
160  std::swap(px, lhs.px);
161  pn.swap(lhs.pn);
162  }
163 
164  operator bool() const noexcept
165  {
166  return (0 < pn.use_count());
167  }
168  long use_count(void) const noexcept
169  {
170  return pn.use_count();
171  }
172 
173  // underlying pointer operations :
174  T& operator*() const noexcept
175  {
176  return *px;
177  }
178  T* operator->() const noexcept
179  {
180  return px;
181  }
182  T* get(void) const noexcept
183  {
184  return px;
185  }
186 
187 private:
188  void acquire(T* p)
189  {
190  pn.acquire(p);
191  px = p;
192  }
193 
194  void release(void) noexcept
195  {
196  pn.release(px);
197  px = nullptr;
198  }
199 
200 private:
201  template<class U>
202  friend class shared_ptr;
203 
204 private:
205  T* px;
206  shared_ptr_count pn;
207 };
208 
209 template<class T, class U> bool operator==(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
210 {
211  return (l.get() == r.get());
212 }
213 template<class T, class U> bool operator!=(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
214 {
215  return (l.get() != r.get());
216 }
217 template<class T, class U> bool operator<=(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
218 {
219  return (l.get() <= r.get());
220 }
221 template<class T, class U> bool operator<(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
222 {
223  return (l.get() < r.get());
224 }
225 template<class T, class U> bool operator>=(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
226 {
227  return (l.get() >= r.get());
228 }
229 template<class T, class U> bool operator>(const shared_ptr<T>& l, const shared_ptr<U>& r) noexcept
230 {
231  return (l.get() > r.get());
232 }
233 template<class T, class U>
235 {
236  return shared_ptr<T>(ptr, static_cast<typename shared_ptr<T>::element_type*>(ptr.get()));
237 }
238 template<class T, class U>
240 {
241  T* p = dynamic_cast<typename shared_ptr<T>::element_type*>(ptr.get());
242  if (nullptr != p)
243  {
244  return shared_ptr<T>(ptr, p);
245  }
246  else
247  {
248  return shared_ptr<T>();
249  }
250 }
251 template<class T>
252 bool operator==(const shared_ptr<T>& pointer1, T* const pointer2) noexcept
253 {
254  return static_cast<T*>(pointer1) == pointer2;
255 }
256 template<class T>
257 bool operator!=(const shared_ptr<T>& pointer1, T* const pointer2) noexcept
258 {
259  return static_cast<T*>(pointer1) != pointer2;
260 }
261 
262 } // namespace std
263 #endif // CARLA_PROPER_CPP11_SUPPORT
264 
265 CARLA_BACKEND_START_NAMESPACE
266 
268 
269 CARLA_BACKEND_END_NAMESPACE
270 
271 // -----------------------------------------------------------------------
272 
273 #endif // CARLA_CPP_COMPAT_HPP_INCLUDED
std::operator<
bool operator<(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:221
std::operator>
bool operator>(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:229
std::shared_ptr_count::shared_ptr_count
shared_ptr_count()
Definition: CarlaPluginPtr.hpp:43
std::shared_ptr_count::pn
volatile long * pn
Definition: CarlaPluginPtr.hpp:97
std::shared_ptr::operator=
shared_ptr & operator=(shared_ptr ptr) noexcept
Definition: CarlaPluginPtr.hpp:142
std::operator==
bool operator==(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:209
std::shared_ptr
Definition: CarlaPluginPtr.hpp:101
std::shared_ptr_count::use_count
long use_count(void) const noexcept
Definition: CarlaPluginPtr.hpp:47
std::shared_ptr_count::release
void release(U *p) noexcept
Definition: CarlaPluginPtr.hpp:82
std::shared_ptr::get
T * get(void) const noexcept
Definition: CarlaPluginPtr.hpp:182
std::shared_ptr::~shared_ptr
~shared_ptr(void) noexcept
Definition: CarlaPluginPtr.hpp:148
std::shared_ptr::operator*
T & operator*() const noexcept
Definition: CarlaPluginPtr.hpp:174
std::shared_ptr_count::acquire
void acquire(U *p)
Definition: CarlaPluginPtr.hpp:58
std::shared_ptr::element_type
T element_type
Definition: CarlaPluginPtr.hpp:104
std::shared_ptr_count::swap
void swap(shared_ptr_count &lhs) noexcept
Definition: CarlaPluginPtr.hpp:45
std::shared_ptr::shared_ptr
shared_ptr(T *p)
Definition: CarlaPluginPtr.hpp:110
std::shared_ptr::shared_ptr
shared_ptr(void) noexcept
Definition: CarlaPluginPtr.hpp:106
std::shared_ptr::use_count
long use_count(void) const noexcept
Definition: CarlaPluginPtr.hpp:168
std::static_pointer_cast
shared_ptr< T > static_pointer_cast(const shared_ptr< U > &ptr)
Definition: CarlaPluginPtr.hpp:234
std::operator!=
bool operator!=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:213
std
Definition: CarlaPluginPtr.hpp:33
std::operator<=
bool operator<=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:217
std::shared_ptr::reset
void reset(void) noexcept
Definition: CarlaPluginPtr.hpp:153
std::shared_ptr_count
Definition: CarlaPluginPtr.hpp:40
std::dynamic_pointer_cast
shared_ptr< T > dynamic_pointer_cast(const shared_ptr< U > &ptr)
Definition: CarlaPluginPtr.hpp:239
std::shared_ptr::swap
void swap(shared_ptr &lhs) noexcept
Definition: CarlaPluginPtr.hpp:158
std::shared_ptr::shared_ptr
shared_ptr(const shared_ptr< U > &ptr) noexcept
Definition: CarlaPluginPtr.hpp:126
CarlaPluginPtr
CARLA_BACKEND_START_NAMESPACE typedef std::shared_ptr< CarlaPlugin > CarlaPluginPtr
Definition: CarlaPluginPtr.hpp:267
std::operator>=
bool operator>=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition: CarlaPluginPtr.hpp:225
std::shared_ptr_count::shared_ptr_count
shared_ptr_count(const shared_ptr_count &count)
Definition: CarlaPluginPtr.hpp:44
std::shared_ptr::shared_ptr
shared_ptr(const shared_ptr &ptr) noexcept
Definition: CarlaPluginPtr.hpp:134
std::shared_ptr::operator->
T * operator->() const noexcept
Definition: CarlaPluginPtr.hpp:178
std::shared_ptr::shared_ptr
shared_ptr(const shared_ptr< U > &ptr, T *p)
Definition: CarlaPluginPtr.hpp:118