aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.hpp
blob: 5e36b3a5b5058bbd44d31022ad28c1e172beefdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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>
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->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