aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 10:41:08 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-21 10:41:08 +0100
commit5134bebc19c46e4e07a5ec3af1d3f3d2d17a86dd (patch)
tree837daf5b5de189d557b6ae8eaed149354a3b5030 /src/crepe/util/OptionalRef.h
parent70b1bf50de703330436f2ae9cb103fe33cbb567e (diff)
parent115d6f50152dc018073345800ca90b85846ebaa9 (diff)
merge `master` into `loek/scripts`
Diffstat (limited to 'src/crepe/util/OptionalRef.h')
-rw-r--r--src/crepe/util/OptionalRef.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h
new file mode 100644
index 0000000..3201667
--- /dev/null
+++ b/src/crepe/util/OptionalRef.h
@@ -0,0 +1,58 @@
+#pragma once
+
+namespace crepe {
+
+/**
+ * \brief Optional reference utility
+ *
+ * This class doesn't need to know the full definition of \c T to be used.
+ *
+ * \tparam T Value type
+ */
+template <typename T>
+class OptionalRef {
+public:
+ //! Initialize empty (nonexistant) reference
+ OptionalRef() = default;
+ //! Initialize reference with value
+ OptionalRef(T & ref);
+ /**
+ * \brief Assign new reference
+ *
+ * \param ref Reference to assign
+ *
+ * \return Reference to this (required for operator)
+ */
+ OptionalRef<T> & operator=(T & ref);
+ /**
+ * \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
+ */
+ 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
+ */
+ void clear() noexcept;
+
+private:
+ /**
+ * \brief Reference to the value of type \c T
+ *
+ * \note This raw pointer is *not* managed, and only used as a reference!
+ */
+ T * ref = nullptr;
+};
+
+} // namespace crepe
+
+#include "OptionalRef.hpp"