aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/OptionalRef.hpp
blob: 4608c9e501860f3ef54f689393c8ee7ae699f3e3 (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
#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