aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/util/OptionalRef.h')
-rw-r--r--src/crepe/util/OptionalRef.h47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h
index 1ad3a6d..1b2cb3f 100644
--- a/src/crepe/util/OptionalRef.h
+++ b/src/crepe/util/OptionalRef.h
@@ -12,20 +12,46 @@ namespace crepe {
template <typename T>
class OptionalRef {
public:
+ //! Initialize empty (nonexistant) reference
OptionalRef() = default;
- OptionalRef(T &);
- OptionalRef<T> & operator=(T &);
+ //! 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 (cast)
+ *
+ * \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 Retrieve this reference (member access)
+ *
+ * \returns Internal reference if it is set
+ *
+ * \throws std::runtime_error if this function is called while the reference it not set
+ */
+ T * operator->() 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;
- void set(T &) noexcept;
- T & get() const;
+ /**
+ * \brief Make this reference empty
+ */
void clear() noexcept;
- OptionalRef(const OptionalRef<T> &);
- OptionalRef(OptionalRef<T> &&);
- OptionalRef<T> & operator=(const OptionalRef<T> &);
- OptionalRef<T> & operator=(OptionalRef<T> &&);
-
private:
/**
* \brief Reference to the value of type \c T
@@ -35,7 +61,6 @@ private:
T * ref = nullptr;
};
-}
+} // namespace crepe
#include "OptionalRef.hpp"
-