From 9d66b6cfccd15a1600c30af5e744e8b0710eeae6 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 21 Nov 2024 09:29:15 +0100 Subject: remove OptionalRef::set and OptionalRef::get --- src/crepe/util/OptionalRef.h | 22 ++++++++-------------- src/crepe/util/OptionalRef.hpp | 21 ++++++++------------- src/test/OptionalRefTest.cpp | 42 ++++++++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h index 57f9635..253bc07 100644 --- a/src/crepe/util/OptionalRef.h +++ b/src/crepe/util/OptionalRef.h @@ -24,19 +24,6 @@ public: * \return Reference to this (required for operator) */ OptionalRef & operator=(T & ref); - /** - * \brief Check if this reference is not empty - * - * \returns `true` if reference is set, or `false` if it is not - */ - explicit operator bool() const noexcept; - - /** - * \brief Assign new reference - * - * \param ref Reference to assign - */ - void set(T & ref) noexcept; /** * \brief Retrieve this reference * @@ -44,7 +31,14 @@ public: * * \throws std::runtime_error if this function is called while the reference it not set */ - T & get() const; + operator T & () const; + /** + * \brief Check if this reference is not empty + * + * \returns `true` if reference is set, or `false` if it is not + */ + explicit operator bool() const noexcept; + /** * \brief Make this reference empty */ diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp index 71e2a39..ae7c73e 100644 --- a/src/crepe/util/OptionalRef.hpp +++ b/src/crepe/util/OptionalRef.hpp @@ -8,29 +8,19 @@ namespace crepe { template OptionalRef::OptionalRef(T & ref) { - this->set(ref); + this->ref = &ref; } template -T & OptionalRef::get() const { +OptionalRef::operator T & () const { if (this->ref == nullptr) throw std::runtime_error("OptionalRef: attempt to dereference nullptr"); return *this->ref; } -template -void OptionalRef::set(T & ref) noexcept { - this->ref = &ref; -} - -template -void OptionalRef::clear() noexcept { - this->ref = nullptr; -} - template OptionalRef & OptionalRef::operator=(T & ref) { - this->set(ref); + this->ref = &ref; return *this; } @@ -39,4 +29,9 @@ OptionalRef::operator bool() const noexcept { return this->ref != nullptr; } +template +void OptionalRef::clear() noexcept { + this->ref = nullptr; +} + } // namespace crepe diff --git a/src/test/OptionalRefTest.cpp b/src/test/OptionalRefTest.cpp index 2072d56..1c69348 100644 --- a/src/test/OptionalRefTest.cpp +++ b/src/test/OptionalRefTest.cpp @@ -6,32 +6,42 @@ using namespace std; using namespace crepe; using namespace testing; -TEST(OptionalRefTest, Explicit) { +TEST(OptionalRefTest, Normal) { string value = "foo"; - OptionalRef ref; - EXPECT_FALSE(ref); - ASSERT_THROW(ref.get(), runtime_error); + OptionalRef ref = value; - ref.set(value); EXPECT_TRUE(ref); - ASSERT_NO_THROW(ref.get()); + ASSERT_NO_THROW({ + string & value_ref = ref; + EXPECT_EQ(value_ref, value); + }); ref.clear(); EXPECT_FALSE(ref); - ASSERT_THROW(ref.get(), runtime_error); + ASSERT_THROW({ + string & value_ref = ref; + }, runtime_error); } -TEST(OptionalRefTest, Implicit) { +TEST(OptionalRefTest, Empty) { string value = "foo"; - OptionalRef ref = value; - EXPECT_TRUE(ref); - ASSERT_NO_THROW(ref.get()); + OptionalRef ref; - ref.clear(); EXPECT_FALSE(ref); - ASSERT_THROW(ref.get(), runtime_error); + ASSERT_THROW({ + string & value_ref = ref; + }, runtime_error); +} - ref = value; - EXPECT_TRUE(ref); - ASSERT_NO_THROW(ref.get()); +TEST(OptionalRefTest, Chain) { + string value = "foo"; + OptionalRef ref1 = value; + OptionalRef ref2 = ref1; + + EXPECT_TRUE(ref2); + string & value_ref = ref2; + EXPECT_EQ(value_ref, value); + value_ref = "bar"; + EXPECT_EQ(value_ref, value); } + -- cgit v1.2.3