summaryrefslogtreecommitdiff
path: root/lib/ptr.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-29 19:07:11 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-29 19:07:11 +0100
commit5a0b3927a33807cca4c77c40eb873f8a3b51b0b0 (patch)
tree4af0585ee8f8f468ab10c0a4fe9994fb30b79599 /lib/ptr.h
parentDunno how, but some DOS new lines got in here! (diff)
downloadilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.tar.bz2
ilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.tar.xz
ilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.zip
Drop .hpp for header only things
Half of them acquired a .cpp part anyway
Diffstat (limited to 'lib/ptr.h')
-rw-r--r--lib/ptr.h67
1 files changed, 67 insertions, 0 deletions
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 <memory>
+#include <special_members.h>
+
+template<typename Obj, auto Destroy> class wrapped_ptr {
+public:
+ template<typename... Args, typename... Params>
+ explicit wrapped_ptr(Obj * (*factory)(Params...), Args &&... args) : obj {factory(std::forward<Args>(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<typename Obj, auto Create, auto Destroy> class wrapped_ptrt : public wrapped_ptr<Obj, Destroy> {
+public:
+ template<typename... Args>
+ explicit wrapped_ptrt(Args &&... args) : wrapped_ptr<Obj, Destroy> {Create(std::forward<Args>(args)...)}
+ {
+ }
+};