33 Optional(
const T &value) { innerValue = std::unique_ptr<T>(
new T(value)); }
39 Optional(T &&value) { innerValue = std::unique_ptr<T>(
new T(std::move(value))); }
45 bool hasValue()
const {
return innerValue !=
nullptr; }
54 T *innerPtr = innerValue.get();
55 if (innerPtr !=
nullptr)
58 throw std::runtime_error(
"Can't extract a reference from a null pointer");
68 T *innerPtr = innerValue.get();
69 if (innerPtr !=
nullptr)
72 throw std::runtime_error(
"Can't extract a reference from a null pointer");
79 void set(
const T &value) { innerValue.reset(
new T(value)); }
85 void set(T &&value) { innerValue.reset(
new T(value)); }
97 innerValue.reset(
new T(value.
get()));
104 void clear() { innerValue.reset(); }
107 std::unique_ptr<T> innerValue =
nullptr;
bool hasValue() const
Returns true if the optional object has a value.
Definition: Optional.h:45
T & get()
Gets the inner object.
Definition: Optional.h:52
Optional(T &&value)
Construct an Optional object initialized with the given value.
Definition: Optional.h:39
Optional(const T &value)
Construct an Optional object initialized with the given value.
Definition: Optional.h:33
A class that wraps a value that can be null.
Definition: Optional.h:21
Optional()
Construct an empty Optional object.
Definition: Optional.h:27
Optional< T > & operator=(const Optional< T > &value)
Assignment operator with copy semantics.
Definition: Optional.h:92
void clear()
Removes the inner object, making the optional void.
Definition: Optional.h:104