aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 09:29:15 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 09:29:15 +0100
commit9d66b6cfccd15a1600c30af5e744e8b0710eeae6 (patch)
tree16db30a36f8802bc5003f64cacc1e596b6f472a2
parentf7a1ca3b7d934043b48602601e789bad27940405 (diff)
remove OptionalRef::set and OptionalRef::get
-rw-r--r--src/crepe/util/OptionalRef.h22
-rw-r--r--src/crepe/util/OptionalRef.hpp21
-rw-r--r--src/test/OptionalRefTest.cpp42
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
@@ -25,26 +25,20 @@ public:
*/
OptionalRef<T> & 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
*
* \returns Internal reference if it is set
*
* \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 <typename T>
OptionalRef<T>::OptionalRef(T & ref) {
- this->set(ref);
+ this->ref = &ref;
}
template <typename T>
-T & OptionalRef<T>::get() const {
+OptionalRef<T>::operator T & () const {
if (this->ref == nullptr)
throw std::runtime_error("OptionalRef: attempt to dereference nullptr");
return *this->ref;
}
template <typename T>
-void OptionalRef<T>::set(T & ref) noexcept {
- this->ref = &ref;
-}
-
-template <typename T>
-void OptionalRef<T>::clear() noexcept {
- this->ref = nullptr;
-}
-
-template <typename T>
OptionalRef<T> & OptionalRef<T>::operator=(T & ref) {
- this->set(ref);
+ this->ref = &ref;
return *this;
}
@@ -39,4 +29,9 @@ OptionalRef<T>::operator bool() const noexcept {
return this->ref != nullptr;
}
+template <typename T>
+void OptionalRef<T>::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<string> ref;
- EXPECT_FALSE(ref);
- ASSERT_THROW(ref.get(), runtime_error);
+ OptionalRef<string> 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<string> ref = value;
- EXPECT_TRUE(ref);
- ASSERT_NO_THROW(ref.get());
+ OptionalRef<string> 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<string> ref1 = value;
+ OptionalRef<string> ref2 = ref1;
+
+ EXPECT_TRUE(ref2);
+ string & value_ref = ref2;
+ EXPECT_EQ(value_ref, value);
+ value_ref = "bar";
+ EXPECT_EQ(value_ref, value);
}
+