From b98265d732bad274f66de66cb93d891e9c41a112 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 12 Jul 2018 20:25:41 -0400 Subject: fix bugs --- include/salticidae/ref.h | 60 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 16 deletions(-) (limited to 'include/salticidae/ref.h') diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h index a1c5dc7..c0b77a3 100644 --- a/include/salticidae/ref.h +++ b/include/salticidae/ref.h @@ -89,7 +89,17 @@ class _BoxObj { return *this; } - operator T*() const { return obj; } + T &operator *() const { return *obj; } + T *get() const { return obj; } + operator bool() const { return obj != nullptr; } + bool operator==(const _BoxObj &other) const { return obj == other.obj; } + bool operator!=(const _BoxObj &other) const { return obj != other.obj; } + bool operator==(std::nullptr_t) const { return obj == nullptr; } + bool operator!=(std::nullptr_t) const { return obj != nullptr; } + bool operator<(const _BoxObj &other) const { return obj < other.obj; } + bool operator>(const _BoxObj &other) const { return obj > other.obj; } + bool operator<=(const _BoxObj &other) const { return obj <= other.obj; } + bool operator>=(const _BoxObj &other) const { return obj >= other.obj; } }; template> @@ -102,7 +112,7 @@ class BoxObj: public _BoxObj { BoxObj() = default; BoxObj(T *obj): base_t(obj) {} template::value>::value> + typename std::enable_if::value>::type> BoxObj(BoxObj &&other): base_t(other) {} T *operator->() const { return base_t::obj; } @@ -175,6 +185,7 @@ struct _ARCCtl { ~_ARCCtl() {} }; +template class _RcObjBase; template class RcObjBase; template @@ -182,9 +193,8 @@ class _WeakObjBase { T *obj; R *ctl; - template using ROB = RcObjBase; - friend class ROB; - friend std::hash<_WeakObjBase>; + template + friend class _RcObjBase; public: _WeakObjBase(): ctl(nullptr) {} @@ -223,7 +233,7 @@ class _WeakObjBase { } template - _WeakObjBase(const RcObjBase &other); + _WeakObjBase(const _RcObjBase &other); ~_WeakObjBase() { if (ctl) ctl->release_weak(); } }; @@ -231,22 +241,30 @@ class _WeakObjBase { template class WeakObjBase: public _WeakObjBase { + using base_t = _WeakObjBase; + friend std::hash; public: WeakObjBase() = default; WeakObjBase(const WeakObjBase &other) = default; WeakObjBase(WeakObjBase &&other) = default; template - WeakObjBase(const RcObjBase &other); + WeakObjBase(const RcObjBase &other): base_t(other) {} + WeakObjBase &operator=(const WeakObjBase &) = default; + WeakObjBase &operator=(WeakObjBase &&) = default; }; template class WeakObjBase: public _WeakObjBase { + using base_t = _WeakObjBase; + friend std::hash; public: WeakObjBase() = default; WeakObjBase(const WeakObjBase &other) = default; WeakObjBase(WeakObjBase &&other) = default; template - WeakObjBase(const RcObjBase &other); + WeakObjBase(const RcObjBase &other): base_t(other) {} + WeakObjBase &operator=(const WeakObjBase &) = default; + WeakObjBase &operator=(WeakObjBase &&) = default; }; @@ -257,13 +275,11 @@ class _RcObjBase { ptr_type obj; R *ctl; - friend WeakObjBase; - friend std::hash<_RcObjBase>; + friend _WeakObjBase; template friend class _RcObjBase; public: - operator T*() const { return obj; } _RcObjBase(): obj(nullptr), ctl(nullptr) {} _RcObjBase(ptr_type obj): obj(obj), ctl(new R()) {} _RcObjBase(BoxObj &&box_ref): obj(box_ref.obj), ctl(new R()) { @@ -315,7 +331,7 @@ class _RcObjBase { other.ctl = nullptr; } - _RcObjBase(const WeakObjBase &other) { + _RcObjBase(const _WeakObjBase &other) { if (other.ctl && other.ctl->ref_cnt) { obj = other.obj; @@ -335,11 +351,23 @@ class _RcObjBase { } size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; } + T &operator *() const { return *obj; } + T *get() const { return obj; } + operator bool() const { return obj != nullptr; } + bool operator==(const _RcObjBase &other) const { return obj == other.obj; } + bool operator!=(const _RcObjBase &other) const { return obj != other.obj; } + bool operator==(std::nullptr_t) const { return obj == nullptr; } + bool operator!=(std::nullptr_t) const { return obj != nullptr; } + bool operator<(const _RcObjBase &other) const { return obj < other.obj; } + bool operator>(const _RcObjBase &other) const { return obj > other.obj; } + bool operator<=(const _RcObjBase &other) const { return obj <= other.obj; } + bool operator>=(const _RcObjBase &other) const { return obj >= other.obj; } }; template> class RcObjBase: public _RcObjBase { using base_t = _RcObjBase; + friend std::hash; template friend RcObjBase static_pointer_cast(const RcObjBase &other); template @@ -352,16 +380,15 @@ class RcObjBase: public _RcObjBase { RcObjBase(BoxObj &&box_ref): base_t(std::move(box_ref)) {} template::value>::value> + typename = typename std::enable_if::value>::type> RcObjBase(const RcObjBase &other): base_t(other) {} template::value>::value> + typename = typename std::enable_if::value>::type> RcObjBase(RcObjBase &&other): base_t(std::move(other)) {} RcObjBase(const WeakObjBase &other): base_t(other) {} - RcObjBase(const RcObjBase &other) = default; RcObjBase(RcObjBase &&other) = default; RcObjBase &operator=(const RcObjBase &) = default; @@ -371,6 +398,7 @@ class RcObjBase: public _RcObjBase { template class RcObjBase: public _RcObjBase { using base_t = _RcObjBase; + friend std::hash; template friend RcObjBase static_pointer_cast(const RcObjBase &other); template @@ -419,7 +447,7 @@ RcObjBase static_pointer_cast(RcObjBase &&other) { template template -inline _WeakObjBase::_WeakObjBase(const RcObjBase &other): +inline _WeakObjBase::_WeakObjBase(const _RcObjBase &other): obj(other.obj), ctl(other.ctl) { if (ctl) ctl->add_weak(); } -- cgit v1.2.3