optional.hpp
Go to the documentation of this file.
1 // Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #ifndef _FASTCDR_XCDR_OPTIONAL_HPP_
16 #define _FASTCDR_XCDR_OPTIONAL_HPP_
17 
18 #include <new>
19 #include <utility>
20 
21 #include "detail/optional.hpp"
22 #include "../exceptions/BadOptionalAccessException.hpp"
23 
24 namespace eprosima {
25 namespace fastcdr {
26 
28 struct nullopt_t
29 {
30  constexpr explicit nullopt_t(
31  int)
32  {
33  }
34 
35 };
36 
40 static constexpr nullopt_t nullopt {0};
41 
45 template<class T>
46 class optional
47 {
48 public:
49 
50  using type = T;
51 
53  optional() = default;
54 
57  const T& val) noexcept
58  {
59  ::new(&storage_.val_)T(val);
60  storage_.engaged_ = true;
61  }
62 
65  T&& val) noexcept
66  {
67  ::new(&storage_.val_)T(std::move(val));
68  storage_.engaged_ = true;
69  }
70 
73  const optional<T>& val) noexcept
74  {
75  ::new(&storage_.val_)T(val.storage_.val_);
76  storage_.engaged_ = val.storage_.engaged_;
77  }
78 
81  optional<T>&& val) noexcept
82  {
83  ::new(&storage_.val_)T(std::move(val.storage_.val_));
84  storage_.engaged_ = val.storage_.engaged_;
85  }
86 
88  ~optional() = default;
89 
95  template<class ... Args> void emplace(
96  Args&&... _args)
97  {
98  reset();
99  storage_.val_.T(std::forward<Args>(_args)...);
100  storage_.engaged_ = true;
101  }
102 
109  void reset(
110  bool initial_engaged = false)
111  {
112  if (storage_.engaged_)
113  {
114  storage_.val_.~T();
115  }
116  storage_.engaged_ = initial_engaged;
117  if (storage_.engaged_)
118  {
119  ::new(&storage_.val_)T();
120  }
121  }
122 
129  T& value()&
130  {
131  if (!storage_.engaged_)
132  {
135  }
136 
137  return storage_.val_;
138  }
139 
146  const T& value() const&
147  {
148  if (!storage_.engaged_)
149  {
152  }
153 
154  return storage_.val_;
155  }
156 
163  T&& value() &&
164  {
165  if (!storage_.engaged_)
166  {
169  }
170 
171  return std::move(storage_.val_);
172  }
173 
180  const T&& value() const&&
181  {
182  if (!storage_.engaged_)
183  {
186  }
187 
188  return std::move(storage_.val_);
189  }
190 
196  bool has_value() const
197  {
198  return storage_.engaged_;
199  }
200 
203  const optional& opt)
204  {
205  reset();
207  if (opt.storage_.engaged_)
208  {
209  ::new(&storage_.val_)T(opt.storage_.val_);
210  }
211  return *this;
212  }
213 
216  optional&& opt)
217  {
218  reset();
219  storage_.engaged_ = opt.storage_.engaged_;
220  if (opt.storage_.engaged_)
221  {
222  ::new(&storage_.val_)T(std::move(opt.storage_.val_));
223  }
224  return *this;
225  }
226 
229  const T& val)
230  {
231  reset();
232  ::new(&storage_.val_)T(val);
233  storage_.engaged_ = true;
234  return *this;
235  }
236 
239  T&& val)
240  {
241  reset();
242  ::new(&storage_.val_)T(std::move(val));
243  storage_.engaged_ = true;
244  return *this;
245  }
246 
249  nullopt_t) noexcept
250  {
251  reset();
252  return *this;
253  }
254 
257  const optional& opt_val) const
258  {
259  return opt_val.storage_.engaged_ == storage_.engaged_ &&
260  (storage_.engaged_ ? opt_val.storage_.val_ == storage_.val_ : true);
261  }
262 
265  const optional& opt_val) const
266  {
267  return !operator ==(opt_val);
268  }
269 
277  T& operator *() & noexcept
278  {
279  return storage_.val_;
280  }
281 
289  const T& operator *() const& noexcept
290  {
291  return storage_.val_;
292  }
293 
301  T&& operator *() && noexcept
302  {
303  return std::move(storage_.val_);
304  }
305 
313  const T&& operator *() const&& noexcept
314  {
315  return std::move(storage_.val_);
316  }
317 
325  T* operator ->() noexcept
326  {
327  return std::addressof(storage_.val_);
328  }
329 
337  const T* operator ->() const noexcept
338  {
339  return std::addressof(storage_.val_);
340  }
341 
343  explicit operator bool() const noexcept
344  {
345  return storage_.engaged_;
346  }
347 
348 private:
349 
351 };
352 
353 } // namespace fastcdr
354 } // namespace eprosima
355 
356 #endif //_FASTCDR_XCDR_OPTIONAL_HPP_
eprosima::fastcdr::optional::reset
void reset(bool initial_engaged=false)
Reset the state of the optional.
Definition: optional.hpp:109
eprosima::fastcdr::optional::has_value
bool has_value() const
Checks whether the optional contains a value.
Definition: optional.hpp:196
eprosima::fastcdr::optional::value
const T & value() const &
Returns the contained value.
Definition: optional.hpp:146
eprosima::fastcdr::optional::optional
optional(const T &val) noexcept
Copy constructor from an instance of the templated class.
Definition: optional.hpp:56
eprosima::fastcdr::exception::BadOptionalAccessException
This class is thrown as an exception when accessing the value of a null optional.
Definition: BadOptionalAccessException.hpp:27
eprosima::fastcdr::optional::operator=
optional & operator=(const optional &opt)
Assigns content from an optional.
Definition: optional.hpp:202
eprosima::fastcdr::detail::optional_storage::engaged_
bool engaged_
Definition: detail/optional.hpp:32
eprosima::fastcdr::exception::BadOptionalAccessException::BAD_OPTIONAL_ACCESS_MESSAGE_DEFAULT
static const Cdr_DllAPI char *const BAD_OPTIONAL_ACCESS_MESSAGE_DEFAULT
Default message used in the library.
Definition: BadOptionalAccessException.hpp:78
eprosima::fastcdr::detail::optional_storage< T >
eprosima::fastcdr::optional::operator==
bool operator==(const optional &opt_val) const
Compares optional values.
Definition: optional.hpp:256
eprosima::fastcdr::detail::optional_storage::val_
T val_
Definition: detail/optional.hpp:29
eprosima::fastcdr::nullopt_t
An empty class type used to indicate optional type with uninitialized state.
Definition: optional.hpp:28
eprosima::fastcdr::optional::operator->
T * operator->() noexcept
Accesses the contained value.
Definition: optional.hpp:325
eprosima::fastcdr::optional::~optional
~optional()=default
Destructor.
eprosima::fastcdr::optional::operator!=
bool operator!=(const optional &opt_val) const
Compares optional values.
Definition: optional.hpp:264
eprosima::fastcdr::nullopt_t::nullopt_t
constexpr nullopt_t(int)
Definition: optional.hpp:30
eprosima::fastcdr::optional::storage_
detail::optional_storage< T > storage_
Definition: optional.hpp:350
eprosima::fastcdr::optional::operator*
T & operator*() &noexcept
Accesses the contained value.
Definition: optional.hpp:277
eprosima::fastcdr::optional::emplace
void emplace(Args &&... _args)
Constructs the contained value in-place.
Definition: optional.hpp:95
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
eprosima::fastcdr::optional::type
T type
Definition: optional.hpp:50
eprosima::fastcdr::optional::optional
optional()=default
Default constructor.
eprosima::fastcdr::optional
This class template manages an optional contained value, i.e. a value that may or may not be present.
Definition: optional.hpp:46
eprosima::fastcdr::optional::optional
optional(T &&val) noexcept
Move constructor from an instance of the templated class.
Definition: optional.hpp:64
optional.hpp
eprosima::fastcdr::optional::optional
optional(const optional< T > &val) noexcept
Copy constructor.
Definition: optional.hpp:72
eprosima::fastcdr::optional::value
const T && value() const &&
Returns the contained value.
Definition: optional.hpp:180
eprosima::fastcdr::optional::optional
optional(optional< T > &&val) noexcept
Move constructor.
Definition: optional.hpp:80
eprosima
Definition: fixed_size_string.hpp:32
eprosima::fastcdr::nullopt
static constexpr nullopt_t nullopt
nullopt is a constant of type nullopt_t that is used to indicate optional type with uninitialized sta...
Definition: optional.hpp:40
eprosima::fastcdr::optional::value
T & value() &
Returns the contained value.
Definition: optional.hpp:129
eprosima::fastcdr::optional::value
T && value() &&
Returns the contained value.
Definition: optional.hpp:163


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45