1 #ifndef DUNE_COMMON_STD_OPTIONAL_HH
2 #define DUNE_COMMON_STD_OPTIONAL_HH
10 #ifdef DUNE_HAVE_CXX_OPTIONAL
21 #ifdef DUNE_HAVE_CXX_OPTIONAL
24 using optional = std::optional< T >;
26 using nullopt_t = std::nullopt_t;
27 using in_place_t = std::in_place_t;
31 const std::nullopt_t nullopt = std::nullopt;
32 const std::in_place_t in_place = std::in_place;
35 using bad_optional_access = std::bad_optional_access;
73 :
public std::logic_error
101 constexpr
optional () noexcept : engaged_( false ) {}
106 std::enable_if_t< std::is_constructible< value_type, U&& >::value,
int > = 0,
107 std::enable_if_t< !std::is_convertible< U&&, value_type >::value,
int > = 0 >
109 : engaged_( true ),
value_( std::forward< U >(
value ) )
113 std::enable_if_t< std::is_constructible< value_type, U&& >::value,
int > = 0,
115 std::enable_if_t< not(std::is_same<std::decay_t<U>,
in_place_t>::
value),
int > = 0
118 : engaged_( true ),
value_( std::forward< U >(
value ) )
124 template<
class... Args >
126 : engaged_( true ),
value_( std::forward< Args >( args )... )
136 optional (
const optional &other ) noexcept( std::is_nothrow_copy_constructible< T >::value )
137 : engaged_( other.engaged_ )
140 rawEmplace(other.value_);
144 : engaged_( other.engaged_ )
147 rawEmplace(std::move( other.value_ ));
151 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
152 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
153 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
154 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
155 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
156 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
157 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
158 std::enable_if_t< !std::is_convertible< const U&, value_type >::value,
int > = 0 >
160 : engaged_( other.engaged_ )
167 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
168 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
169 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
170 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
171 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
172 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
173 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
174 std::enable_if_t< std::is_convertible< const U&, value_type >::value,
int > = 0 >
176 : engaged_( other.engaged_ )
183 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
184 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
185 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
186 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
187 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
188 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
189 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
190 std::enable_if_t< !std::is_convertible< const U&, value_type >::value,
int > = 0 >
192 : engaged_( other.engaged_ )
195 rawEmplace(std::move(other.value_));
199 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
200 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
201 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
202 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
203 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
204 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
205 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
206 std::enable_if_t< std::is_convertible< const U&, value_type >::value,
int > = 0 >
208 : engaged_( other.engaged_ )
211 rawEmplace(std::move(other.value_));
222 optional &
operator= (
const optional &other ) noexcept( std::is_nothrow_copy_constructible< T >::value && std::is_nothrow_copy_assignable< T >::value )
231 else if( other.engaged_ )
232 rawEmplace(other.value_);
233 engaged_ = other.engaged_;
237 optional &
operator= (
optional &&other ) noexcept( std::is_nothrow_move_constructible< T >::value && std::is_nothrow_move_assignable< T >::value )
242 value_ = std::move( other.value_ );
246 else if( other.engaged_ )
247 rawEmplace(std::move(other.value_));
248 engaged_ = other.engaged_;
252 template<
class U = value_type >
253 typename std::enable_if< std::is_constructible< value_type, U >::value && std::is_assignable< value_type, U >::value,
optional & >::type
259 rawEmplace(std::forward<U>(
value));
278 explicit constexpr
operator bool () const noexcept {
return engaged_; }
319 template<
class... Args >
325 rawEmplace(std::forward<Args>(args)...);
338 void swap (
optional &other ) noexcept( std::is_nothrow_move_constructible< T >::value && noexcept( std::swap( std::declval< T & >(), std::declval< T & >() ) ) )
340 std::swap( engaged_, other.engaged_ );
344 std::swap(
value_, other.value_ );
347 rawEmplace( std::move( other.value_ ) );
348 other.value_.~value_type();
351 else if( other.engaged_ )
353 other.rawEmplace( std::move(
value_ ) );
365 template<
class... Args>
366 void rawEmplace(Args&&... args)
370 new(
const_cast<std::remove_const_t<value_type>*
>(&
value_) )
value_type( std::forward< Args >( args )... );
385 return (lhs && rhs ? *lhs == *rhs :
static_cast< bool >( lhs ) ==
static_cast< bool >( rhs ));
392 return (rhs && (lhs ? std::less< T >()( *lhs, *rhs ) :
true));
417 return static_cast< bool >( rhs );
423 return (lhs && (*lhs == rhs));
429 return (rhs && (lhs == *rhs));
435 return (lhs ? std::less< T >()( *lhs, rhs ) :
true);
441 return (rhs ? std::less< T >()( lhs, *rhs ) :
false);
462 #ifndef DUNE_HAVE_CXX_OPTIONAL
481 struct hash<
Dune::Std::optional< T > >
485 return (arg ? std::hash< T >()( arg ) : 0);
Dune namespace.
Definition: alignedallocator.hh:14
static constexpr bool operator<(const optional< T > &lhs, const optional< T > &rhs)
Definition: optional.hh:390
static constexpr bool operator==(const optional< T > &lhs, const optional< T > &rhs)
Definition: optional.hh:383
static constexpr optional< typename std::decay< T >::type > make_optional(T &&value)
Definition: optional.hh:450
Definition: optional.hh:43
Definition: optional.hh:57
Definition: optional.hh:74
bad_optional_access(const char *what)
Definition: optional.hh:77
bad_optional_access(const std::string &what)
Definition: optional.hh:76
Definition: optional.hh:91
void emplace(Args &&... args)
Definition: optional.hh:320
constexpr optional(U &&value)
Definition: optional.hh:108
const value_type * operator->() const noexcept
pointer operator
Definition: optional.hh:286
optional(value_type &&value)
Definition: optional.hh:122
optional(const optional &other) noexcept(std::is_nothrow_copy_constructible< T >::value)
Definition: optional.hh:136
constexpr optional(nullopt_t) noexcept
Definition: optional.hh:103
value_type value_
Definition: optional.hh:374
optional & operator=(nullopt_t) noexcept
Definition: optional.hh:214
optional(const value_type &value)
Definition: optional.hh:121
constexpr optional(in_place_t, Args &&... args)
Definition: optional.hh:125
value_type & value()
Definition: optional.hh:298
optional(optional &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: optional.hh:143
value_type * operator->() noexcept
pointer operator
Definition: optional.hh:288
~optional()
Definition: optional.hh:266
void reset() noexcept
Definition: optional.hh:329
value_type value_or(U &&value) const
Definition: optional.hh:307
T value_type
type of value
Definition: optional.hh:94
const value_type & value() const
Definition: optional.hh:290
void swap(optional &other) noexcept(std::is_nothrow_move_constructible< T >::value &&noexcept(std::swap(std::declval< T & >(), std::declval< T & >())))
Definition: optional.hh:338
const value_type & operator*() const noexcept
dereference pointer
Definition: optional.hh:281
constexpr optional(U &&value)
Definition: optional.hh:117
optional(optional< U > &&other)
Definition: optional.hh:191
constexpr optional() noexcept
Definition: optional.hh:101
optional(const optional< U > &other)
Definition: optional.hh:159