// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel #ifndef NEKIT_OWNPTR_H #define NEKIT_OWNPTR_H #include #include #include #include namespace Kernel { template class OwnPtr; template class NonNullRefPtr; template class OwnPtr final { public: OwnPtr() : fCls(nullptr) {} ~OwnPtr() { this->Reset(); } OwnPtr& operator=(const OwnPtr&) = default; OwnPtr(const OwnPtr&) = default; public: template bool New(Args&&... arg) { if (fCls) { return false; } fCls = new T(arg...); return fCls; } void Reset() { if (fCls) delete fCls; fCls = nullptr; } T* operator->() const { return fCls; } T* Raw() { return fCls; } Ref AsRef() { return Ref(fCls); } explicit operator bool() { return fCls; } private: T* fCls{nullptr}; }; template using OwnOr = ErrorOr>; template inline OwnPtr make_ptr(Args&&... args) { OwnPtr ret; ret.template New(forward(args)...); return ret; } } // namespace Kernel #endif