From 2c088213e0768d4fe9d9a0126702608b457bc943 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 13 Jul 2018 19:15:21 -0400 Subject: ... --- include/salticidae/ref.h | 107 +++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 46 deletions(-) (limited to 'include/salticidae/ref.h') diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h index 7239402..972a3d1 100644 --- a/include/salticidae/ref.h +++ b/include/salticidae/ref.h @@ -52,32 +52,40 @@ struct default_delete { } }; -template -class BoxObj; +template class _RcObjBase; +template class _BoxObj; +template class BoxObj; template class _BoxObj { protected: using ptr_type = T *; ptr_type obj; - constexpr _BoxObj(ptr_type obj): obj(obj) {} + + template + friend class _RcObjBase; + template + friend class _BoxObj; public: constexpr _BoxObj(): obj(nullptr) {} + constexpr _BoxObj(ptr_type obj): obj(obj) {} _BoxObj(const _BoxObj &other) = delete; _BoxObj(_BoxObj &&other): obj(other.obj) { other.obj = nullptr; } + template + _BoxObj(_BoxObj &&other): obj(other.obj) { + other.obj = nullptr; + } ~_BoxObj() { if (obj) D()(obj); } - void swap(_BoxObj &other) { - std::swap(obj, other.obj); - } + void swap(_BoxObj &other) { std::swap(obj, other.obj); } _BoxObj &operator=(const _BoxObj &other) = delete; _BoxObj &operator=(_BoxObj &&other) { @@ -111,9 +119,9 @@ class BoxObj: public _BoxObj { public: BoxObj() = default; BoxObj(T *obj): base_t(obj) {} - template::value>::type> - BoxObj(BoxObj &&other): base_t(other) {} + template::value>::type> + BoxObj(BoxObj &&other): base_t(std::move(other)) {} T *operator->() const { return base_t::obj; } }; @@ -127,11 +135,10 @@ class BoxObj: public _BoxObj { public: BoxObj() = default; BoxObj(T obj[]): base_t(obj) {} - template - BoxObj(BoxObj &&other): base_t(std::move(other)) {} + template + BoxObj(BoxObj &&other): base_t(std::move(other)) {} - T &operator[](size_t idx) { return base_t::obj[idx]; } - const T &operator[] (size_t idx) const { return base_t::obj[idx]; } + T &operator[](size_t idx) const { return base_t::obj[idx]; } }; template, typename T_, typename D_> @@ -186,7 +193,6 @@ struct _ARCCtl { ~_ARCCtl() {} }; -template class _RcObjBase; template class RcObjBase; template @@ -200,6 +206,16 @@ class _WeakObjBase { public: _WeakObjBase(): ctl(nullptr) {} + _WeakObjBase(const _WeakObjBase &other): + obj(other.obj), ctl(other.ctl) { + if (ctl) ctl->add_weak(); + } + + _WeakObjBase(_WeakObjBase &&other): + obj(other.obj), ctl(other.ctl) { + other.ctl = nullptr; + } + void swap(_WeakObjBase &other) { std::swap(obj, other.obj); std::swap(ctl, other.ctl); @@ -223,16 +239,6 @@ class _WeakObjBase { return *this; } - _WeakObjBase(const _WeakObjBase &other): - obj(other.obj), ctl(other.ctl) { - if (ctl) ctl->add_weak(); - } - - _WeakObjBase(_WeakObjBase &&other): - obj(other.obj), ctl(other.ctl) { - other.ctl = nullptr; - } - template _WeakObjBase(const _RcObjBase &other); @@ -244,6 +250,7 @@ template class WeakObjBase: public _WeakObjBase { using base_t = _WeakObjBase; friend std::hash; + public: WeakObjBase() = default; WeakObjBase(const WeakObjBase &other) = default; @@ -258,6 +265,7 @@ template class WeakObjBase: public _WeakObjBase { using base_t = _WeakObjBase; friend std::hash; + public: WeakObjBase() = default; WeakObjBase(const WeakObjBase &other) = default; @@ -287,6 +295,28 @@ class _RcObjBase { box_ref.obj = nullptr; } + _RcObjBase(const _RcObjBase &other): + obj(other.obj), ctl(other.ctl) { + if (ctl) ctl->add_ref(); + } + + _RcObjBase(_RcObjBase &&other): + obj(other.obj), ctl(other.ctl) { + other.ctl = nullptr; + } + + template + _RcObjBase(const _RcObjBase &other): + obj(other.obj), ctl(other.ctl) { + if (ctl) ctl->add_ref(); + } + + template + _RcObjBase(_RcObjBase &&other): + obj(other.obj), ctl(other.ctl) { + other.ctl = nullptr; + } + void swap(_RcObjBase &other) { std::swap(obj, other.obj); std::swap(ctl, other.ctl); @@ -310,28 +340,6 @@ class _RcObjBase { return *this; } - _RcObjBase(const _RcObjBase &other): - obj(other.obj), ctl(other.ctl) { - if (ctl) ctl->add_ref(); - } - - template - _RcObjBase(const _RcObjBase &other): - obj(other.obj), ctl(other.ctl) { - if (ctl) ctl->add_ref(); - } - - _RcObjBase(_RcObjBase &&other): - obj(other.obj), ctl(other.ctl) { - other.ctl = nullptr; - } - - template - _RcObjBase(_RcObjBase &&other): - obj(other.obj), ctl(other.ctl) { - other.ctl = nullptr; - } - _RcObjBase(const _WeakObjBase &other) { if (other.ctl && other.ctl->ref_cnt) { @@ -468,6 +476,13 @@ namespace std { return (size_t)k.obj; } }; + + template + struct hash> { + size_t operator()(const salticidae::BoxObj &k) const { + return (size_t)k.obj; + } + }; } #endif -- cgit v1.2.3