18 #ifndef NONSTD_OPTIONAL_LITE_HPP 19 #define NONSTD_OPTIONAL_LITE_HPP 25 #define optional_lite_VERSION "2.0.0" 29 #ifndef optional_CONFIG_MAX_ALIGN_HACK 30 # define optional_CONFIG_MAX_ALIGN_HACK 0 33 #ifndef optional_CONFIG_ALIGN_AS 37 #ifndef optional_CONFIG_ALIGN_AS_FALLBACK 38 # define optional_CONFIG_ALIGN_AS_FALLBACK double 43 #define optional_CPP11_OR_GREATER ( __cplusplus >= 201103L ) 44 #define optional_CPP14_OR_GREATER ( __cplusplus >= 201402L ) 47 #define optional_BETWEEN( v, lo, hi ) ( lo <= v && v < hi ) 49 #if defined(_MSC_VER) && !defined(__clang__) 50 # define optional_COMPILER_MSVC_VERSION (_MSC_VER / 100 - 5 - (_MSC_VER < 1900)) 52 # define optional_COMPILER_MSVC_VERSION 0 56 # define optional_COMPILER_GNUC_VERSION __GNUC__ 58 # define optional_COMPILER_GNUC_VERSION 0 61 #if optional_BETWEEN(optional_COMPILER_MSVC_VERSION, 7, 14 ) 62 # pragma warning( push ) 63 # pragma warning( disable: 4345 ) // initialization behavior changed 66 #if optional_BETWEEN(optional_COMPILER_MSVC_VERSION, 7, 15 ) 67 # pragma warning( push ) 68 # pragma warning( disable: 4814 ) // in C++14 'constexpr' will not imply 'const' 73 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 10 74 # define optional_HAVE_AUTO 1 75 # define optional_HAVE_NULLPTR 1 76 # define optional_HAVE_STATIC_ASSERT 1 79 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 12 80 # define optional_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 1 81 # define optional_HAVE_INITIALIZER_LIST 1 84 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 14 85 # define optional_HAVE_ALIAS_TEMPLATE 1 86 # define optional_HAVE_CONSTEXPR_11 1 87 # define optional_HAVE_ENUM_CLASS 1 88 # define optional_HAVE_EXPLICIT_CONVERSION 1 89 # define optional_HAVE_IS_DEFAULT 1 90 # define optional_HAVE_IS_DELETE 1 91 # define optional_HAVE_NOEXCEPT 1 92 # define optional_HAVE_REF_QUALIFIER 1 97 #if optional_CPP14_OR_GREATER 98 # define optional_HAVE_CONSTEXPR_14 1 103 #if optional_COMPILER_GNUC_VERSION 104 # define optional_HAVE_TR1_TYPE_TRAITS 1 105 # define optional_HAVE_TR1_ADD_POINTER 1 108 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 9 109 # define optional_HAVE_TYPE_TRAITS 1 110 # define optional_HAVE_STD_ADD_POINTER 1 113 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 11 114 # define optional_HAVE_ARRAY 1 117 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 12 118 # define optional_HAVE_CONDITIONAL 1 121 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 14 || (optional_COMPILER_MSVC_VERSION >= 9 && _HAS_CPP0X) 122 # define optional_HAVE_CONTAINER_DATA_METHOD 1 125 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 12 126 # define optional_HAVE_REMOVE_CV 1 129 #if optional_CPP11_OR_GREATER || optional_COMPILER_MSVC_VERSION >= 14 130 # define optional_HAVE_SIZED_TYPES 1 135 #if optional_COMPILER_MSVC_VERSION >= 14 136 # undef optional_CPP11_OR_GREATER 137 # define optional_CPP11_OR_GREATER 1 142 #if optional_HAVE_CONSTEXPR_11 143 # define optional_constexpr constexpr 145 # define optional_constexpr 148 #if optional_HAVE_CONSTEXPR_14 149 # define optional_constexpr14 constexpr 151 # define optional_constexpr14 154 #if optional_HAVE_NOEXCEPT 155 # define optional_noexcept noexcept 157 # define optional_noexcept 160 #if optional_HAVE_NULLPTR 161 # define optional_nullptr nullptr 163 # define optional_nullptr NULL 166 #if optional_HAVE_REF_QUALIFIER 167 # define optional_ref_qual & 168 # define optional_refref_qual && 170 # define optional_ref_qual 171 # define optional_refref_qual 176 #if optional_HAVE_INITIALIZER_LIST 177 # include <initializer_list> 180 #if optional_HAVE_TYPE_TRAITS 181 # include <type_traits> 182 #elif optional_HAVE_TR1_TYPE_TRAITS 183 # include <tr1/type_traits> 190 #if ! nonstd_lite_HAVE_IN_PLACE_TYPES 197 struct in_place_type_tag {};
199 template< std::
size_t I >
200 struct in_place_index_tag {};
207 inline in_place_t in_place( detail::in_place_type_tag<T> = detail::in_place_type_tag<T>() )
212 template< std::
size_t I >
213 inline in_place_t in_place( detail::in_place_index_tag<I> = detail::in_place_index_tag<I>() )
220 #define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> ) 221 #define nonstd_lite_in_place_index_t(T) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<I> ) 223 #define nonstd_lite_HAVE_IN_PLACE_TYPES 1 227 #endif // nonstd_lite_HAVE_IN_PLACE_TYPES 233 namespace nonstd {
namespace optional_lite {
237 template<
typename T >
244 #if variant_HAVE_CONDITIONAL 246 using std::conditional;
250 template<
bool Cond,
class Then,
class Else >
253 template<
class Then,
class Else >
256 template<
class Then,
class Else >
259 #endif // variant_HAVE_CONDITIONAL 263 template<
typename Head,
typename Tail >
270 #if optional_CONFIG_MAX_ALIGN_HACK 274 #define optional_UNIQUE( name ) optional_UNIQUE2( name, __LINE__ ) 275 #define optional_UNIQUE2( name, line ) optional_UNIQUE3( name, line ) 276 #define optional_UNIQUE3( name, line ) name ## line 278 #define optional_ALIGN_TYPE( type ) \ 279 type optional_UNIQUE( _t ); struct_t< type > optional_UNIQUE( _st ) 281 template<
typename T >
302 #ifdef HAVE_LONG_LONG 308 Unknown ( * optional_UNIQUE(_) )( Unknown );
309 Unknown * Unknown::* optional_UNIQUE(_);
310 Unknown ( Unknown::* optional_UNIQUE(_) )( Unknown );
317 #undef optional_UNIQUE 318 #undef optional_UNIQUE2 319 #undef optional_UNIQUE3 321 #undef optional_ALIGN_TYPE 323 #elif defined( optional_CONFIG_ALIGN_AS ) // optional_CONFIG_MAX_ALIGN_HACK 327 #define optional_ALIGN_AS( unused ) \ 328 optional_CONFIG_ALIGN_AS 330 #else // optional_CONFIG_MAX_ALIGN_HACK 334 #define optional_ALIGN_AS( to_align ) \ 335 typename type_of_size< alignment_types, alignment_of< to_align >::value >::type 337 template <
typename T>
340 template <
typename T>
348 template <
unsigned A,
unsigned S>
351 enum {
value = A < S ? A : S };
354 template<
typename T >
361 template<
typename List,
size_t N >
365 N ==
sizeof(
typename List::head ),
376 template<
typename T>
379 #define optional_ALIGN_TYPE( type ) \ 380 typelist< type , typelist< struct_t< type > 406 > > > > > > > > > > > > > >
407 > > > > > > > > > > > > > >
411 #undef optional_ALIGN_TYPE 413 #endif // optional_CONFIG_MAX_ALIGN_HACK 417 template<
typename T >
429 construct_value( v );
434 ::new( value_ptr() ) value_type( v );
437 #if optional_CPP11_OR_GREATER 444 void construct_value( value_type && v )
446 ::new( value_ptr() ) value_type(
std::move( v ) );
458 return as<value_type>();
463 return as<value_type>();
468 return * value_ptr();
473 return * value_ptr();
476 #if optional_CPP11_OR_GREATER 480 return * value_ptr();
485 return * value_ptr();
490 #if optional_CPP11_OR_GREATER 493 aligned_storage_t
data;
495 #elif optional_CONFIG_MAX_ALIGN_HACK 497 typedef struct {
unsigned char data[
sizeof(value_type) ]; } aligned_storage_t;
500 aligned_storage_t
data;
505 typedef struct { align_as_type data[ 1 + (
sizeof(value_type) - 1 ) /
sizeof(align_as_type) ]; } aligned_storage_t;
508 # undef optional_ALIGN_AS 510 #endif // optional_CONFIG_MAX_ALIGN_HACK 522 template <
typename U>
525 return reinterpret_cast<U*
>(
ptr() );
528 template <
typename U>
531 return reinterpret_cast<U
const *
>(
ptr() );
545 #if optional_HAVE_CONSTEXPR_11 558 : logic_error(
"bad optional access" ) {}
563 template<
typename T>
573 : has_value_( false )
578 : has_value_( false )
583 : has_value_( rhs.has_value() )
589 #if optional_CPP11_OR_GREATER 591 : has_value_( rhs.has_value() )
593 if ( rhs.has_value() )
594 contained.construct_value(
std::move( rhs.contained.value() ) );
603 #if optional_CPP11_OR_GREATER 610 template<
class... Args >
613 , contained( T( std::forward<Args>(args)...) )
616 template<
class U,
class... Args >
619 , contained( T( il, std::forward<Args>(args)...) )
622 #endif // optional_CPP11_OR_GREATER 627 contained.destruct_value();
639 #if optional_CPP11_OR_GREATER 644 else if ( has_value() ==
false && rhs.
has_value() == true ) initialize( *rhs );
645 else if ( has_value() ==
true && rhs.
has_value() == true ) contained.value() = *rhs;
649 #if optional_CPP11_OR_GREATER 651 optional & operator=( optional && rhs ) noexcept
654 else if ( has_value() ==
false && rhs.
has_value() == true ) initialize(
std::move( *rhs ) );
655 else if ( has_value() ==
true && rhs.
has_value() == true ) contained.value() =
std::move( *rhs );
661 optional & operator=( U && v )
663 if ( has_value() ) contained.value() = std::forward<U>( v );
664 else initialize( T( std::forward<U>( v ) ) );
668 template<
class... Args >
669 void emplace( Args&&... args )
672 initialize( T( std::forward<Args>(args)...) );
675 template<
class U,
class... Args >
676 void emplace( std::initializer_list<U> il, Args&&... args )
679 initialize( T( il, std::forward<Args>(args)...) );
682 #endif // optional_CPP11_OR_GREATER 687 #if optional_CPP11_OR_GREATER 692 if ( has_value() ==
true && rhs.
has_value() == true ) {
swap( **
this, *rhs ); }
693 else if ( has_value() ==
false && rhs.
has_value() == true ) { initialize( *rhs ); rhs.
reset(); }
701 return assert( has_value() ),
702 contained.value_ptr();
707 return assert( has_value() ),
708 contained.value_ptr();
713 return assert( has_value() ),
719 return assert( has_value() ),
723 #if optional_CPP11_OR_GREATER 738 #if optional_CPP11_OR_GREATER 760 return contained.value();
768 return contained.value();
771 #if optional_HAVE_REF_QUALIFIER 791 #if optional_CPP11_OR_GREATER 796 return has_value() ? contained.value() :
static_cast<T
>(std::forward<U>( v ) );
802 return has_value() ?
std::move( contained.value() ) : static_cast<T>(std::forward<U>( v ) );
810 return has_value() ? contained.value() :
static_cast<value_type
>( v );
813 #endif // optional_CPP11_OR_GREATER 820 contained.destruct_value();
828 template<
typename V >
832 contained.construct_value( value );
836 #if optional_CPP11_OR_GREATER 837 template<
typename V >
838 void initialize( V &&
value )
856 return bool(x) !=
bool(y) ?
false :
bool(x) ==
false ?
true : *x == *y;
864 template<
typename T >
bool operator<( optional<T>
const & x,
optional<T> const & y )
866 return (!y) ?
false : (!x) ?
true : *x < *y;
874 template<
typename T >
bool operator<=( optional<T>
const & x,
optional<T> const & y )
950 return bool(x) ? *x == v :
false;
955 return bool(x) ? v == *x :
false;
960 return bool(x) ? *x != v :
true;
965 return bool(x) ? v != *x :
true;
968 template<
typename T >
bool operator<( optional<T>
const & x,
const T& v )
970 return bool(x) ? *x < v :
true;
973 template<
typename T >
bool operator<( T const & v, optional<T>
const & x )
975 return bool(x) ? v < *x :
false;
978 template<
typename T >
bool operator<=( optional<T>
const & x,
const T& v )
980 return bool(x) ? *x <= v :
true;
983 template<
typename T >
bool operator<=( T const & v, optional<T>
const & x )
985 return bool(x) ? v <= *x :
false;
990 return bool(x) ? *x > v :
false;
995 return bool(x) ? v > *x :
true;
1000 return bool(x) ? *x >= v :
false;
1005 return bool(x) ? v >= *x :
true;
1010 template<
typename T >
1012 #if optional_CPP11_OR_GREATER 1013 noexcept( noexcept( x.
swap(y) ) )
1019 #if optional_CPP11_OR_GREATER 1027 template<
class T,
class...Args >
1033 template<
class T,
class U,
class... Args >
1041 template<
typename T >
1047 #endif // optional_CPP11_OR_GREATER 1051 using namespace optional_lite;
1055 #if optional_CPP11_OR_GREATER 1062 class hash< nonstd::optional<T> >
1067 return bool( v ) ? hash<T>()( *v ) : 0;
1073 #endif // optional_CPP11_OR_GREATER 1075 #endif // NONSTD_OPTIONAL_LITE_HPP
enum MQTTPropertyCodes value
bool operator>=(T const &v, optional< T > const &x)
conditional< N==sizeof(typename List::head), typename List::head, typename type_of_size< typename List::tail, N >::type >::type type
#define optional_noexcept
#define optional_ref_qual
void swap(optional< T > &x, optional< T > &y)
optional_constexpr14 value_type & value() optional_ref_qual
bool operator==(T const &v, optional< T > const &x)
void this_type_does_not_support_comparisons() const
optional_constexpr14 value_type const & value() const optional_ref_qual
void reset() optional_noexcept
std::size_t hash(const BasicJsonType &j)
hash a JSON value
optional< T > make_optional(T const &v)
bool operator>(T const &v, optional< T > const &x)
optional_constexpr nullopt_t(init)
void const * ptr() const optional_noexcept
optional & operator=(nullopt_t) optional_noexcept
optional_constexpr optional(value_type const &value)
optional_CONFIG_ALIGN_AS_FALLBACK type
void initialize(V const &value)
void construct_value(value_type const &v)
optional(optional const &rhs)
#define nonstd_lite_in_place_type_t(T)
value_type const * value_ptr() const
detail::storage_t< value_type > contained
storage_t(value_type const &v)
#define optional_CONFIG_ALIGN_AS_FALLBACK
value_type & value() optional_ref_qual
#define optional_refref_qual
optional_constexpr value_type value_or(U const &v) const
value_type const & value() const optional_ref_qual
std::nullopt_t nullopt_t
A tag type to represent an empty optional.
void * ptr() optional_noexcept
const T & move(const T &v)
#define assert(a, b, c, d)
optional_constexpr optional(nullopt_t) optional_noexcept
optional_constexpr optional() optional_noexcept
#define optional_ALIGN_TYPE(type)
#define optional_constexpr14
optional & operator=(optional const &rhs)
std::in_place_t in_place_t
bool operator!=(T const &v, optional< T > const &x)
C++03 constructed union to hold value.
#define optional_ALIGN_AS(to_align)
const nullopt_t nullopt((nullopt_t::init()))
#define optional_constexpr
in_place_t in_place(detail::in_place_type_tag< T >=detail::in_place_type_tag< T >())
optional_constexpr bool has_value() const optional_noexcept