From 5a0b3927a33807cca4c77c40eb873f8a3b51b0b0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 29 Apr 2023 19:07:11 +0100 Subject: Drop .hpp for header only things Half of them acquired a .cpp part anyway --- lib/ptr.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/ptr.h (limited to 'lib/ptr.h') diff --git a/lib/ptr.h b/lib/ptr.h new file mode 100644 index 0000000..b7d15a1 --- /dev/null +++ b/lib/ptr.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include + +template class wrapped_ptr { +public: + template + explicit wrapped_ptr(Obj * (*factory)(Params...), Args &&... args) : obj {factory(std::forward(args)...)} + { + } + + explicit wrapped_ptr(wrapped_ptr && p) : obj {p.obj} + { + p.obj = nullptr; + } + + ~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)...)} + { + } +}; -- cgit v1.2.3