From 37670dca2c46212c7d7be96d6312cc1449833ab9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 17 Aug 2019 13:44:46 -0400 Subject: [PATCH] BooObject: Make SObjToken interface noexcept where applicable Now that decrement() doesn't lock a mutex every time its executed, we can mark it as noexcept. This allows us to make most of the interface noexcept. In particular, the move constructor and move assignment operator can now be noexcept. This allows all Boo objects to play nicely with interfaces that may make use of std::move_if_noexcept, like some of the standard library facilities, without always taking the copy constructor/copy assignment. --- include/boo/BooObject.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/boo/BooObject.hpp b/include/boo/BooObject.hpp index d82e864..a819836 100644 --- a/include/boo/BooObject.hpp +++ b/include/boo/BooObject.hpp @@ -11,8 +11,8 @@ protected: virtual ~IObj() = default; public: - void increment() { m_refCount.fetch_add(1, std::memory_order_relaxed); } - void decrement() { + void increment() noexcept { m_refCount.fetch_add(1, std::memory_order_relaxed); } + void decrement() noexcept { if (m_refCount.fetch_sub(1, std::memory_order_release) == 1) { std::atomic_thread_fence(std::memory_order_acquire); delete this; @@ -25,17 +25,17 @@ class ObjToken { SubCls* m_obj = nullptr; public: - ObjToken() = default; - ObjToken(SubCls* obj) : m_obj(obj) { + ObjToken() noexcept = default; + ObjToken(SubCls* obj) noexcept : m_obj(obj) { if (m_obj) m_obj->increment(); } - ObjToken(const ObjToken& other) : m_obj(other.m_obj) { + ObjToken(const ObjToken& other) noexcept : m_obj(other.m_obj) { if (m_obj) m_obj->increment(); } - ObjToken(ObjToken&& other) : m_obj(other.m_obj) { other.m_obj = nullptr; } - ObjToken& operator=(SubCls* obj) { + ObjToken(ObjToken&& other) noexcept : m_obj(other.m_obj) { other.m_obj = nullptr; } + ObjToken& operator=(SubCls* obj) noexcept { if (m_obj) m_obj->decrement(); m_obj = obj; @@ -43,7 +43,7 @@ public: m_obj->increment(); return *this; } - ObjToken& operator=(const ObjToken& other) { + ObjToken& operator=(const ObjToken& other) noexcept { if (m_obj) m_obj->decrement(); m_obj = other.m_obj; @@ -51,26 +51,26 @@ public: m_obj->increment(); return *this; } - ObjToken& operator=(ObjToken&& other) { + ObjToken& operator=(ObjToken&& other) noexcept { if (m_obj) m_obj->decrement(); m_obj = other.m_obj; other.m_obj = nullptr; return *this; } - ~ObjToken() { + ~ObjToken() noexcept { if (m_obj) m_obj->decrement(); } - SubCls* get() const { return m_obj; } - SubCls* operator->() const { return m_obj; } - SubCls& operator*() const { return *m_obj; } + SubCls* get() const noexcept { return m_obj; } + SubCls* operator->() const noexcept { return m_obj; } + SubCls& operator*() const noexcept { return *m_obj; } template - T* cast() const { + T* cast() const noexcept { return static_cast(m_obj); } - operator bool() const { return m_obj != nullptr; } - void reset() { + operator bool() const noexcept { return m_obj != nullptr; } + void reset() noexcept { if (m_obj) m_obj->decrement(); m_obj = nullptr;