JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
smartpointer.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2// Project : SDK Core
3//
4// Category : SDK Core Interfaces
5// Filename : pluginterfaces/base/smartpointer.h
6// Created by : Steinberg, 01/2004
7// Description : Basic Interface
8//
9//-----------------------------------------------------------------------------
10// This file is part of a Steinberg SDK. It is subject to the license terms
11// in the LICENSE file found in the top-level directory of this distribution
12// and at www.steinberg.net/sdklicenses.
13// No part of the SDK, including this file, may be copied, modified, propagated,
14// or distributed except according to the terms contained in the LICENSE file.
15//-----------------------------------------------------------------------------
16
17#pragma once
18
20#if SMTG_CPP11_STDLIBSUPPORT
21#include <utility>
22#endif
23
24//------------------------------------------------------------------------
25namespace Steinberg {
26
27//------------------------------------------------------------------------
28// IPtr
29//------------------------------------------------------------------------
42template <class I>
43class IPtr
44{
45public:
46//------------------------------------------------------------------------
47 inline IPtr (I* ptr, bool addRef = true);
48 inline IPtr (const IPtr&);
49
50 template <class T>
51 inline IPtr (const IPtr<T>& other) : ptr (other.get ())
52 {
53 if (ptr)
54 ptr->addRef ();
55 }
56
57 inline IPtr ();
58 inline ~IPtr ();
59
60 inline I* operator= (I* ptr);
61
62 inline IPtr& operator= (const IPtr& other);
63
64 template <class T>
65 inline IPtr& operator= (const IPtr<T>& other)
66 {
67 operator= (other.get ());
68 return *this;
69 }
70
71 inline operator I* () const { return ptr; } // act as I*
72 inline I* operator-> () const { return ptr; } // act as I*
73
74 inline I* get () const { return ptr; }
75
76#if SMTG_CPP11_STDLIBSUPPORT
77 inline IPtr (IPtr<I>&& movePtr) SMTG_NOEXCEPT : ptr (movePtr.take ()) { }
78
79 template <typename T>
80 inline IPtr (IPtr<T>&& movePtr) SMTG_NOEXCEPT : ptr (movePtr.take ()) { }
81
82 inline IPtr& operator= (IPtr<I>&& movePtr) SMTG_NOEXCEPT
83 {
84 if (ptr)
85 ptr->release ();
86
87 ptr = movePtr.take ();
88 return *this;
89 }
90
91 template <typename T>
92 inline IPtr& operator= (IPtr<T>&& movePtr)
93 {
94 if (ptr)
95 ptr->release ();
96
97 ptr = movePtr.take ();
98 return *this;
99 }
100#endif
101
102 inline void reset (I* obj = nullptr)
103 {
104 if (ptr)
105 ptr->release();
106 ptr = obj;
107 }
108
109 I* take () SMTG_NOEXCEPT
110 {
111 I* out = ptr;
112 ptr = nullptr;
113 return out;
114 }
115
116 template <typename T>
117 static IPtr<T> adopt (T* obj) SMTG_NOEXCEPT { return IPtr<T> (obj, false); }
118
119//------------------------------------------------------------------------
120protected:
121 I* ptr;
122};
123
124//------------------------------------------------------------------------
125template <class I>
126inline IPtr<I>::IPtr (I* _ptr, bool addRef) : ptr (_ptr)
127{
128 if (ptr && addRef)
129 ptr->addRef ();
130}
131
132//------------------------------------------------------------------------
133template <class I>
134inline IPtr<I>::IPtr (const IPtr<I>& other) : ptr (other.ptr)
135{
136 if (ptr)
137 ptr->addRef ();
138}
139
140//------------------------------------------------------------------------
141template <class I>
142inline IPtr<I>::IPtr () : ptr (0)
143{
144}
145
146//------------------------------------------------------------------------
147template <class I>
148inline IPtr<I>::~IPtr ()
149{
150 if (ptr)
151 {
152 ptr->release ();
153 ptr = nullptr; //TODO_CORE: how much does this cost? is this something hiding for us?
154 }
155}
156
157//------------------------------------------------------------------------
158template <class I>
159inline I* IPtr<I>::operator= (I* _ptr)
160{
161 if (_ptr != ptr)
162 {
163 if (ptr)
164 ptr->release ();
165 ptr = _ptr;
166 if (ptr)
167 ptr->addRef ();
168 }
169 return ptr;
170}
171
172//------------------------------------------------------------------------
173template <class I>
174inline IPtr<I>& IPtr<I>::operator= (const IPtr<I>& _ptr)
175{
176 operator= (_ptr.ptr);
177 return *this;
178}
179
180//------------------------------------------------------------------------
208template <class I>
209class OPtr : public IPtr<I>
210{
211public:
212//------------------------------------------------------------------------
213 inline OPtr (I* p) : IPtr<I> (p, false) {}
214 inline OPtr (const IPtr<I>& p) : IPtr<I> (p) {}
215 inline OPtr (const OPtr<I>& p) : IPtr<I> (p) {}
216 inline OPtr () {}
217 inline I* operator= (I* _ptr)
218 {
219 if (_ptr != this->ptr)
220 {
221 if (this->ptr)
222 this->ptr->release ();
223 this->ptr = _ptr;
224 }
225 return this->ptr;
226 }
227};
228
229//------------------------------------------------------------------------
240template <class I>
242{
243 return IPtr<I> (p, false);
244}
245
252template <class I>
254{
255 return IPtr<I> (p, true);
256}
257
258#if SMTG_CPP11_STDLIBSUPPORT
259//------------------------------------------------------------------------
260// Ownership functionality
261//------------------------------------------------------------------------
262namespace SKI {
263namespace Detail {
264struct Adopt;
265} // Detail
266
271template <typename T>
272class Shared
273{
274 friend struct Detail::Adopt;
275 T* obj = nullptr;
276};
277
283template <typename T>
284class Owned
285{
286 friend struct Detail::Adopt;
287 T* obj = nullptr;
288};
289
295template <typename T>
296class Used
297{
298 friend struct Detail::Adopt;
299 T* obj = nullptr;
300};
301
302namespace Detail {
303
304struct Adopt
305{
306 template <typename T>
307 static IPtr<T> adopt (Shared<T>& ref)
308 {
309 using Steinberg::shared;
310 return shared (ref.obj);
311 }
312
313 template <typename T>
314 static IPtr<T> adopt (Owned<T>& ref)
315 {
316 using Steinberg::owned;
317 IPtr<T> out = owned (ref.obj);
318 ref.obj = nullptr;
319 return out;
320 }
321
322 template <typename T>
323 static T* adopt (Used<T>& ref)
324 {
325 return ref.obj;
326 }
327
328 template <template <typename> class OwnerType, typename T>
329 static OwnerType<T> toOwnerType (T* obj)
330 {
331 OwnerType<T> out;
332 out.obj = obj;
333 return out;
334 }
335};
336
337} // Detail
338
343template <typename T>
344IPtr<T> adopt (Shared<T>& ref) { return Detail::Adopt::adopt (ref); }
345
346template <typename T>
347IPtr<T> adopt (Shared<T>&& ref) { return Detail::Adopt::adopt (ref); }
348
353template <typename T>
354IPtr<T> adopt (Owned<T>& ref) { return Detail::Adopt::adopt (ref); }
355
356template <typename T>
357IPtr<T> adopt (Owned<T>&& ref) { return Detail::Adopt::adopt (ref); }
358
363template <typename T>
364T* adopt (Used<T>& ref) { return Detail::Adopt::adopt (ref); }
365
366template <typename T>
367T* adopt (Used<T>&& ref) { return Detail::Adopt::adopt (ref); }
368
370template <typename T>
371Owned<T> toOwned (T* obj) { return Detail::Adopt::toOwnerType<Owned> (obj); }
372
374template <typename T>
375Shared<T> toShared (T* obj) { return Detail::Adopt::toOwnerType<Shared> (obj); }
376
378template <typename T>
379Used<T> toUsed (T* obj) { return Detail::Adopt::toOwnerType<Used> (obj); }
380
381//------------------------------------------------------------------------
382} // SKI
383#endif
384} // Steinberg
IPtr - Smart pointer template class.
OPtr - "owning" smart pointer used for newly created FObjects.
T ref(T... args)
IPtr< I > shared(I *p)
Assigning shared object to an IPtr.
IPtr< I > owned(I *p)
Assigning newly created object to an IPtr.