11 #ifndef NONSTD_ANY_LITE_HPP 12 #define NONSTD_ANY_LITE_HPP 14 #define any_lite_MAJOR 0 15 #define any_lite_MINOR 4 16 #define any_lite_PATCH 0 18 #define any_lite_VERSION any_STRINGIFY(any_lite_MAJOR) "." any_STRINGIFY(any_lite_MINOR) "." any_STRINGIFY(any_lite_PATCH) 20 #define any_STRINGIFY( x ) any_STRINGIFY_( x ) 21 #define any_STRINGIFY_( x ) #x 25 #define any_ANY_DEFAULT 0 26 #define any_ANY_NONSTD 1 32 # if __has_include(<nonstd/any.tweak.hpp>) 33 # include <nonstd/any.tweak.hpp> 35 #define any_HAVE_TWEAK_HEADER 1 37 #define any_HAVE_TWEAK_HEADER 0 43 #if !defined( any_CONFIG_SELECT_ANY ) 44 # define any_CONFIG_SELECT_ANY ( any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD ) 49 #ifndef any_CONFIG_NO_EXCEPTIONS 53 # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS) 54 # define any_CONFIG_NO_EXCEPTIONS 0 56 # define any_CONFIG_NO_EXCEPTIONS 1 64 # if defined(_MSVC_LANG ) && !defined(__clang__) 65 # define any_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) 67 # define any_CPLUSPLUS __cplusplus 71 #define any_CPP98_OR_GREATER ( any_CPLUSPLUS >= 199711L ) 72 #define any_CPP11_OR_GREATER ( any_CPLUSPLUS >= 201103L ) 73 #define any_CPP14_OR_GREATER ( any_CPLUSPLUS >= 201402L ) 74 #define any_CPP17_OR_GREATER ( any_CPLUSPLUS >= 201703L ) 75 #define any_CPP20_OR_GREATER ( any_CPLUSPLUS >= 202000L ) 79 #if any_CPP17_OR_GREATER && defined(__has_include ) 80 # if __has_include( <any> ) 81 # define any_HAVE_STD_ANY 1 83 # define any_HAVE_STD_ANY 0 86 # define any_HAVE_STD_ANY 0 89 #define any_USES_STD_ANY ( (any_CONFIG_SELECT_ANY == any_ANY_STD) || ((any_CONFIG_SELECT_ANY == any_ANY_DEFAULT) && any_HAVE_STD_ANY) ) 95 #ifndef nonstd_lite_HAVE_IN_PLACE_TYPES 96 #define nonstd_lite_HAVE_IN_PLACE_TYPES 1 100 #if any_CPP17_OR_GREATER 109 using std::in_place_t;
110 using std::in_place_type_t;
111 using std::in_place_index_t;
113 #define nonstd_lite_in_place_t( T) std::in_place_t 114 #define nonstd_lite_in_place_type_t( T) std::in_place_type_t<T> 115 #define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K> 117 #define nonstd_lite_in_place( T) std::in_place_t{} 118 #define nonstd_lite_in_place_type( T) std::in_place_type_t<T>{} 119 #define nonstd_lite_in_place_index(K) std::in_place_index_t<K>{} 123 #else // any_CPP17_OR_GREATER 133 template< std::
size_t K >
146 template< std::
size_t K >
158 template< std::
size_t K >
166 #define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> ) 167 #define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> ) 168 #define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<K> ) 170 #define nonstd_lite_in_place( T) nonstd::in_place_type<T> 171 #define nonstd_lite_in_place_type( T) nonstd::in_place_type<T> 172 #define nonstd_lite_in_place_index(K) nonstd::in_place_index<K> 176 #endif // any_CPP17_OR_GREATER 177 #endif // nonstd_lite_HAVE_IN_PLACE_TYPES 194 using std::bad_any_cast;
197 #else // any_USES_STD_ANY 215 #if defined(_MSC_VER ) && !defined(__clang__) 216 # define any_COMPILER_MSVC_VER (_MSC_VER ) 217 # define any_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) ) 219 # define any_COMPILER_MSVC_VER 0 220 # define any_COMPILER_MSVC_VERSION 0 223 #define any_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) ) 225 #if defined(__clang__) 226 # define any_COMPILER_CLANG_VERSION any_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) 228 # define any_COMPILER_CLANG_VERSION 0 231 #if defined(__GNUC__) && !defined(__clang__) 232 # define any_COMPILER_GNUC_VERSION any_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) 234 # define any_COMPILER_GNUC_VERSION 0 242 #define any_HAVE( feature ) ( any_HAVE_##feature ) 245 # define any_HAS_CPP0X _HAS_CPP0X 247 # define any_HAS_CPP0X 0 250 #define any_CPP11_90 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1500) 251 #define any_CPP11_100 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1600) 252 #define any_CPP11_120 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1800) 253 #define any_CPP11_140 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1900) 255 #define any_CPP14_000 (any_CPP14_OR_GREATER) 256 #define any_CPP17_000 (any_CPP17_OR_GREATER) 260 #define any_HAVE_CONSTEXPR_11 any_CPP11_140 261 #define any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG \ 263 #define any_HAVE_INITIALIZER_LIST any_CPP11_120 264 #define any_HAVE_NOEXCEPT any_CPP11_140 265 #define any_HAVE_NULLPTR any_CPP11_100 266 #define any_HAVE_TYPE_TRAITS any_CPP11_90 267 #define any_HAVE_STATIC_ASSERT any_CPP11_100 268 #define any_HAVE_ADD_CONST any_CPP11_90 269 #define any_HAVE_OVERRIDE any_CPP11_90 270 #define any_HAVE_REMOVE_REFERENCE any_CPP11_90 272 #define any_HAVE_TR1_ADD_CONST (!! any_COMPILER_GNUC_VERSION ) 273 #define any_HAVE_TR1_REMOVE_REFERENCE (!! any_COMPILER_GNUC_VERSION ) 274 #define any_HAVE_TR1_TYPE_TRAITS (!! any_COMPILER_GNUC_VERSION ) 278 #define any_HAVE_CONSTEXPR_14 any_CPP14_000 282 #define any_HAVE_NODISCARD any_CPP17_000 286 #if any_HAVE_CONSTEXPR_11 287 # define any_constexpr constexpr 289 # define any_constexpr 292 #if any_HAVE_CONSTEXPR_14 293 # define any_constexpr14 constexpr 295 # define any_constexpr14 298 #if any_HAVE_NOEXCEPT 299 # define any_noexcept noexcept 301 # define any_noexcept 305 # define any_nullptr nullptr 307 # define any_nullptr NULL 310 #if any_HAVE_NODISCARD 311 # define any_nodiscard [[nodiscard]] 313 # define any_nodiscard 316 #if any_HAVE_OVERRIDE 317 # define any_override override 319 # define any_override 324 #if any_CONFIG_NO_EXCEPTIONS 330 #if ! any_HAVE_NULLPTR 334 #if any_HAVE_INITIALIZER_LIST 335 # include <initializer_list> 338 #if any_HAVE_TYPE_TRAITS 339 # include <type_traits> 340 #elif any_HAVE_TR1_TYPE_TRAITS 341 # include <tr1/type_traits> 346 #if any_CPP11_OR_GREATER 348 #define any_REQUIRES_0(...) \ 349 template< bool B = (__VA_ARGS__), typename std::enable_if<B, int>::type = 0 > 351 #define any_REQUIRES_T(...) \ 352 , typename std::enable_if< (__VA_ARGS__), int >::type = 0 354 #define any_REQUIRES_R(R, ...) \ 355 typename std::enable_if<__VA_ARGS__, R>::type 357 #define any_REQUIRES_A(...) \ 358 , typename std::enable_if<__VA_ARGS__, void*>::type = nullptr 372 #if any_HAVE_ADD_CONST 374 using std::add_const;
376 #elif any_HAVE_TR1_ADD_CONST 378 using std::tr1::add_const;
384 #endif // any_HAVE_ADD_CONST 386 #if any_HAVE_REMOVE_REFERENCE 388 using std::remove_reference;
390 #elif any_HAVE_TR1_REMOVE_REFERENCE 392 using std::tr1::remove_reference;
399 #endif // any_HAVE_REMOVE_REFERENCE 411 #if ! any_CONFIG_NO_EXCEPTIONS 413 class bad_any_cast :
public std::bad_cast
416 #if any_CPP11_OR_GREATER 419 virtual const char* what()
const throw()
422 return "any-lite: bad any_cast";
426 #endif // any_CONFIG_NO_EXCEPTIONS 436 : content( other.content ? other.content->clone() :
any_nullptr )
439 #if any_CPP11_OR_GREATER 448 class ValueType,
class T =
typename std::decay<ValueType>::type
452 : content(
new holder<T>( std::forward<ValueType>(
value ) ) )
456 class T,
class... Args
460 : content(
new holder<T>( T( std::forward<Args>(args)... ) ) )
464 class T,
class U,
class... Args
465 any_REQUIRES_T( std::is_constructible<T, std::initializer_list<U>&, Args&&...>::
value )
468 : content(
new holder<T>( T( il, std::forward<Args>(args)... ) ) )
473 template<
class ValueType >
475 : content( new holder<ValueType>( value ) )
478 #endif // any_CPP11_OR_GREATER 487 any( other ).
swap( *
this );
491 #if any_CPP11_OR_GREATER 500 class ValueType,
class T =
typename std::decay<ValueType>::type
503 any & operator=( T &&
value )
509 template<
class T,
class... Args >
510 void emplace( Args && ... args )
512 any( T( std::forward<Args>(args)... ) ).swap( *
this );
516 class T,
class U,
class... Args
517 any_REQUIRES_T( std::is_constructible<T, std::initializer_list<U>&, Args&&...>::
value )
519 void emplace( std::initializer_list<U> il, Args&&... args )
521 any( T( il, std::forward<Args>(args)... ) ).swap( *
this );
526 template<
class ValueType >
529 any( value ).
swap( *
this );
533 #endif // any_CPP11_OR_GREATER 552 return has_value() ? content->type() :
typeid( void );
559 template<
class ValueType >
562 return &(
static_cast<holder<ValueType> *
>( content )->held );
565 template<
class ValueType >
568 return &(
static_cast<holder<ValueType> *
>( content )->held );
575 virtual ~placeholder()
579 virtual std::type_info
const & type()
const = 0;
581 virtual placeholder * clone()
const = 0;
584 template<
typename ValueType >
585 class holder :
public placeholder
588 holder( ValueType
const &
value )
592 #if any_CPP11_OR_GREATER 593 holder( ValueType && value )
598 virtual std::type_info
const & type()
const any_override 600 return typeid( ValueType );
605 return new holder( held );
611 placeholder * content;
619 #if any_CPP11_OR_GREATER 621 template<
class T,
class ...Args >
622 inline any make_any( Args&& ...args )
627 template<
class T,
class U,
class ...Args >
628 inline any make_any( std::initializer_list<U> il, Args&& ...args )
633 #endif // any_CPP11_OR_GREATER 637 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 646 #if any_CONFIG_NO_EXCEPTIONS 651 throw bad_any_cast();
660 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 669 #if any_CONFIG_NO_EXCEPTIONS 674 throw bad_any_cast();
681 #if any_CPP11_OR_GREATER 685 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 693 #if any_CONFIG_NO_EXCEPTIONS 698 throw bad_any_cast();
705 #endif // any_CPP11_OR_GREATER 707 template<
class ValueType >
713 template<
class ValueType >
721 using namespace any_lite;
725 #endif // any_USES_STD_ANY 727 #endif // NONSTD_ANY_LITE_HPP
void swap(any &x, any &y) any_noexcept
bool has_value() const any_noexcept
in_place_t in_place(detail::in_place_index_tag< K >=detail::in_place_index_tag< K >())
any(ValueType const &value)
void reset() any_noexcept
in_place_t in_place_index(detail::in_place_index_tag< K >=detail::in_place_index_tag< K >())
#define nonstd_lite_in_place_type_t(T)
const ValueType * to_ptr() const
#define nonstd_lite_in_place_type(T)
any & operator=(any const &other)
any_nodiscard ValueType * any_cast(any *operand) any_noexcept
any_constexpr any() any_noexcept
in_place_t in_place_type(detail::in_place_type_tag< T >=detail::in_place_type_tag< T >())
any & operator=(ValueType const &value)
void swap(any &other) any_noexcept
const std::type_info & type() const any_noexcept
in_place_t in_place(detail::in_place_type_tag< T >=detail::in_place_type_tag< T >())