aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.hpp
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-21 20:35:09 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-21 20:35:09 +0100
commitad0dcad1f11d698abf71bf69fb0927c26298d253 (patch)
tree3e8a501a84682ca2b6c085a987666c9b777bc1c7 /src/crepe/util/OptionalRef.hpp
parentbdc81e355e5bee5d2a3e29346ba08f7bc55196ca (diff)
parent115d6f50152dc018073345800ca90b85846ebaa9 (diff)
Merge branch 'master' into niels/decoupling_pixel_and_pos
Diffstat (limited to 'src/crepe/util/OptionalRef.hpp')
-rw-r--r--src/crepe/util/OptionalRef.hpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp
new file mode 100644
index 0000000..4608c9e
--- /dev/null
+++ b/src/crepe/util/OptionalRef.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <stdexcept>
+
+#include "OptionalRef.h"
+
+namespace crepe {
+
+template <typename T>
+OptionalRef<T>::OptionalRef(T & ref) {
+ this->ref = &ref;
+}
+
+template <typename T>
+OptionalRef<T>::operator T &() const {
+ if (this->ref == nullptr)
+ throw std::runtime_error("OptionalRef: attempt to dereference nullptr");
+ return *this->ref;
+}
+
+template <typename T>
+OptionalRef<T> & OptionalRef<T>::operator=(T & ref) {
+ this->ref = &ref;
+ return *this;
+}
+
+template <typename T>
+OptionalRef<T>::operator bool() const noexcept {
+ return this->ref != nullptr;
+}
+
+template <typename T>
+void OptionalRef<T>::clear() noexcept {
+ this->ref = nullptr;
+}
+
+} // namespace crepe