diff options
Diffstat (limited to 'src/crepe/util/OptionalRef.hpp')
-rw-r--r-- | src/crepe/util/OptionalRef.hpp | 45 |
1 files changed, 11 insertions, 34 deletions
diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp index 7b201b0..5e36b3a 100644 --- a/src/crepe/util/OptionalRef.hpp +++ b/src/crepe/util/OptionalRef.hpp @@ -8,53 +8,26 @@ 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; + 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; +T * OptionalRef<T>::operator->() 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->set(ref); + this->ref = &ref; return *this; } @@ -63,5 +36,9 @@ OptionalRef<T>::operator bool() const noexcept { return this->ref != nullptr; } +template <typename T> +void OptionalRef<T>::clear() noexcept { + this->ref = nullptr; } +} // namespace crepe |