From d29a025ec587ed8784c2992898ae2cd92cebcaa6 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Thu, 3 Jul 2025 09:10:31 +0200 Subject: [PATCH] Properly constexpred and noexcepted BoxedObject. --- source/mijin/container/boxed_object.hpp | 80 ++++++++++++------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/source/mijin/container/boxed_object.hpp b/source/mijin/container/boxed_object.hpp index d4bb191..f9365b7 100644 --- a/source/mijin/container/boxed_object.hpp +++ b/source/mijin/container/boxed_object.hpp @@ -57,8 +57,8 @@ private: bool constructed = false; #endif public: - BoxedObject() noexcept : placeholder_() {}; - explicit BoxedObject(T object) + constexpr BoxedObject() MIJIN_NOEXCEPT : placeholder_() {}; + explicit constexpr BoxedObject(T object) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v) #if MIJIN_BOXED_OBJECT_DEBUG : constructed(true) #endif @@ -69,7 +69,7 @@ public: BoxedObject(BoxedObject&&) = delete; #if MIJIN_BOXED_OBJECT_DEBUG - ~BoxedObject() + constexpr ~BoxedObject() noexcept { MIJIN_ASSERT(!constructed, "BoxedObject::~BoxedObject(): Object has not been destroyed prior to destructor!"); } @@ -78,13 +78,13 @@ public: BoxedObject& operator=(const BoxedObject&) = delete; BoxedObject& operator=(BoxedObject&&) = delete; - T& operator*() noexcept { return get(); } - const T& operator*() const noexcept { return get(); } - T* operator->() noexcept { return &get(); } - const T* operator->() const noexcept { return &get(); } + constexpr T& operator*() noexcept { return get(); } + constexpr const T& operator*() const noexcept { return get(); } + constexpr T* operator->() noexcept { return &get(); } + constexpr const T* operator->() const noexcept { return &get(); } template - void construct(TArgs&&... args) + constexpr void construct(TArgs&&... args) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v)) { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(!constructed, "BoxedObject::construct(): Attempt to construct an already constructed object!"); @@ -93,7 +93,7 @@ public: std::construct_at(&object_, std::forward(args)...); } - void destroy() + constexpr void destroy() MIJIN_NOEXCEPT { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(constructed, "BoxedObject::destroy(): Attempt to destroy a not constructed object!"); @@ -101,8 +101,8 @@ public: #endif std::destroy_at(&object_); } - - void copyTo(BoxedObject& other) const + + constexpr void copyTo(BoxedObject& other) const MIJIN_NOEXCEPT_IF(std::is_nothrow_copy_constructible_v) { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!"); @@ -110,7 +110,7 @@ public: other.construct(object_); } - void moveTo(BoxedObject& other) + constexpr void moveTo(BoxedObject& other) MIJIN_NOEXCEPT_IF(std::is_nothrow_move_constructible_v) { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(constructed, "BoxedObject::copy(): Attempt to copy a not constructed object!"); @@ -119,7 +119,7 @@ public: destroy(); } - [[nodiscard]] T& get() + [[nodiscard]] constexpr T& get() MIJIN_NOEXCEPT { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!"); @@ -127,7 +127,7 @@ public: return object_; } - [[nodiscard]] const T& get() const + [[nodiscard]] constexpr const T& get() const MIJIN_NOEXCEPT { #if MIJIN_BOXED_OBJECT_DEBUG MIJIN_ASSERT(constructed, "BoxedObject::get(): Attempt to access a not constructed object!"); @@ -149,38 +149,38 @@ private: BoxedObject* end_ = nullptr; #endif public: - BoxedObjectIterator() = default; - explicit constexpr BoxedObjectIterator(BoxedObject* box, BoxedObject* start, BoxedObject* end) + BoxedObjectIterator() noexcept = default; + explicit constexpr BoxedObjectIterator(BoxedObject* box, BoxedObject* start, BoxedObject* end) MIJIN_NOEXCEPT : box_(box) #if MIJIN_CHECKED_ITERATORS , start_(start), end_(end) #endif {} - BoxedObjectIterator(const BoxedObjectIterator&) = default; - BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default; + constexpr BoxedObjectIterator(const BoxedObjectIterator&) noexcept = default; + constexpr BoxedObjectIterator(BoxedObjectIterator&&) noexcept = default; - BoxedObjectIterator& operator=(const BoxedObjectIterator&) = default; - BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default; - BoxedObjectIterator& operator+=(difference_type diff); - BoxedObjectIterator& operator-=(difference_type diff) { return (*this += -diff); } + constexpr BoxedObjectIterator& operator=(const BoxedObjectIterator&) noexcept = default; + constexpr BoxedObjectIterator& operator=(BoxedObjectIterator&&) noexcept = default; + constexpr BoxedObjectIterator& operator+=(difference_type diff) MIJIN_NOEXCEPT; + constexpr BoxedObjectIterator& operator-=(difference_type diff) MIJIN_NOEXCEPT { return (*this += -diff); } constexpr auto operator<=>(const BoxedObjectIterator& other) const noexcept = default; - [[nodiscard]] T& operator*() const; - [[nodiscard]] T* operator->() const; - BoxedObjectIterator& operator++(); - BoxedObjectIterator operator++(int); - BoxedObjectIterator& operator--(); - BoxedObjectIterator operator--(int); + [[nodiscard]] constexpr T& operator*() const MIJIN_NOEXCEPT; + [[nodiscard]] constexpr T* operator->() const MIJIN_NOEXCEPT; + constexpr BoxedObjectIterator& operator++() MIJIN_NOEXCEPT; + constexpr BoxedObjectIterator operator++(int) MIJIN_NOEXCEPT; + constexpr BoxedObjectIterator& operator--() MIJIN_NOEXCEPT; + constexpr BoxedObjectIterator operator--(int) MIJIN_NOEXCEPT; - [[nodiscard]] difference_type operator-(const BoxedObjectIterator& other) const { return box_ - other.box_; } - [[nodiscard]] BoxedObjectIterator operator+(difference_type diff) const; - [[nodiscard]] BoxedObjectIterator operator-(difference_type diff) const { return (*this + -diff); } + [[nodiscard]] constexpr difference_type operator-(const BoxedObjectIterator& other) const MIJIN_NOEXCEPT { return box_ - other.box_; } + [[nodiscard]] constexpr BoxedObjectIterator operator+(difference_type diff) const MIJIN_NOEXCEPT; + [[nodiscard]] constexpr BoxedObjectIterator operator-(difference_type diff) const MIJIN_NOEXCEPT { return (*this + -diff); } - [[nodiscard]] T& operator[](difference_type diff) const { return *(*this + diff); } + [[nodiscard]] T& operator[](difference_type diff) const MIJIN_NOEXCEPT { return *(*this + diff); } }; template -inline BoxedObjectIterator operator+(std::iter_difference_t diff, const BoxedObjectIterator& iter) { +constexpr BoxedObjectIterator operator+(std::iter_difference_t diff, const BoxedObjectIterator& iter) MIJIN_NOEXCEPT { return iter + diff; } static_assert(std::random_access_iterator>); @@ -190,7 +190,7 @@ static_assert(std::random_access_iterator>); // template -BoxedObjectIterator& BoxedObjectIterator::operator+=(difference_type diff) +constexpr BoxedObjectIterator& BoxedObjectIterator::operator+=(difference_type diff) MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(diff <= (end_ - box_) && diff >= (box_ - start_), "BoxedObjectIterator::operator+=(): Attempt to iterate out of range."); @@ -200,7 +200,7 @@ BoxedObjectIterator& BoxedObjectIterator::operator+=(difference_type diff) } template -T& BoxedObjectIterator::operator*() const +constexpr T& BoxedObjectIterator::operator*() const MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(box_ >= start_ && box_ < end_, "BoxedObjectIterator::operator->(): Attempt to dereference out-of-range iterator."); @@ -209,7 +209,7 @@ T& BoxedObjectIterator::operator*() const } template -BoxedObjectIterator& BoxedObjectIterator::operator++() +constexpr BoxedObjectIterator& BoxedObjectIterator::operator++() MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(): Attempt to iterator past the end."); @@ -218,7 +218,7 @@ BoxedObjectIterator& BoxedObjectIterator::operator++() } template -BoxedObjectIterator BoxedObjectIterator::operator++(int) +constexpr BoxedObjectIterator BoxedObjectIterator::operator++(int) MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(box_ < end_, "BoxedObjectIterator::operator++(int): Attempt to iterator past the end."); @@ -229,7 +229,7 @@ BoxedObjectIterator BoxedObjectIterator::operator++(int) } template -BoxedObjectIterator& BoxedObjectIterator::operator--() +constexpr BoxedObjectIterator& BoxedObjectIterator::operator--() MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(): Attempt to iterator past start."); @@ -238,7 +238,7 @@ BoxedObjectIterator& BoxedObjectIterator::operator--() } template -BoxedObjectIterator BoxedObjectIterator::operator--(int) +constexpr BoxedObjectIterator BoxedObjectIterator::operator--(int) MIJIN_NOEXCEPT { #if MIJIN_CHECKED_ITERATORS MIJIN_ASSERT(box_ > start_, "BoxedObjectIterator::operator--(int): Attempt to iterator past start."); @@ -249,7 +249,7 @@ BoxedObjectIterator BoxedObjectIterator::operator--(int) } template -BoxedObjectIterator BoxedObjectIterator::operator+(difference_type diff) const +constexpr BoxedObjectIterator BoxedObjectIterator::operator+(difference_type diff) const MIJIN_NOEXCEPT { BoxedObjectIterator copy(*this); copy += diff;