ipaddress 1.1.0
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1/**
2 * @file optional.hpp
3 * @brief Manages an optional contained value within a class template
4 * @author Vladimir Shaleev
5 * @copyright MIT License
6 *
7 * The `optional` class template is designed to handle situations where a value
8 * may or may not be present. It encapsulates a value in a way that does not require
9 * dynamic memory allocation, ensuring that the value, if present, is part of the
10 * `optional` object's footprint. This makes `optional` ideal for use as a return
11 * type for functions that may fail to return a value. An `optional` object can be
12 * contextually converted to `bool`, indicating whether a value is contained or not.
13 */
14
15#ifndef IPADDRESS_OPTIONAL_HPP
16#define IPADDRESS_OPTIONAL_HPP
17
18#include "config.hpp"
19
20namespace IPADDRESS_NAMESPACE {
21
22/**
23 * A template class to manage an optional contained value.
24 *
25 * The `optional` class template is often used to represent the outcome of a function
26 * that might not succeed. An `optional<T>` can either hold a value or be empty at any
27 * moment. When it does hold a value, that value is stored within the optional object
28 * itself, meaning there’s no need for separate dynamic memory allocation. If you
29 * check an `optional<T>` in a boolean context, such as in an if-statement, it will
30 * evaluate to true if there’s a value present, and false otherwise.
31 *
32 * @tparam T The type of the value to manage initialization state for.
33 */
34IPADDRESS_EXPORT template <typename T>
35class optional { // NOLINT(cppcoreguidelines-special-member-functions)
36public:
37 using value_type = T; /**< The type of the value to manage initialization state for */
38
39 /**
40 * Default constructor that constructs an `optional` object without a contained value.
41 */
42 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional() noexcept(noexcept(T())) = default;
43
44 /**
45 * Constructs an `optional` object that does not contain a value.
46 */
47 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional(std::nullptr_t) noexcept(noexcept(T())) {
48 }
49
50 /**
51 * Copy constructor that constructs an `optional` object with a contained value, initializing it with \a val.
52 *
53 * @param[in] val The value to copy into the `optional` object.
54 */
55 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional(const value_type& val) noexcept(noexcept(T(val)))
56 :
57 _has_value(true),
58 _value(val) {
59 }
60
61 /**
62 * Move constructor that constructs an `optional` object with a contained value, initializing it with \a val.
63 *
64 * @param[in,out] val The value to move into the `optional` object.
65 */
66 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional(value_type&& val) IPADDRESS_NOEXCEPT
67 :
68 _has_value(true),
69 _value(val) {
70 }
71
72 /**
73 * Copy constructor that constructs an `optional` object with a contained value, copying it from \a opt.
74 *
75 * @param[in] opt The `optional` object to copy the value from.
76 */
78 :
79 _has_value(opt._has_value),
80 _value(opt._value) {
81 }
82
83 /**
84 * Assignment operator that clears the contained value of the `optional` object.
85 *
86 * @return A reference to the `optional` object.
87 */
88 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional& operator=(std::nullptr_t) IPADDRESS_NOEXCEPT {
89 _has_value = false;
90 _value = value_type{};
91 return *this;
92 }
93
94 /**
95 * Move assignment operator that sets the contained value of the `optional` object to \a val.
96 *
97 * @param[in,out] val The value to move into the `optional` object.
98 * @return A reference to the `optional` object.
99 */
100 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE optional& operator=(value_type&& val) IPADDRESS_NOEXCEPT {
101 _has_value = true;
102 _value = val;
103 return *this;
104 }
105
106 /**
107 * Assigns the value and the state of existence from another `optional` object.
108 *
109 * @param[in] opt The `optional` object to copy from.
110 * @return A reference to `*this`.
111 */
113 if (this != &opt) {
114 _has_value = opt._has_value;
115 _value = opt._value;
116 }
117 return *this;
118 }
119
120 /**
121 * Pointer access to the contained value.
122 *
123 * Provides pointer access to the contained value. It is undefined behavior
124 * if the `optional` object does not contain a value.
125 *
126 * @return A pointer to the contained value.
127 */
129 return &_value;
130 }
131
132 /**
133 * Constant pointer access to the contained value.
134 *
135 * Provides constant pointer access to the contained value. It is undefined behavior
136 * if the `optional` object does not contain a value.
137 *
138 * @return A constant pointer to the contained value.
139 */
140 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const value_type* operator->() const IPADDRESS_NOEXCEPT {
141 return &_value;
142 }
143
144 /**
145 * Reference access to the contained value.
146 *
147 * Provides reference access to the contained value. It is undefined behavior
148 * if the `optional` object does not contain a value.
149 *
150 * @return A reference to the contained value.
151 */
153 return _value;
154 }
155
156 /**
157 * Constant reference access to the contained value.
158 *
159 * Provides constant reference access to the contained value. It is undefined behavior
160 * if the `optional` object does not contain a value.
161 *
162 * @return A constant reference to the contained value.
163 */
164 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const value_type& operator*() const IPADDRESS_NOEXCEPT {
165 return _value;
166 }
167
168 /**
169 * Checks existence of the contained value.
170 *
171 * Determines whether the `optional` object contains a value.
172 *
173 * @return `true` if a value is contained, otherwise `false`.
174 */
175 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE explicit operator bool() const IPADDRESS_NOEXCEPT {
176 return has_value();
177 }
178
179 /**
180 * Checks existence of the contained value.
181 *
182 * Determines whether the `optional` object contains a value.
183 *
184 * @return `true` if a value is contained, otherwise `false`.
185 */
187 return _has_value;
188 }
189
190 /**
191 * Reference access to the contained value.
192 *
193 * Provides reference access to the contained value. It is undefined behavior
194 * if the `optional` object does not contain a value.
195 *
196 * @return A reference to the contained value.
197 */
199 return _value;
200 }
201
202 /**
203 * Constant reference access to the contained value.
204 *
205 * Provides constant reference access to the contained value. It is undefined behavior
206 * if the `optional` object does not contain a value.
207 *
208 * @return A constant reference to the contained value.
209 */
210 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const value_type& value() const & IPADDRESS_NOEXCEPT {
211 return _value;
212 }
213
214 /**
215 * Move access to the contained value.
216 *
217 * Moves the contained value. It is undefined behavior if the `optional` object
218 * does not contain a value.
219 *
220 * @return The contained value, moved.
221 */
223 return std::move(_value);
224 }
225
226 /**
227 * Constant move access to the contained value.
228 *
229 * Moves the contained value, maintaining constness. It is undefined behavior if the `optional` object
230 * does not contain a value.
231 *
232 * @return The contained value, moved, as a constant.
233 */
234 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE const value_type&& value() const && IPADDRESS_NOEXCEPT {
235 return std::move(_value);
236 }
237
238private:
239 bool _has_value{};
240 value_type _value{};
241};
242
243} // namespace IPADDRESS_NAMESPACE
244
245#endif // IPADDRESS_OPTIONAL_HPP
A template class to manage an optional contained value.
Definition optional.hpp:35
constexpr inline value_type && value() &&noexcept
Move access to the contained value.
Definition optional.hpp:222
constexpr inline optional & operator=(const optional< T > &opt) noexcept
Assigns the value and the state of existence from another optional object.
Definition optional.hpp:112
constexpr inline const value_type && value() const &&noexcept
Constant move access to the contained value.
Definition optional.hpp:234
constexpr inline optional(value_type &&val) noexcept
Move constructor that constructs an optional object with a contained value, initializing it with val.
Definition optional.hpp:66
constexpr inline value_type & value() &noexcept
Reference access to the contained value.
Definition optional.hpp:198
constexpr inline optional() noexcept(noexcept(T()))=default
Default constructor that constructs an optional object without a contained value.
constexpr inline value_type & operator*() noexcept
Reference access to the contained value.
Definition optional.hpp:152
constexpr inline const value_type & operator*() const noexcept
Constant reference access to the contained value.
Definition optional.hpp:164
constexpr inline optional(std::nullptr_t) noexcept(noexcept(T()))
Constructs an optional object that does not contain a value.
Definition optional.hpp:47
constexpr inline value_type * operator->() noexcept
Pointer access to the contained value.
Definition optional.hpp:128
constexpr inline optional(const optional< T > &opt) noexcept
Copy constructor that constructs an optional object with a contained value, copying it from opt.
Definition optional.hpp:77
constexpr inline optional & operator=(value_type &&val) noexcept
Move assignment operator that sets the contained value of the optional object to val.
Definition optional.hpp:100
constexpr inline const value_type * operator->() const noexcept
Constant pointer access to the contained value.
Definition optional.hpp:140
constexpr inline optional(const value_type &val) noexcept(noexcept(T(val)))
Copy constructor that constructs an optional object with a contained value, initializing it with val.
Definition optional.hpp:55
constexpr inline optional & operator=(std::nullptr_t) noexcept
Assignment operator that clears the contained value of the optional object.
Definition optional.hpp:88
constexpr inline operator bool() const noexcept
Checks existence of the contained value.
Definition optional.hpp:175
constexpr inline const value_type & value() const &noexcept
Constant reference access to the contained value.
Definition optional.hpp:210
constexpr inline bool has_value() const noexcept
Checks existence of the contained value.
Definition optional.hpp:186
#define IPADDRESS_EXPORT
Definition config.hpp:42
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89