|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- //-----------------------------------------------------------------------------
- // Project : SDK Core
- //
- // Category : SDK Core Interfaces
- // Filename : pluginterfaces/base/smartpointer.h
- // Created by : Steinberg, 01/2004
- // Description : Basic Interface
- //
- //-----------------------------------------------------------------------------
- // This file is part of a Steinberg SDK. It is subject to the license terms
- // in the LICENSE file found in the top-level directory of this distribution
- // and at www.steinberg.net/sdklicenses.
- // No part of the SDK, including this file, may be copied, modified, propagated,
- // or distributed except according to the terms contained in the LICENSE file.
- //-----------------------------------------------------------------------------
-
- #pragma once
-
- #include "pluginterfaces/base/fplatform.h"
- #if SMTG_CPP11_STDLIBSUPPORT
- #include <memory>
- #endif
-
- //------------------------------------------------------------------------
- namespace Steinberg {
-
- //------------------------------------------------------------------------
- // IPtr
- //------------------------------------------------------------------------
- /** IPtr - Smart pointer template class.
- \ingroup pluginBase
-
- - can be used as an I* pointer
- - handles refCount of the interface
- - Usage example:
- \code
- IPtr<IPath> path (sharedPath);
- if (path)
- path->ascend ();
- \endcode
- */
- //------------------------------------------------------------------------
- template <class I>
- class IPtr
- {
- public:
- //------------------------------------------------------------------------
- inline IPtr (I* ptr, bool addRef = true);
- inline IPtr (const IPtr&);
-
- template <class T>
- inline IPtr (const IPtr<T>& other) : ptr (other.get ())
- {
- if (ptr)
- ptr->addRef ();
- }
-
- inline IPtr ();
- inline ~IPtr ();
-
- inline I* operator= (I* ptr);
-
- inline IPtr& operator= (const IPtr& other);
-
- template <class T>
- inline IPtr& operator= (const IPtr<T>& other)
- {
- operator= (other.get ());
- return *this;
- }
-
- inline operator I* () const { return ptr; } // act as I*
- inline I* operator-> () const { return ptr; } // act as I*
-
- inline I* get () const { return ptr; }
-
- #if SMTG_CPP11_STDLIBSUPPORT
- inline IPtr (IPtr<I>&& movePtr) : ptr (nullptr) { *this = std::move (movePtr); }
- inline IPtr& operator= (IPtr<I>&& movePtr)
- {
- if (ptr)
- ptr->release ();
- ptr = movePtr.ptr;
- movePtr.ptr = nullptr;
- return *this;
- }
- #endif
- //------------------------------------------------------------------------
- protected:
- I* ptr;
- };
-
- //------------------------------------------------------------------------
- template <class I>
- inline IPtr<I>::IPtr (I* _ptr, bool addRef) : ptr (_ptr)
- {
- if (ptr && addRef)
- ptr->addRef ();
- }
-
- //------------------------------------------------------------------------
- template <class I>
- inline IPtr<I>::IPtr (const IPtr<I>& other) : ptr (other.ptr)
- {
- if (ptr)
- ptr->addRef ();
- }
-
- //------------------------------------------------------------------------
- template <class I>
- inline IPtr<I>::IPtr () : ptr (0)
- {
- }
-
- //------------------------------------------------------------------------
- template <class I>
- inline IPtr<I>::~IPtr ()
- {
- if (ptr)
- ptr->release ();
- }
-
- //------------------------------------------------------------------------
- template <class I>
- inline I* IPtr<I>::operator= (I* _ptr)
- {
- if (_ptr != ptr)
- {
- if (ptr)
- ptr->release ();
- ptr = _ptr;
- if (ptr)
- ptr->addRef ();
- }
- return ptr;
- }
-
- //------------------------------------------------------------------------
- template <class I>
- inline IPtr<I>& IPtr<I>::operator= (const IPtr<I>& _ptr)
- {
- operator= (_ptr.ptr);
- return *this;
- }
-
- //------------------------------------------------------------------------
- /** OPtr - "owning" smart pointer used for newly created FObjects.
- \ingroup pluginBase
-
- FUnknown implementations are supposed to have a refCount of 1 right after creation.
- So using an IPtr on newly created objects would lead to a leak.
- Instead the OPtr can be used in this case. \n
- Example:
- \code
- OPtr<IPath> path = FHostCreate (IPath, hostClasses);
- // no release is needed...
- \endcode
- The assignment operator takes ownership of a new object and releases the old.
- So its safe to write:
- \code
- OPtr<IPath> path = FHostCreate (IPath, hostClasses);
- path = FHostCreate (IPath, hostClasses);
- path = 0;
- \endcode
- This is the difference to using an IPtr with addRef=false.
- \code
- // DONT DO THIS:
- IPtr<IPath> path (FHostCreate (IPath, hostClasses), false);
- path = FHostCreate (IPath, hostClasses);
- path = 0;
- \endcode
- This will lead to a leak!
- */
- //------------------------------------------------------------------------
- template <class I>
- class OPtr : public IPtr<I>
- {
- public:
- //------------------------------------------------------------------------
- inline OPtr (I* p) : IPtr<I> (p, false) {}
- inline OPtr (const IPtr<I>& p) : IPtr<I> (p) {}
- inline OPtr (const OPtr<I>& p) : IPtr<I> (p) {}
- inline OPtr () {}
- inline I* operator= (I* _ptr)
- {
- if (_ptr != this->ptr)
- {
- if (this->ptr)
- this->ptr->release ();
- this->ptr = _ptr;
- }
- return this->ptr;
- }
- };
-
- //------------------------------------------------------------------------
- /** Assigning newly created object to an IPtr.
- Example:
- \code
- IPtr<IPath> path = owned (FHostCreate (IPath, hostClasses));
- \endcode
- which is a slightly shorter form of writing:
- \code
- IPtr<IPath> path = OPtr<IPath> (FHostCreate (IPath, hostClasses));
- \endcode
- */
- template <class I>
- IPtr<I> owned (I* p)
- {
- return IPtr<I> (p, false);
- }
-
- /** Assigning shared object to an IPtr.
- Example:
- \code
- IPtr<IPath> path = shared (iface.getXY ());
- \endcode
- */
- template <class I>
- IPtr<I> shared (I* p)
- {
- return IPtr<I> (p, true);
- }
-
- #if SMTG_CPP11_STDLIBSUPPORT
- //------------------------------------------------------------------------
- // Ownership functionality
- //------------------------------------------------------------------------
- namespace SKI {
- namespace Detail {
- struct Adopt;
- } // Detail
-
- /** Strong typedef for shared reference counted objects.
- * Use SKI::adopt to unwrap the provided object.
- * @tparam T Referenced counted type.
- */
- template <typename T>
- class Shared
- {
- friend struct Detail::Adopt;
- T* obj = nullptr;
- };
-
- /** Strong typedef for transferring the ownership of reference counted objects.
- * Use SKI::adopt to unwrap the provided object.
- * After calling adopt the reference in this object is null.
- * @tparam T Referenced counted type.
- */
- template <typename T>
- class Owned
- {
- friend struct Detail::Adopt;
- T* obj = nullptr;
- };
-
- /** Strong typedef for using reference counted objects.
- * Use SKI::adopt to unwrap the provided object.
- * After calling adopt the reference in this object is null.
- * @tparam T Referenced counted type.
- */
- template <typename T>
- class Used
- {
- friend struct Detail::Adopt;
- T* obj = nullptr;
- };
-
- namespace Detail {
-
- struct Adopt
- {
- template <typename T>
- static IPtr<T> adopt (Shared<T>& ref)
- {
- using Steinberg::shared;
- return shared (ref.obj);
- }
-
- template <typename T>
- static IPtr<T> adopt (Owned<T>& ref)
- {
- using Steinberg::owned;
- IPtr<T> out = owned (ref.obj);
- ref.obj = nullptr;
- return out;
- }
-
- template <typename T>
- static T* adopt (Used<T>& ref)
- {
- return ref.obj;
- }
-
- template <template <typename> class OwnerType, typename T>
- static OwnerType<T> toOwnerType (T* obj)
- {
- OwnerType<T> out;
- out.obj = obj;
- return out;
- }
- };
-
- } // Detail
-
- /** Common function to adopt referenced counted object.
- * @tparam T Referenced counted type.
- * @param ref The reference to be adopted in a smart pointer.
- */
- template <typename T>
- IPtr<T> adopt (Shared<T>& ref) { return Detail::Adopt::adopt (ref); }
-
- template <typename T>
- IPtr<T> adopt (Shared<T>&& ref) { return Detail::Adopt::adopt (ref); }
-
- /** Common function to adopt referenced counted object.
- * @tparam T Referenced counted type.
- * @param ref The reference to be adopted in a smart pointer.
- */
- template <typename T>
- IPtr<T> adopt (Owned<T>& ref) { return Detail::Adopt::adopt (ref); }
-
- template <typename T>
- IPtr<T> adopt (Owned<T>&& ref) { return Detail::Adopt::adopt (ref); }
-
- /** Common function to adopt referenced counted object.
- * @tparam T Referenced counted type.
- * @param ref The reference to be adopted in a smart pointer.
- */
- template <typename T>
- T* adopt (Used<T>& ref) { return Detail::Adopt::adopt (ref); }
-
- template <typename T>
- T* adopt (Used<T>&& ref) { return Detail::Adopt::adopt (ref); }
-
- /** Common function to wrap owned instances. */
- template <typename T>
- Owned<T> toOwned (T* obj) { return Detail::Adopt::toOwnerType<Owned> (obj); }
-
- /** Common function to wrap shared instances. */
- template <typename T>
- Shared<T> toShared (T* obj) { return Detail::Adopt::toOwnerType<Shared> (obj); }
-
- /** Common function to wrap used instances. */
- template <typename T>
- Used<T> toUsed (T* obj) { return Detail::Adopt::toOwnerType<Used> (obj); }
-
- //------------------------------------------------------------------------
- } // SKI
- #endif
- } // Steinberg
|