#pragma once #include #include template class SharedPtr; template class OwnedPtr { public: OwnedPtr() { m_ptr = nullptr; } OwnedPtr(T* ptr) { m_ptr = ptr; } ~OwnedPtr() { if (m_ptr) delete m_ptr; } OwnedPtr(const OwnedPtr& other) = delete; OwnedPtr(OwnedPtr&& other) { m_ptr = other.m_ptr; other.m_ptr = nullptr; } OwnedPtr& operator=(const OwnedPtr& other) = delete; OwnedPtr& operator=(OwnedPtr&& other) { if (&other == this) return *this; if (m_ptr) delete m_ptr; m_ptr = other.m_ptr; other.m_ptr = nullptr; return *this; } T* ptr() const { return m_ptr; } T* operator->() const { return m_ptr; } T& operator*() const { return *m_ptr; } operator bool() const { return m_ptr != nullptr; } template friend Result> adopt_shared_from_owned(OwnedPtr&&); private: T* m_ptr; }; template Result> make_owned(Args... args) { T* raw = TRY(make(args...)); return OwnedPtr { raw }; } template OwnedPtr adopt_owned(T* ptr) { return OwnedPtr { ptr }; } template Result> adopt_owned_if_nonnull(T* ptr) { if (ptr) return OwnedPtr { ptr }; else return err(ENOMEM); }