// // Yet Another POD-Bot, based on PODBot by Markus Klinge ("CountFloyd"). // Copyright (c) Yet Another POD-Bot Contributors . // // This software is licensed under the MIT license. // Additional exceptions apply. For full license details, see LICENSE.txt // #pragma once #include #include CR_NAMESPACE_BEGIN // simple unique ptr template class UniquePtr final : public DenyCopying { private: T *m_ptr = nullptr; public: UniquePtr () = default; explicit UniquePtr (T *ptr) : m_ptr (ptr) { } UniquePtr (UniquePtr &&rhs) noexcept : m_ptr (rhs.release ()) { } template UniquePtr (UniquePtr &&rhs) noexcept : m_ptr (rhs.release ()) { } ~UniquePtr () { destroy (); } public: T *get () const { return m_ptr; } T *release () { auto ret = m_ptr; m_ptr = nullptr; return ret; } void reset (T *ptr = nullptr) { destroy (); m_ptr = ptr; } private: void destroy () { if (m_ptr) { alloc.destroy (m_ptr); m_ptr = nullptr; } } public: UniquePtr &operator = (UniquePtr &&rhs) noexcept { if (this != &rhs) { reset (rhs.release ()); } return *this; } template UniquePtr &operator = (UniquePtr &&rhs) noexcept { if (this != &rhs) { reset (rhs.release ()); } return *this; } UniquePtr &operator = (decltype (nullptr)) { destroy (); return *this; } T &operator * () const { return *m_ptr; } T *operator -> () const { return m_ptr; } explicit operator bool () const { return m_ptr != nullptr; } }; // create unique template UniquePtr makeUnique (Args &&... args) { return UniquePtr (alloc.create (cr::forward (args)...)); } CR_NAMESPACE_END