cgv
Loading...
Searching...
No Matches
optional.h
1#pragma once
2
3namespace cgv {
4namespace data {
5
21template<typename T>
22class optional {
23private:
25 T _value;
27 bool _has_value = false;
28
29public:
32
35 optional(T v) : _value(v), _has_value(true) {}
36
43 optional<T>& operator=(const T& v) {
44 _value = v;
45 _has_value = true;
46 return *this;
47 }
48
52 bool operator==(const optional<T>& rhs) const {
53 return _has_value && rhs.has_value() ? (_value == rhs.value()) : false;
54 }
55
57 explicit operator bool() const {
58 return _has_value;
59 }
60
64 void reset() {
65 _has_value = false;
66 }
67
70 bool has_value() const {
71 return _has_value;
72 }
73
80 T value() const {
81 return _value;
82 }
83};
84
85} // namespace data
86} // namespace cgv
A simple and naiive implementation of an optional value.
Definition optional.h:22
optional()
Construct without a contained value.
Definition optional.h:31
optional< T > & operator=(const T &v)
Assign a non-optional value of type T.
Definition optional.h:43
void reset()
Mark the optional as not containing a valid value.
Definition optional.h:64
optional(T v)
Construct with a contained value.
Definition optional.h:35
T value() const
Access the contained value.
Definition optional.h:80
bool has_value() const
Test if a value is contained.
Definition optional.h:70
bool operator==(const optional< T > &rhs) const
Compare with another optional of the same type T.
Definition optional.h:52
the cgv namespace
Definition print.h:11