Fixup shared_cptr, atomic_cptr

This commit is contained in:
Nekotekina 2020-01-20 19:08:31 +03:00
parent 1e7a02badb
commit 0147bc2c72

View file

@ -42,6 +42,9 @@ namespace stx
// Combined borrowed refcounter and object pointer // Combined borrowed refcounter and object pointer
mutable ptr_type m_val; mutable ptr_type m_val;
template <typename T, bool Const>
friend class atomic_cptr;
constexpr atomic_base() noexcept constexpr atomic_base() noexcept
: m_val(0) : m_val(0)
{ {
@ -130,7 +133,7 @@ namespace stx
long long fetch_add(long long value) long long fetch_add(long long value)
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
return _InterlockedExchangeAdd64(&m_ref_count, &value); return _InterlockedExchangeAdd64(&m_ref_count, value);
#else #else
return __atomic_fetch_add(&m_ref_count, value, __ATOMIC_SEQ_CST); return __atomic_fetch_add(&m_ref_count, value, __ATOMIC_SEQ_CST);
#endif #endif
@ -226,6 +229,9 @@ namespace stx
{ {
using cb = atomic_base; using cb = atomic_base;
protected:
using atomic_base::m_val;
public: public:
constexpr shared_cptr() noexcept constexpr shared_cptr() noexcept
: atomic_base() : atomic_base()
@ -338,7 +344,7 @@ namespace stx
std::conditional_t<Const, const T*, T*> get() const noexcept std::conditional_t<Const, const T*, T*> get() const noexcept
{ {
return &this->ptr_get<T>()->m_value; return &this->ptr_get<T>()->m_data;
} }
std::conditional_t<Const, const T&, T&> operator*() const noexcept std::conditional_t<Const, const T&, T&> operator*() const noexcept
@ -418,13 +424,13 @@ namespace stx
base load() const noexcept base load() const noexcept
{ {
base result; base result;
result->m_val = this->ref_load(); static_cast<cb&>(result).m_val = this->ref_load();
if (result) if (result)
{ {
// TODO: obtain max-1 and try to return as much as possible // TODO: obtain max-1 and try to return as much as possible
this->template ptr_get<T>()->fetch_add(1); this->template ptr_get<T>()->fetch_add(1);
this->ref_fix(result->m_val); this->ref_fix(static_cast<cb&>(result).m_val);
} }
return result; return result;
@ -437,7 +443,7 @@ namespace stx
base exchange(base value) noexcept base exchange(base value) noexcept
{ {
value->m_val = this->val_exchange(value->m_val); static_cast<cb&>(value).m_val = this->val_exchange(static_cast<cb&>(value).m_val);
return value; return value;
} }
@ -478,6 +484,8 @@ namespace stx
// } // }
// void atomic_op(); // void atomic_op();
using base::make;
}; };
template <typename T> template <typename T>