Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
OptionalRef.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <type_traits>
5
7
8namespace storm {
9
15namespace optionalref_detail {
16template<class T>
17constexpr T& FUN(T& t) noexcept {
18 return t;
19}
20template<class T>
21void FUN(T&&) = delete;
22} // namespace optionalref_detail
23
29 constexpr explicit NullRefType(int) {}
30};
31inline constexpr NullRefType NullRef{0};
32
47template<class T>
49 static_assert(!std::is_reference_v<T>, "Type of OptionalRef should not be a reference type itself.");
50
51 public:
52 using type = T;
53
57 OptionalRef() : ptr(nullptr) {}
58
62 OptionalRef(NullRefType /*NullRef*/) : ptr(nullptr) {}
63
70 template<class U, class = decltype(optionalref_detail::FUN<T>(std::declval<U>()),
71 std::enable_if_t<!std::is_same_v<OptionalRef, std::remove_cv_t<std::remove_reference_t<U>>>>())>
72 constexpr OptionalRef(U&& u) noexcept(noexcept(optionalref_detail::FUN<T>(std::forward<U>(u))))
73 : ptr(std::addressof(optionalref_detail::FUN<T>(std::forward<U>(u)))) {}
74
78 OptionalRef(OptionalRef<T> const& other) = default;
79
83 OptionalRef(OptionalRef<T>&& other) = default;
84
88 OptionalRef& operator=(OptionalRef const& other) = delete;
89
93 operator bool() const {
94 return ptr != nullptr;
95 }
96
100 bool has_value() const {
101 return ptr != nullptr;
102 }
103
109 STORM_LOG_ASSERT(has_value(), "OptionalRef operator* called but no object is referenced by this.");
110 return *ptr;
111 }
112
117 T const& operator*() const {
118 STORM_LOG_ASSERT(has_value(), "OptionalRef operator* called but no object is referenced by this.");
119 return *ptr;
120 }
121
126 T& value() {
127 STORM_LOG_ASSERT(has_value(), "OptionalRef value() called but no object is referenced by this.");
128 return *ptr;
129 }
130
135 T const& value() const {
136 STORM_LOG_ASSERT(has_value(), "OptionalRef value called but no object is referenced by this.");
137 return *ptr;
138 }
139
143 T& value_or(T& defaultValue) {
144 return has_value() ? value() : defaultValue;
145 }
146
150 T const& value_or(T const& defaultValue) const {
151 return has_value() ? value() : defaultValue;
152 }
153
159 STORM_LOG_ASSERT(has_value(), "OptionalRef operator-> called but no object is referenced by this.");
160 return ptr;
161 }
162
167 T const* operator->() const {
168 STORM_LOG_ASSERT(has_value(), "OptionalRef operator-> called but no object is referenced by this.");
169 return ptr;
170 }
171
175 void reset() {
176 ptr = nullptr;
177 }
178
182 void reset(NullRefType const&) {
183 ptr = nullptr;
184 }
185
189 void reset(T& t) {
190 ptr = std::addressof(t);
191 }
192
193 private:
194 T* ptr;
195};
196
198template<class T>
200
201} // namespace storm
Helper class that optionally holds a reference to an object of type T.
Definition OptionalRef.h:48
T & value()
Accesses the contained reference (if any)
void reset(NullRefType const &)
Unsets the reference.
bool has_value() const
Yields true iff this contains a reference.
OptionalRef(NullRefType)
Creates a non-initialized reference.
Definition OptionalRef.h:62
T const & value_or(T const &defaultValue) const
Returns the contained reference (if any).
T & value_or(T &defaultValue)
Returns the contained reference (if any).
OptionalRef & operator=(OptionalRef const &other)=delete
Deleted assignment operator (see class description)
OptionalRef()
Creates a non-initialized reference.
Definition OptionalRef.h:57
T & operator*()
Accesses the contained reference (if any)
void reset(T &t)
Rebinds the reference.
T const * operator->() const
Yields a pointer to the referenced object (if any)
OptionalRef(OptionalRef< T > const &other)=default
Creates a copy of the given OptionalRef.
void reset()
Unsets the reference.
T const & value() const
Accesses the contained reference (if any)
T * operator->()
Yields a pointer to the referenced object (if any)
OptionalRef(OptionalRef< T > &&other)=default
Move constructs this OptionalRef from another one.
T const & operator*() const
Accesses the contained reference (if any)
constexpr OptionalRef(U &&u) noexcept(noexcept(optionalref_detail::FUN< T >(std::forward< U >(u))))
Creates a reference to the provided object.
Definition OptionalRef.h:72
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
constexpr T & FUN(T &t) noexcept
Definition OptionalRef.h:17
LabParser.cpp.
Definition cli.cpp:18
constexpr NullRefType NullRef
Definition OptionalRef.h:31
Auxiliary struct used to identify OptionalRefs that do not contain a reference.
Definition OptionalRef.h:28
constexpr NullRefType(int)
Definition OptionalRef.h:29