11 #ifndef NONSTD_ANY_LITE_HPP 12 #define NONSTD_ANY_LITE_HPP 14 #define any_lite_MAJOR 0 15 #define any_lite_MINOR 1 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 29 #if !defined( any_CONFIG_SELECT_ANY ) 30 # define any_CONFIG_SELECT_ANY ( any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD ) 37 # if defined(_MSVC_LANG ) && !defined(__clang__) 38 # define any_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) 40 # define any_CPLUSPLUS __cplusplus 44 #define any_CPP98_OR_GREATER ( any_CPLUSPLUS >= 199711L ) 45 #define any_CPP11_OR_GREATER ( any_CPLUSPLUS >= 201103L ) 46 #define any_CPP14_OR_GREATER ( any_CPLUSPLUS >= 201402L ) 47 #define any_CPP17_OR_GREATER ( any_CPLUSPLUS >= 201703L ) 48 #define any_CPP20_OR_GREATER ( any_CPLUSPLUS >= 202000L ) 52 #if any_CPP17_OR_GREATER && defined(__has_include ) 53 # if __has_include( <any> ) 54 # define any_HAVE_STD_ANY 1 56 # define any_HAVE_STD_ANY 0 59 # define any_HAVE_STD_ANY 0 62 #define any_USES_STD_ANY ( (any_CONFIG_SELECT_ANY == any_ANY_STD) || ((any_CONFIG_SELECT_ANY == any_ANY_DEFAULT) && any_HAVE_STD_ANY) ) 68 #ifndef nonstd_lite_HAVE_IN_PLACE_TYPES 69 #define nonstd_lite_HAVE_IN_PLACE_TYPES 1 73 #if any_CPP17_OR_GREATER 82 using std::in_place_t;
83 using std::in_place_type_t;
84 using std::in_place_index_t;
86 #define nonstd_lite_in_place_t( T) std::in_place_t 87 #define nonstd_lite_in_place_type_t( T) std::in_place_type_t<T> 88 #define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K> 90 #define nonstd_lite_in_place( T) std::in_place_t{} 91 #define nonstd_lite_in_place_type( T) std::in_place_type_t<T>{} 92 #define nonstd_lite_in_place_index(K) std::in_place_index_t<K>{} 96 #else // any_CPP17_OR_GREATER 106 template< std::
size_t K >
119 template< std::
size_t K >
131 template< std::
size_t K >
139 #define nonstd_lite_in_place_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> ) 140 #define nonstd_lite_in_place_type_t( T) nonstd::in_place_t(&)( nonstd::detail::in_place_type_tag<T> ) 141 #define nonstd_lite_in_place_index_t(K) nonstd::in_place_t(&)( nonstd::detail::in_place_index_tag<K> ) 143 #define nonstd_lite_in_place( T) nonstd::in_place_type<T> 144 #define nonstd_lite_in_place_type( T) nonstd::in_place_type<T> 145 #define nonstd_lite_in_place_index(K) nonstd::in_place_index<K> 149 #endif // any_CPP17_OR_GREATER 150 #endif // nonstd_lite_HAVE_IN_PLACE_TYPES 167 using std::bad_any_cast;
170 #else // any_USES_STD_ANY 188 #if defined(_MSC_VER ) && !defined(__clang__) 189 # define any_COMPILER_MSVC_VER (_MSC_VER ) 190 # define any_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) ) 192 # define any_COMPILER_MSVC_VER 0 193 # define any_COMPILER_MSVC_VERSION 0 196 #define any_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) ) 198 #if defined(__clang__) 199 # define any_COMPILER_CLANG_VERSION any_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__) 201 # define any_COMPILER_CLANG_VERSION 0 204 #if defined(__GNUC__) && !defined(__clang__) 205 # define any_COMPILER_GNUC_VERSION any_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) 207 # define any_COMPILER_GNUC_VERSION 0 215 #define any_HAVE( feature ) ( any_HAVE_##feature ) 218 # define any_HAS_CPP0X _HAS_CPP0X 220 # define any_HAS_CPP0X 0 223 #define any_CPP11_90 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1500) 224 #define any_CPP11_100 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1600) 225 #define any_CPP11_120 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1800) 226 #define any_CPP11_140 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1900) 228 #define any_CPP14_000 (any_CPP14_OR_GREATER) 229 #define any_CPP17_000 (any_CPP17_OR_GREATER) 233 #define any_HAVE_CONSTEXPR_11 any_CPP11_140 234 #define any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG \ 236 #define any_HAVE_INITIALIZER_LIST any_CPP11_120 237 #define any_HAVE_NOEXCEPT any_CPP11_140 238 #define any_HAVE_NULLPTR any_CPP11_100 239 #define any_HAVE_TYPE_TRAITS any_CPP11_90 240 #define any_HAVE_STATIC_ASSERT any_CPP11_100 241 #define any_HAVE_ADD_CONST any_CPP11_90 242 #define any_HAVE_REMOVE_REFERENCE any_CPP11_90 244 #define any_HAVE_TR1_ADD_CONST (!! any_COMPILER_GNUC_VERSION ) 245 #define any_HAVE_TR1_REMOVE_REFERENCE (!! any_COMPILER_GNUC_VERSION ) 246 #define any_HAVE_TR1_TYPE_TRAITS (!! any_COMPILER_GNUC_VERSION ) 250 #define any_HAVE_CONSTEXPR_14 any_CPP14_000 254 #define any_HAVE_NODISCARD any_CPP17_000 258 #if any_HAVE_CONSTEXPR_11 259 # define any_constexpr constexpr 261 # define any_constexpr 264 #if any_HAVE_CONSTEXPR_14 265 # define any_constexpr14 constexpr 267 # define any_constexpr14 270 #if any_HAVE_NOEXCEPT 271 # define any_noexcept noexcept 273 # define any_noexcept 277 # define any_nullptr nullptr 279 # define any_nullptr NULL 282 #if any_HAVE_NODISCARD 283 # define any_nodiscard [[nodiscard]] 285 # define any_nodiscard 290 #if ! any_HAVE_NULLPTR 294 #if any_HAVE_INITIALIZER_LIST 295 # include <initializer_list> 298 #if any_HAVE_TYPE_TRAITS 299 # include <type_traits> 300 #elif any_HAVE_TR1_TYPE_TRAITS 301 # include <tr1/type_traits> 314 #if any_HAVE_ADD_CONST 316 using std::add_const;
318 #elif any_HAVE_TR1_ADD_CONST 320 using std::tr1::add_const;
326 #endif // any_HAVE_ADD_CONST 328 #if any_HAVE_REMOVE_REFERENCE 330 using std::remove_reference;
332 #elif any_HAVE_TR1_REMOVE_REFERENCE 334 using std::tr1::remove_reference;
341 #endif // any_HAVE_REMOVE_REFERENCE 348 #if any_CPP11_OR_GREATER 351 virtual const char*
what()
const throw()
354 return "any-lite: bad any_cast";
366 : content( other.content ? other.content->clone() :
any_nullptr )
369 #if any_CPP11_OR_GREATER 372 : content( std::move( other.content ) )
378 class ValueType,
class T =
typename std::decay<ValueType>::type
379 ,
typename =
typename std::enable_if< ! std::is_same<T, any>::value >
::type 382 : content(
new holder<T>( std::move( value ) ) )
386 class T,
class... Args
387 ,
typename =
typename std::enable_if< std::is_constructible<T, Args...>::value >
::type 390 : content( new holder<T>( T( std::forward<Args>(args)... ) ) )
394 class T,
class U,
class... Args
395 ,
typename =
typename std::enable_if< std::is_constructible<T, std::initializer_list<U>&, Args...>::value >
::type 398 : content( new holder<T>( T( il, std::forward<Args>(args)... ) ) )
403 template<
class ValueType >
404 any( ValueType
const & value )
405 : content( new holder<ValueType>( value ) )
408 #endif // any_CPP11_OR_GREATER 417 any( other ).swap( *
this );
421 #if any_CPP11_OR_GREATER 425 any( std::move( other ) ).swap( *
this );
430 class ValueType,
class T =
typename std::decay<ValueType>::type
431 ,
typename =
typename std::enable_if< ! std::is_same<T, any>::value >
::type 435 any( std::move( value ) ).swap( *
this );
439 template<
class T,
class... Args >
440 void emplace( Args && ... args )
442 any( T( std::forward<Args>(args)... ) ).swap( *
this );
446 class T,
class U,
class... Args
447 ,
typename =
typename std::enable_if< std::is_constructible<T, std::initializer_list<U>&, Args...>::value >
::type 449 void emplace( std::initializer_list<U> il, Args&&... args )
451 any( T( il, std::forward<Args>(args)... ) ).swap( *
this );
456 template<
class ValueType >
459 any( value ).swap( *
this );
463 #endif // any_CPP11_OR_GREATER 482 return has_value() ? content->type() :
typeid( void );
489 template<
class ValueType >
492 return &(
static_cast<holder<ValueType> *
>( content )->held );
495 template<
class ValueType >
498 return &(
static_cast<holder<ValueType> *
>( content )->held );
505 virtual ~placeholder()
509 virtual std::type_info
const &
type()
const = 0;
511 virtual placeholder * clone()
const = 0;
514 template<
typename ValueType >
515 class holder :
public placeholder
518 holder( ValueType
const & value )
522 #if any_CPP11_OR_GREATER 523 holder( ValueType && value )
524 : held( std::move( value ) )
528 virtual std::type_info
const &
type()
const 530 return typeid( ValueType );
533 virtual placeholder * clone()
const 535 return new holder( held );
541 placeholder * content;
549 #if any_CPP11_OR_GREATER 551 template<
class T,
class ...Args >
552 inline any make_any( Args&& ...args )
557 template<
class T,
class U,
class ...Args >
558 inline any make_any( std::initializer_list<U> il, Args&& ...args )
563 #endif // any_CPP11_OR_GREATER 567 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 568 ,
typename =
typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
585 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 586 ,
typename =
typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
601 #if any_CPP11_OR_GREATER 605 #if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG 606 ,
typename =
typename std::enable_if< std::is_reference<ValueType>::value || std::is_copy_constructible<ValueType>::value >::type
621 #endif // any_CPP11_OR_GREATER 623 template<
class ValueType >
626 return operand !=
any_nullptr && operand->type() ==
typeid(ValueType) ? operand->to_ptr<ValueType>() :
any_nullptr;
629 template<
class ValueType >
632 return operand !=
any_nullptr && operand->type() ==
typeid(ValueType) ? operand->to_ptr<ValueType>() :
any_nullptr;
637 using namespace any_lite;
641 #endif // any_USES_STD_ANY 643 #endif // NONSTD_ANY_LITE_HPP void swap(any &x, any &y) any_noexcept
bool has_value() const any_noexcept
#define nonstd_lite_in_place_type(T)
in_place_t in_place(detail::in_place_index_tag< K >=detail::in_place_index_tag< K >())
virtual const char * what() const
any(ValueType const &value)
any_nodiscard ValueType any_cast(any const &operand)
void reset() any_noexcept
in_place_t in_place_index(detail::in_place_index_tag< K >=detail::in_place_index_tag< K >())
const ValueType * to_ptr() const
#define nonstd_lite_in_place_type_t(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 >())