summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-12-04 20:11:09 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-12-04 20:11:09 +0000
commit53ae3f24c9458d324572ac8c23f5b8c90c4ac24e (patch)
tree6dfda92116d5f41f6ad9d730785c0694c174e5e2 /lib
parentGCC 11 now default (diff)
downloadilt-53ae3f24c9458d324572ac8c23f5b8c90c4ac24e.tar.bz2
ilt-53ae3f24c9458d324572ac8c23f5b8c90c4ac24e.tar.xz
ilt-53ae3f24c9458d324572ac8c23f5b8c90c4ac24e.zip
Refactor wrapped_ptr to include destory function as template param, and possibly constructor function
Diffstat (limited to 'lib')
-rw-r--r--lib/ptr.hpp65
1 files changed, 55 insertions, 10 deletions
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 <memory>
+#include <special_members.hpp>
-template<typename Obj> class wrapped_ptr : public std::unique_ptr<Obj, void (*)(Obj *)> {
+template<typename Obj, auto Destroy> class wrapped_ptr {
public:
- using std::unique_ptr<Obj, void (*)(Obj *)>::unique_ptr;
- wrapped_ptr() : std::unique_ptr<Obj, void (*)(Obj *)> {{}, {}} { }
+ template<typename... Args, typename... Params>
+ explicit wrapped_ptr(Obj * (*factory)(Params...), Args &&... args) : obj {factory(std::forward<Args>(args)...)}
+ {
+ }
- inline
- operator Obj *() const
+ explicit wrapped_ptr(wrapped_ptr && p) : obj {p.obj}
{
- return this->get();
+ p.obj = nullptr;
}
- template<typename... Args, typename... Params>
- 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<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)...)}
{
- return wrapped_ptr<Obj> {factory(std::forward<Args>(args)...), deleter};
}
};