#pragma once #include #include "pymol/type_traits.h" namespace pymol { /** * A copyable unique pointer. Will make a copy of the managed object with "new". */ template > class copyable_ptr : public std::unique_ptr { T* copy_ptr() const { return *this ? new T(**this) : nullptr; } public: using std::unique_ptr::unique_ptr; copyable_ptr() = default; // move copyable_ptr(copyable_ptr&& other) : copyable_ptr(other.release()) {} copyable_ptr& operator=(copyable_ptr&& other) { this->reset(other.release()); return *this; } // copy copyable_ptr(const copyable_ptr &other) : copyable_ptr(other.copy_ptr()) {} copyable_ptr& operator=(const copyable_ptr& other) { this->reset(other.copy_ptr()); return *this; } }; template copyable_ptr make_copyable(Args &&... args) { return copyable_ptr(new T(std::forward(args)...)); } /** * A unique pointer which copies to nullptr. */ template > class cache_ptr : public std::unique_ptr { public: using std::unique_ptr::unique_ptr; cache_ptr() = default; // move cache_ptr(cache_ptr&& other) noexcept : cache_ptr(other.release()) {} cache_ptr& operator=(cache_ptr&& other) noexcept { this->reset(other.release()); return *this; } // copy cache_ptr(const cache_ptr& other) {} cache_ptr& operator=(const cache_ptr& other) { this->reset(); return *this; } }; template cache_ptr make_cache(Args &&... args) { return cache_ptr(new T(std::forward(args)...)); } /** * Take ownership of a raw pointer with a custom delete function. * * Example: * * auto s = unique_ptr_take_ownership(strdup("Hello"), free); */ template std::unique_ptr unique_ptr_take_ownership(T* ptr, Deleter func) { return std::unique_ptr(ptr, func); } } // namespace pymol