Carla Backend
Loading...
Searching...
No Matches
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
33namespace 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{
42public:
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
96public:
97 volatile long* pn;
98};
99
100template<class T>
102{
103public:
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
187private:
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
200private:
201 template<class U>
202 friend class shared_ptr;
203
204private:
205 T* px;
207};
208
209template<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}
213template<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}
217template<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}
221template<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}
225template<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}
229template<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}
233template<class T, class U>
235{
236 return shared_ptr<T>(ptr, static_cast<typename shared_ptr<T>::element_type*>(ptr.get()));
237}
238template<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}
251template<class T>
252bool operator==(const shared_ptr<T>& pointer1, T* const pointer2) noexcept
253{
254 return static_cast<T*>(pointer1) == pointer2;
255}
256template<class T>
257bool 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
265CARLA_BACKEND_START_NAMESPACE
266
268
269CARLA_BACKEND_END_NAMESPACE
270
271// -----------------------------------------------------------------------
272
273#endif // CARLA_CPP_COMPAT_HPP_INCLUDED
CARLA_BACKEND_START_NAMESPACE typedef std::shared_ptr< CarlaPlugin > CarlaPluginPtr
Definition CarlaPluginPtr.hpp:267
Definition CarlaPluginPtr.hpp:41
void swap(shared_ptr_count &lhs) noexcept
Definition CarlaPluginPtr.hpp:45
shared_ptr_count(const shared_ptr_count &count)
Definition CarlaPluginPtr.hpp:44
void acquire(U *p)
Definition CarlaPluginPtr.hpp:58
long use_count(void) const noexcept
Definition CarlaPluginPtr.hpp:47
shared_ptr_count()
Definition CarlaPluginPtr.hpp:43
void release(U *p) noexcept
Definition CarlaPluginPtr.hpp:82
volatile long * pn
Definition CarlaPluginPtr.hpp:97
Definition CarlaPluginPtr.hpp:102
shared_ptr(T *p)
Definition CarlaPluginPtr.hpp:110
~shared_ptr(void) noexcept
Definition CarlaPluginPtr.hpp:148
T * operator->() const noexcept
Definition CarlaPluginPtr.hpp:178
void reset(void) noexcept
Definition CarlaPluginPtr.hpp:153
shared_ptr(const shared_ptr &ptr) noexcept
Definition CarlaPluginPtr.hpp:134
void swap(shared_ptr &lhs) noexcept
Definition CarlaPluginPtr.hpp:158
shared_ptr(const shared_ptr< U > &ptr) noexcept
Definition CarlaPluginPtr.hpp:126
T element_type
Definition CarlaPluginPtr.hpp:104
T & operator*() const noexcept
Definition CarlaPluginPtr.hpp:174
long use_count(void) const noexcept
Definition CarlaPluginPtr.hpp:168
T * get(void) const noexcept
Definition CarlaPluginPtr.hpp:182
friend class shared_ptr
Definition CarlaPluginPtr.hpp:202
shared_ptr(void) noexcept
Definition CarlaPluginPtr.hpp:106
shared_ptr(const shared_ptr< U > &ptr, T *p)
Definition CarlaPluginPtr.hpp:118
shared_ptr & operator=(shared_ptr ptr) noexcept
Definition CarlaPluginPtr.hpp:142
Definition CarlaPluginPtr.hpp:33
bool operator<=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:217
bool operator<(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:221
bool operator==(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:209
shared_ptr< T > static_pointer_cast(const shared_ptr< U > &ptr)
Definition CarlaPluginPtr.hpp:234
bool operator>=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:225
bool operator!=(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:213
bool operator>(const shared_ptr< T > &l, const shared_ptr< U > &r) noexcept
Definition CarlaPluginPtr.hpp:229
shared_ptr< T > dynamic_pointer_cast(const shared_ptr< U > &ptr)
Definition CarlaPluginPtr.hpp:239