#ifndef _DEUNIQUEPTR_HPP #define _DEUNIQUEPTR_HPP /*------------------------------------------------------------------------- * drawElements C++ Base Library * ----------------------------- * * Copyright 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *//*! * \file * \brief Unique pointer. *//*--------------------------------------------------------------------*/ #include "deDefs.hpp" namespace de { //! Unique pointer self-test. void UniquePtr_selfTest (void); // Hide implementation-private types in a details namespace. namespace details { //! Auxiliary struct used to pass references between unique pointers. To //! ensure that managed pointers are deleted exactly once, this type should //! not appear in user code. template struct PtrData { PtrData (T* p, D d) : ptr(p), deleter(d) {} template PtrData (const PtrData& d) : ptr(d.ptr), deleter(d.deleter) {} T* ptr; D deleter; }; template class UniqueBase { public: typedef T element_type; typedef D deleter_type; T* get (void) const throw() { return m_data.ptr; } //!< Get stored pointer. D getDeleter (void) const throw() { return m_data.deleter; } T* operator-> (void) const throw() { return get(); } //!< Get stored pointer. T& operator* (void) const throw() { return *get(); } //!< De-reference stored pointer. operator bool (void) const throw() { return !!get(); } protected: UniqueBase (T* ptr, D deleter) : m_data(ptr, deleter) {} UniqueBase (PtrData data) : m_data(data) {} ~UniqueBase (void); void reset (void); //!< Delete previous pointer, set to null. PtrData releaseData (void) throw(); //!< Relinquish ownership, return pointer data. void assignData (PtrData data); //!< Set new pointer, delete previous pointer. private: PtrData m_data; }; template UniqueBase::~UniqueBase (void) { reset(); } template void UniqueBase::reset (void) { if (m_data.ptr != DE_NULL) { m_data.deleter(m_data.ptr); m_data.ptr = DE_NULL; } } template PtrData UniqueBase::releaseData (void) throw() { PtrData data = m_data; m_data.ptr = DE_NULL; return data; } template void UniqueBase::assignData (PtrData data) { if (data.ptr != m_data.ptr) { reset(); m_data = data; } } /*--------------------------------------------------------------------*//*! * \brief Movable unique pointer * * A MovePtr is smart pointer that retains sole ownership of a pointer and * destroys it when it is destroyed (for example when it goes out of scope). * * A MovePtr can be copied and assigned to. The pointer ownership is moved to * the newly constructer or assigned-to MovePtr. Upon assignment to a * MovePtr, the previously managed pointer is deleted. * *//*--------------------------------------------------------------------*/ template > class MovePtr : public UniqueBase { public: MovePtr (void) : UniqueBase (DE_NULL, Deleter()) {} explicit MovePtr (T* ptr, Deleter deleter = Deleter()) : UniqueBase (ptr, deleter) {} MovePtr (MovePtr& other) : UniqueBase (other.releaseData()) {} MovePtr& operator= (MovePtr& other); T* release (void) throw(); void clear (void) { this->reset(); } // These implicit by-value conversions to and from a PtrData are used to // allow copying a MovePtr by value when returning from a function. To // ensure that the managed pointer gets deleted exactly once, the PtrData // should only exist as a temporary conversion step between two MovePtrs. MovePtr (PtrData data) : UniqueBase (data) {} MovePtr& operator= (PtrData data); template operator PtrData (void) { return this->releaseData(); } }; template MovePtr& MovePtr::operator= (PtrData data) { this->assignData(data); return *this; } template MovePtr& MovePtr::operator= (MovePtr& other) { return (*this = other.releaseData()); } //! Steal the managed pointer. The caller is responsible for explicitly //! deleting the returned pointer. template inline T* MovePtr::release (void) throw() { return this->releaseData().ptr; } //! Construct a MovePtr from a pointer. template inline MovePtr movePtr (T* ptr) { return MovePtr(ptr); } //! Allocate and construct an object and return its address as a MovePtr. template inline MovePtr newMovePtr (void) { return MovePtr(new T()); } template inline MovePtr newMovePtr (P0 p0) { return MovePtr(new T(p0)); } template inline MovePtr newMovePtr (P0 p0, P1 p1) { return MovePtr(new T(p0, p1)); } template inline MovePtr newMovePtr (P0 p0, P1 p1, P2 p2) { return MovePtr(new T(p0, p1, p2)); } /*--------------------------------------------------------------------*//*! * \brief Unique pointer * * UniquePtr is smart pointer that retains sole ownership of a pointer * and destroys it when UniquePtr is destroyed (for example when UniquePtr * goes out of scope). * * UniquePtr is not copyable or assignable. Pointer ownership can be transferred * from a UniquePtr only explicitly with the move() member function. * * A UniquePtr can be constructed from a MovePtr. In this case it assumes * ownership of the pointer from the MovePtr. Because a UniquePtr cannot be * copied, direct initialization syntax must be used, i.e.: * * MovePtr createFoo (void); * UniquePtr fooPtr(createFoo()); // NOT fooPtr = createFoo(); * *//*--------------------------------------------------------------------*/ template > class UniquePtr : public UniqueBase { public: explicit UniquePtr (T* ptr, Deleter deleter = Deleter()); UniquePtr (PtrData data); MovePtr move (void); private: UniquePtr (const UniquePtr& other); // Not allowed! UniquePtr operator= (const UniquePtr& other); // Not allowed! }; /*--------------------------------------------------------------------*//*! * \brief Construct unique pointer. * \param ptr Pointer to be managed. * * Pointer ownership is transferred to the UniquePtr. *//*--------------------------------------------------------------------*/ template inline UniquePtr::UniquePtr (T* ptr, Deleter deleter) : UniqueBase (ptr, deleter) { } template inline UniquePtr::UniquePtr (PtrData data) : UniqueBase (data) { } /*--------------------------------------------------------------------*//*! * \brief Relinquish ownership of pointer. * * This method returns a MovePtr that now owns the pointer. The pointer in * the UniquePtr is set to null. *//*--------------------------------------------------------------------*/ template inline MovePtr UniquePtr::move (void) { return MovePtr(this->releaseData()); } } // details using details::UniquePtr; using details::MovePtr; using details::newMovePtr; } // de #endif // _DEUNIQUEPTR_HPP