From 53ae3f24c9458d324572ac8c23f5b8c90c4ac24e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Dec 2021 20:11:09 +0000 Subject: Refactor wrapped_ptr to include destory function as template param, and possibly constructor function --- lib/ptr.hpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 10 deletions(-) (limited to 'lib/ptr.hpp') diff --git a/lib/ptr.hpp b/lib/ptr.hpp index 0b00285..f242670 100644 --- a/lib/ptr.hpp +++ b/lib/ptr.hpp @@ -2,23 +2,68 @@ #define PTR_H #include +#include -template class wrapped_ptr : public std::unique_ptr { +template class wrapped_ptr { public: - using std::unique_ptr::unique_ptr; - wrapped_ptr() : std::unique_ptr {{}, {}} { } + template + explicit wrapped_ptr(Obj * (*factory)(Params...), Args &&... args) : obj {factory(std::forward(args)...)} + { + } - inline - operator Obj *() const + explicit wrapped_ptr(wrapped_ptr && p) : obj {p.obj} { - return this->get(); + p.obj = nullptr; } - template - static auto - create(Obj * (*factory)(Params...), void (*deleter)(Obj *), Args &&... args) + ~wrapped_ptr() + { + if (obj) { + Destroy(obj); + } + } + + NO_COPY(wrapped_ptr); + + wrapped_ptr & + operator=(wrapped_ptr && p) + { + if (obj) { + Destroy(obj); + } + obj = p.obj; + p.obj = nullptr; + return *this; + } + + [[nodiscard]] inline + operator Obj *() const noexcept + { + return obj; + } + + [[nodiscard]] inline auto + operator->() const noexcept + { + return obj; + } + + [[nodiscard]] inline auto + get() const noexcept + { + return obj; + } + +protected: + explicit wrapped_ptr(Obj * o) : obj {o} { } + Obj * obj; +}; + +template class wrapped_ptrt : public wrapped_ptr { +public: + template + explicit wrapped_ptrt(Args &&... args) : wrapped_ptr {Create(std::forward(args)...)} { - return wrapped_ptr {factory(std::forward(args)...), deleter}; } }; -- cgit v1.2.3