blob: 0a94ae564c08e24fabcada098e1a9071ec198897 (
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#pragma once
namespace crepe {
/**
* \brief Optional reference utility
*
* This class doesn't need to know the full definition of \c T to be used.
*
* \tparam T Value type
*/
template <typename T>
class OptionalRef {
public:
//! Initialize empty (nonexistant) reference
OptionalRef() = default;
//! Initialize reference with value
OptionalRef(T & ref);
/**
* \brief Assign new reference
*
* \param ref Reference to assign
*
* \return Reference to this (required for operator)
*/
OptionalRef<T> & operator=(T & ref);
/**
* \brief Check if this reference is not empty
*
* \returns `true` if reference is set, or `false` if it is not
*/
explicit operator bool() const noexcept;
/**
* \brief Assign new reference
*
* \param ref Reference to assign
*/
void set(T &) noexcept;
/**
* \brief Retrieve this reference
*
* \returns Internal reference if it is set
*
* \throws std::runtime_error if this function is called while the reference it not set
*/
T & get() const;
/**
* \brief Make this reference empty
*/
void clear() noexcept;
//! Copy constructor
OptionalRef(const OptionalRef<T> &);
//! Move constructor
OptionalRef(OptionalRef<T> &&);
//! Copy assignment
OptionalRef<T> & operator=(const OptionalRef<T> &);
//! Move assignment
OptionalRef<T> & operator=(OptionalRef<T> &&);
private:
/**
* \brief Reference to the value of type \c T
*
* \note This raw pointer is *not* managed, and only used as a reference!
*/
T * ref = nullptr;
};
} // namespace crepe
#include "OptionalRef.hpp"
|