aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-18 18:02:09 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-18 18:02:09 +0100
commit8d78727d6e7badca16ba7a1328643928a0039569 (patch)
tree3b0bab3c1a800f74e9f80292c9b5b7b3955ad999 /src/crepe/util/OptionalRef.hpp
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
move utilities from loek/audio
Diffstat (limited to 'src/crepe/util/OptionalRef.hpp')
-rw-r--r--src/crepe/util/OptionalRef.hpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp
new file mode 100644
index 0000000..7b201b0
--- /dev/null
+++ b/src/crepe/util/OptionalRef.hpp
@@ -0,0 +1,67 @@
+#pragma once
+
+#include <stdexcept>
+
+#include "OptionalRef.h"
+
+namespace crepe {
+
+template <typename T>
+OptionalRef<T>::OptionalRef(T & ref) {
+ this->set(ref);
+}
+
+template <typename T>
+OptionalRef<T>::OptionalRef(const OptionalRef<T> & other) {
+ this->ref = other.ref;
+}
+
+template <typename T>
+OptionalRef<T>::OptionalRef(OptionalRef<T> && other) {
+ this->ref = other.ref;
+ other.clear();
+}
+
+template <typename T>
+OptionalRef<T> & OptionalRef<T>::operator=(const OptionalRef<T> & other) {
+ this->ref = other.ref;
+ return *this;
+}
+
+template <typename T>
+OptionalRef<T> & OptionalRef<T>::operator=(OptionalRef<T> && other) {
+ this->ref = other.ref;
+ other.clear();
+ return *this;
+}
+
+template <typename T>
+T & OptionalRef<T>::get() 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);
+ return *this;
+}
+
+template <typename T>
+OptionalRef<T>::operator bool() const noexcept {
+ return this->ref != nullptr;
+}
+
+}
+