756 lines
18 KiB
C
Raw Normal View History

2024-01-06 13:54:58 +08:00
//
// Copyright (c) 2016-2018 Martin Moene
//
// https://github.com/martinmoene/any-lite
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef NONSTD_ANY_LITE_HPP
#define NONSTD_ANY_LITE_HPP
2024-01-06 13:56:12 +08:00
#define any_lite_MAJOR 0
#define any_lite_MINOR 4
#define any_lite_PATCH 0
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_lite_VERSION \
any_STRINGIFY(any_lite_MAJOR) "." any_STRINGIFY( \
any_lite_MINOR) "." any_STRINGIFY(any_lite_PATCH)
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_STRINGIFY(x) any_STRINGIFY_(x)
#define any_STRINGIFY_(x) #x
2024-01-06 13:54:58 +08:00
// any-lite configuration:
2024-01-06 13:56:12 +08:00
#define any_ANY_DEFAULT 0
#define any_ANY_NONSTD 1
#define any_ANY_STD 2
2024-01-06 13:54:58 +08:00
// tweak header support:
#ifdef __has_include
2024-01-06 13:56:12 +08:00
#if __has_include(<nonstd/any.tweak.hpp>)
#include <nonstd/any.tweak.hpp>
#endif
#define any_HAVE_TWEAK_HEADER 1
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_HAVE_TWEAK_HEADER 0
2024-01-06 13:54:58 +08:00
//# pragma message("any.hpp: Note: Tweak header not supported.")
#endif
// any selection and configuration:
2024-01-06 13:56:12 +08:00
#if !defined(any_CONFIG_SELECT_ANY)
#define any_CONFIG_SELECT_ANY (any_HAVE_STD_ANY ? any_ANY_STD : any_ANY_NONSTD)
2024-01-06 13:54:58 +08:00
#endif
// Control presence of exception handling (try and auto discover):
#ifndef any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:56:12 +08:00
#if defined(_MSC_VER)
#include <cstddef>// for _HAS_EXCEPTIONS
#endif
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
#define any_CONFIG_NO_EXCEPTIONS 0
#else
#define any_CONFIG_NO_EXCEPTIONS 1
#endif
2024-01-06 13:54:58 +08:00
#endif
// C++ language version detection (C++23 is speculative):
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
2024-01-06 13:56:12 +08:00
#ifndef any_CPLUSPLUS
#if defined(_MSVC_LANG) && !defined(__clang__)
#define any_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG)
#else
#define any_CPLUSPLUS __cplusplus
#endif
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
#define any_CPP98_OR_GREATER (any_CPLUSPLUS >= 199711L)
#define any_CPP11_OR_GREATER (any_CPLUSPLUS >= 201103L)
#define any_CPP14_OR_GREATER (any_CPLUSPLUS >= 201402L)
#define any_CPP17_OR_GREATER (any_CPLUSPLUS >= 201703L)
#define any_CPP20_OR_GREATER (any_CPLUSPLUS >= 202002L)
#define any_CPP23_OR_GREATER (any_CPLUSPLUS >= 202300L)
2024-01-06 13:54:58 +08:00
// Use C++17 std::any if available and requested:
2024-01-06 13:56:12 +08:00
#if any_CPP17_OR_GREATER && defined(__has_include)
#if __has_include(<any> )
#define any_HAVE_STD_ANY 1
#else
#define any_HAVE_STD_ANY 0
#endif
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_HAVE_STD_ANY 0
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
#define any_USES_STD_ANY \
((any_CONFIG_SELECT_ANY == any_ANY_STD) \
|| ((any_CONFIG_SELECT_ANY == any_ANY_DEFAULT) && any_HAVE_STD_ANY))
2024-01-06 13:54:58 +08:00
//
// in_place: code duplicated in any-lite, expected-lite, optional-lite, value-ptr-lite, variant-lite:
//
#ifndef nonstd_lite_HAVE_IN_PLACE_TYPES
2024-01-06 13:56:12 +08:00
#define nonstd_lite_HAVE_IN_PLACE_TYPES 1
2024-01-06 13:54:58 +08:00
// C++17 std::in_place in <utility>:
#if any_CPP17_OR_GREATER
#include <utility>
namespace nonstd {
using std::in_place;
using std::in_place_index;
2024-01-06 13:56:12 +08:00
using std::in_place_index_t;
2024-01-06 13:54:58 +08:00
using std::in_place_t;
2024-01-06 13:56:12 +08:00
using std::in_place_type;
2024-01-06 13:54:58 +08:00
using std::in_place_type_t;
2024-01-06 13:56:12 +08:00
#define nonstd_lite_in_place_t(T) std::in_place_t
#define nonstd_lite_in_place_type_t(T) std::in_place_type_t<T>
#define nonstd_lite_in_place_index_t(K) std::in_place_index_t<K>
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define nonstd_lite_in_place(T) \
std::in_place_t {}
#define nonstd_lite_in_place_type(T) \
std::in_place_type_t<T> {}
#define nonstd_lite_in_place_index(K) \
std::in_place_index_t<K> {}
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
}// namespace nonstd
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#else// any_CPP17_OR_GREATER
2024-01-06 13:54:58 +08:00
#include <cstddef>
namespace nonstd {
namespace detail {
2024-01-06 13:56:12 +08:00
template<class T>
2024-01-06 13:54:58 +08:00
struct in_place_type_tag {};
2024-01-06 13:56:12 +08:00
template<std::size_t K>
2024-01-06 13:54:58 +08:00
struct in_place_index_tag {};
2024-01-06 13:56:12 +08:00
}// namespace detail
2024-01-06 13:54:58 +08:00
struct in_place_t {};
2024-01-06 13:56:12 +08:00
template<class T>
inline in_place_t
in_place(detail::in_place_type_tag<T> = detail::in_place_type_tag<T>())
2024-01-06 13:54:58 +08:00
{
return in_place_t();
}
2024-01-06 13:56:12 +08:00
template<std::size_t K>
inline in_place_t
in_place(detail::in_place_index_tag<K> = detail::in_place_index_tag<K>())
2024-01-06 13:54:58 +08:00
{
return in_place_t();
}
2024-01-06 13:56:12 +08:00
template<class T>
inline in_place_t
in_place_type(detail::in_place_type_tag<T> = detail::in_place_type_tag<T>())
2024-01-06 13:54:58 +08:00
{
return in_place_t();
}
2024-01-06 13:56:12 +08:00
template<std::size_t K>
inline in_place_t
in_place_index(detail::in_place_index_tag<K> = detail::in_place_index_tag<K>())
2024-01-06 13:54:58 +08:00
{
return in_place_t();
}
// mimic templated typedef:
2024-01-06 13:56:12 +08:00
#define nonstd_lite_in_place_t(T) \
nonstd::in_place_t (&)(nonstd::detail::in_place_type_tag<T>)
#define nonstd_lite_in_place_type_t(T) \
nonstd::in_place_t (&)(nonstd::detail::in_place_type_tag<T>)
#define nonstd_lite_in_place_index_t(K) \
nonstd::in_place_t (&)(nonstd::detail::in_place_index_tag<K>)
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define nonstd_lite_in_place(T) nonstd::in_place_type<T>
#define nonstd_lite_in_place_type(T) nonstd::in_place_type<T>
#define nonstd_lite_in_place_index(K) nonstd::in_place_index<K>
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
}// namespace nonstd
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#endif// any_CPP17_OR_GREATER
#endif// nonstd_lite_HAVE_IN_PLACE_TYPES
2024-01-06 13:54:58 +08:00
//
// Using std::any:
//
#if any_USES_STD_ANY
#include <any>
#include <utility>
namespace nonstd {
2024-01-06 13:56:12 +08:00
using std::any;
using std::any_cast;
using std::bad_any_cast;
using std::make_any;
using std::swap;
}// namespace nonstd
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#else// any_USES_STD_ANY
2024-01-06 13:54:58 +08:00
#if !any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
#include <algorithm>// std::swap()
2024-01-06 13:54:58 +08:00
#endif
#include <utility>
// Compiler versions:
//
// MSVC++ 6.0 _MSC_VER == 1200 any_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
// MSVC++ 7.0 _MSC_VER == 1300 any_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
// MSVC++ 7.1 _MSC_VER == 1310 any_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
// MSVC++ 8.0 _MSC_VER == 1400 any_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
// MSVC++ 9.0 _MSC_VER == 1500 any_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
// MSVC++ 10.0 _MSC_VER == 1600 any_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
// MSVC++ 11.0 _MSC_VER == 1700 any_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
// MSVC++ 12.0 _MSC_VER == 1800 any_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
// MSVC++ 14.0 _MSC_VER == 1900 any_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
// MSVC++ 14.1 _MSC_VER >= 1910 any_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
// MSVC++ 14.2 _MSC_VER >= 1920 any_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
2024-01-06 13:56:12 +08:00
#if defined(_MSC_VER) && !defined(__clang__)
#define any_COMPILER_MSVC_VER (_MSC_VER)
#define any_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * (5 + (_MSC_VER < 1900)))
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_COMPILER_MSVC_VER 0
#define any_COMPILER_MSVC_VERSION 0
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
#define any_COMPILER_VERSION(major, minor, patch) \
(10 * (10 * (major) + (minor)) + (patch))
2024-01-06 13:54:58 +08:00
#if defined(__clang__)
2024-01-06 13:56:12 +08:00
#define any_COMPILER_CLANG_VERSION \
any_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_COMPILER_CLANG_VERSION 0
2024-01-06 13:54:58 +08:00
#endif
#if defined(__GNUC__) && !defined(__clang__)
2024-01-06 13:56:12 +08:00
#define any_COMPILER_GNUC_VERSION \
any_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_COMPILER_GNUC_VERSION 0
2024-01-06 13:54:58 +08:00
#endif
// half-open range [lo..hi):
//#define any_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
// Presence of language and library features:
2024-01-06 13:56:12 +08:00
#define any_HAVE(feature) (any_HAVE_##feature)
2024-01-06 13:54:58 +08:00
#ifdef _HAS_CPP0X
2024-01-06 13:56:12 +08:00
#define any_HAS_CPP0X _HAS_CPP0X
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_HAS_CPP0X 0
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
#define any_CPP11_90 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1500)
#define any_CPP11_100 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1600)
#define any_CPP11_120 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1800)
#define any_CPP11_140 (any_CPP11_OR_GREATER || any_COMPILER_MSVC_VER >= 1900)
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_CPP14_000 (any_CPP14_OR_GREATER)
#define any_CPP17_000 (any_CPP17_OR_GREATER)
2024-01-06 13:54:58 +08:00
// Presence of C++11 language features:
2024-01-06 13:56:12 +08:00
#define any_HAVE_CONSTEXPR_11 any_CPP11_140
#define any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG any_CPP11_120
#define any_HAVE_INITIALIZER_LIST any_CPP11_120
#define any_HAVE_NOEXCEPT any_CPP11_140
#define any_HAVE_NULLPTR any_CPP11_100
#define any_HAVE_TYPE_TRAITS any_CPP11_90
#define any_HAVE_STATIC_ASSERT any_CPP11_100
#define any_HAVE_ADD_CONST any_CPP11_90
#define any_HAVE_OVERRIDE any_CPP11_90
#define any_HAVE_REMOVE_REFERENCE any_CPP11_90
#define any_HAVE_TR1_ADD_CONST (!!any_COMPILER_GNUC_VERSION)
#define any_HAVE_TR1_REMOVE_REFERENCE (!!any_COMPILER_GNUC_VERSION)
#define any_HAVE_TR1_TYPE_TRAITS (!!any_COMPILER_GNUC_VERSION)
2024-01-06 13:54:58 +08:00
// Presence of C++14 language features:
2024-01-06 13:56:12 +08:00
#define any_HAVE_CONSTEXPR_14 any_CPP14_000
2024-01-06 13:54:58 +08:00
// Presence of C++17 language features:
2024-01-06 13:56:12 +08:00
#define any_HAVE_NODISCARD any_CPP17_000
2024-01-06 13:54:58 +08:00
// Presence of C++ language features:
#if any_HAVE_CONSTEXPR_11
2024-01-06 13:56:12 +08:00
#define any_constexpr constexpr
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_constexpr /*constexpr*/
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_CONSTEXPR_14
2024-01-06 13:56:12 +08:00
#define any_constexpr14 constexpr
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_constexpr14 /*constexpr*/
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_NOEXCEPT
2024-01-06 13:56:12 +08:00
#define any_noexcept noexcept
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_noexcept /*noexcept*/
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_NULLPTR
2024-01-06 13:56:12 +08:00
#define any_nullptr nullptr
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_nullptr NULL
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_NODISCARD
2024-01-06 13:56:12 +08:00
#define any_nodiscard [[nodiscard]]
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_nodiscard /*[[nodiscard]]*/
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_OVERRIDE
2024-01-06 13:56:12 +08:00
#define any_override override
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#define any_override /*override*/
2024-01-06 13:54:58 +08:00
#endif
// additional includes:
#if any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:56:12 +08:00
#include <cassert>
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
#include <typeinfo>
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
#if !any_HAVE_NULLPTR
#include <cstddef>
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_INITIALIZER_LIST
2024-01-06 13:56:12 +08:00
#include <initializer_list>
2024-01-06 13:54:58 +08:00
#endif
#if any_HAVE_TYPE_TRAITS
2024-01-06 13:56:12 +08:00
#include <type_traits>
2024-01-06 13:54:58 +08:00
#elif any_HAVE_TR1_TYPE_TRAITS
2024-01-06 13:56:12 +08:00
#include <tr1/type_traits>
2024-01-06 13:54:58 +08:00
#endif
// Method enabling
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
#define any_REQUIRES_0(...) \
template<bool B = (__VA_ARGS__), typename std::enable_if<B, int>::type = 0>
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_REQUIRES_T(...) \
, typename std::enable_if<(__VA_ARGS__), int>::type = 0
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_REQUIRES_R(R, ...) typename std::enable_if<__VA_ARGS__, R>::type
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#define any_REQUIRES_A(...) \
, typename std::enable_if<__VA_ARGS__, void *>::type = nullptr
2024-01-06 13:54:58 +08:00
#endif
//
// any:
//
2024-01-06 13:56:12 +08:00
namespace nonstd {
namespace any_lite {
2024-01-06 13:54:58 +08:00
// C++11 emulation:
namespace std11 {
#if any_HAVE_ADD_CONST
using std::add_const;
#elif any_HAVE_TR1_ADD_CONST
using std::tr1::add_const;
#else
2024-01-06 13:56:12 +08:00
template<class T>
struct add_const {
typedef const T type;
};
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#endif// any_HAVE_ADD_CONST
2024-01-06 13:54:58 +08:00
#if any_HAVE_REMOVE_REFERENCE
using std::remove_reference;
#elif any_HAVE_TR1_REMOVE_REFERENCE
using std::tr1::remove_reference;
#else
2024-01-06 13:56:12 +08:00
template<class T>
struct remove_reference {
typedef T type;
};
template<class T>
struct remove_reference<T &> {
typedef T type;
};
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#endif// any_HAVE_REMOVE_REFERENCE
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
}// namespace std11
2024-01-06 13:54:58 +08:00
namespace detail {
// for any_REQUIRES_T
2024-01-06 13:56:12 +08:00
/*enum*/ class enabler {};
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
}// namespace detail
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#if !any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
class bad_any_cast : public std::bad_cast {
2024-01-06 13:54:58 +08:00
public:
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
virtual const char *what() const any_noexcept any_override
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
virtual const char *what() const throw()
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
{
return "any-lite: bad any_cast";
}
2024-01-06 13:54:58 +08:00
};
2024-01-06 13:56:12 +08:00
#endif// any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
class any {
2024-01-06 13:54:58 +08:00
public:
2024-01-06 13:56:12 +08:00
any_constexpr any() any_noexcept : content(any_nullptr) {}
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
any(any const &other)
: content(other.content ? other.content->clone() : any_nullptr)
2024-01-06 13:54:58 +08:00
{}
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
any(any &&other) any_noexcept : content(std::move(other.content))
2024-01-06 13:54:58 +08:00
{
other.content = any_nullptr;
}
2024-01-06 13:56:12 +08:00
template<class ValueType,
class T = typename std::decay<ValueType>::type any_REQUIRES_T(
!std::is_same<T, any>::value)>
any(ValueType &&value) any_noexcept
: content(new holder<T>(std::forward<ValueType>(value)))
2024-01-06 13:54:58 +08:00
{}
2024-01-06 13:56:12 +08:00
template<class T,
class... Args any_REQUIRES_T(
std::is_constructible<T, Args &&...>::value)>
explicit any(nonstd_lite_in_place_type_t(T), Args &&...args)
: content(new holder<T>(T(std::forward<Args>(args)...)))
2024-01-06 13:54:58 +08:00
{}
2024-01-06 13:56:12 +08:00
template<class T,
class U,
class... Args any_REQUIRES_T(
std::is_constructible<T,
std::initializer_list<U> &,
Args &&...>::value)>
explicit any(nonstd_lite_in_place_type_t(T),
std::initializer_list<U> il,
Args &&...args)
: content(new holder<T>(T(il, std::forward<Args>(args)...)))
2024-01-06 13:54:58 +08:00
{}
#else
2024-01-06 13:56:12 +08:00
template<class ValueType>
any(ValueType const &value) : content(new holder<ValueType>(value))
2024-01-06 13:54:58 +08:00
{}
2024-01-06 13:56:12 +08:00
#endif// any_CPP11_OR_GREATER
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
~any() { reset(); }
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
any &operator=(any const &other)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(other).swap(*this);
2024-01-06 13:54:58 +08:00
return *this;
}
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
any &operator=(any &&other) any_noexcept
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(std::move(other)).swap(*this);
2024-01-06 13:54:58 +08:00
return *this;
}
2024-01-06 13:56:12 +08:00
template<class ValueType,
class T = typename std::decay<ValueType>::type any_REQUIRES_T(
!std::is_same<T, any>::value)>
any &operator=(T &&value)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(std::move(value)).swap(*this);
2024-01-06 13:54:58 +08:00
return *this;
}
2024-01-06 13:56:12 +08:00
template<class T, class... Args>
void emplace(Args &&...args)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(T(std::forward<Args>(args)...)).swap(*this);
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
template<class T,
class U,
class... Args any_REQUIRES_T(
std::is_constructible<T,
std::initializer_list<U> &,
Args &&...>::value)>
void emplace(std::initializer_list<U> il, Args &&...args)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(T(il, std::forward<Args>(args)...)).swap(*this);
2024-01-06 13:54:58 +08:00
}
#else
2024-01-06 13:56:12 +08:00
template<class ValueType>
any &operator=(ValueType const &value)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
any(value).swap(*this);
2024-01-06 13:54:58 +08:00
return *this;
}
2024-01-06 13:56:12 +08:00
#endif// any_CPP11_OR_GREATER
2024-01-06 13:54:58 +08:00
void reset() any_noexcept
{
2024-01-06 13:56:12 +08:00
delete content;
content = any_nullptr;
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
void swap(any &other) any_noexcept { std::swap(content, other.content); }
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
bool has_value() const any_noexcept { return content != any_nullptr; }
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
const std::type_info &type() const any_noexcept
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return has_value() ? content->type() : typeid(void);
2024-01-06 13:54:58 +08:00
}
//
// non-standard:
//
2024-01-06 13:56:12 +08:00
template<class ValueType>
const ValueType *to_ptr() const
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return &(static_cast<holder<ValueType> *>(content)->held);
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
template<class ValueType>
ValueType *to_ptr()
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return &(static_cast<holder<ValueType> *>(content)->held);
2024-01-06 13:54:58 +08:00
}
private:
2024-01-06 13:56:12 +08:00
class placeholder {
2024-01-06 13:54:58 +08:00
public:
2024-01-06 13:56:12 +08:00
virtual ~placeholder() {}
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
virtual std::type_info const &type() const = 0;
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
virtual placeholder *clone() const = 0;
2024-01-06 13:54:58 +08:00
};
2024-01-06 13:56:12 +08:00
template<typename ValueType>
class holder : public placeholder {
2024-01-06 13:54:58 +08:00
public:
2024-01-06 13:56:12 +08:00
holder(ValueType const &value) : held(value) {}
2024-01-06 13:54:58 +08:00
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
holder(ValueType &&value) : held(std::move(value)) {}
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
virtual std::type_info const &type() const any_override
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return typeid(ValueType);
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
virtual placeholder *clone() const any_override
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return new holder(held);
2024-01-06 13:54:58 +08:00
}
ValueType held;
};
2024-01-06 13:56:12 +08:00
placeholder *content;
2024-01-06 13:54:58 +08:00
};
2024-01-06 13:56:12 +08:00
inline void
swap(any &x, any &y) any_noexcept
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
x.swap(y);
2024-01-06 13:54:58 +08:00
}
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
template<class T, class... Args>
inline any
make_any(Args &&...args)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return any(nonstd_lite_in_place_type(T), std::forward<Args>(args)...);
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
template<class T, class U, class... Args>
inline any
make_any(std::initializer_list<U> il, Args &&...args)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return any(nonstd_lite_in_place_type(T), il, std::forward<Args>(args)...);
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
#endif// any_CPP11_OR_GREATER
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
template<class ValueType
2024-01-06 13:54:58 +08:00
#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
2024-01-06 13:56:12 +08:00
// any_REQUIRES_T(...) Allow for VC120 (VS2013):
,
typename = typename std::enable_if<
(std::is_reference<ValueType>::value
|| std::is_copy_constructible<ValueType>::value),
nonstd::any_lite::detail::enabler>::type
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
>
any_nodiscard inline ValueType
any_cast(any const &operand)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
const ValueType *result = any_cast<typename std11::add_const<
typename std11::remove_reference<ValueType>::type>::type>(&operand);
2024-01-06 13:54:58 +08:00
#if any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:56:12 +08:00
assert(result);
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
if (!result) { throw bad_any_cast(); }
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
return *result;
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
template<class ValueType
2024-01-06 13:54:58 +08:00
#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
2024-01-06 13:56:12 +08:00
// any_REQUIRES_T(...) Allow for VC120 (VS2013):
,
typename = typename std::enable_if<
(std::is_reference<ValueType>::value
|| std::is_copy_constructible<ValueType>::value),
nonstd::any_lite::detail::enabler>::type
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
>
any_nodiscard inline ValueType
any_cast(any &operand)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
const ValueType *result =
any_cast<typename std11::remove_reference<ValueType>::type>(&operand);
2024-01-06 13:54:58 +08:00
#if any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:56:12 +08:00
assert(result);
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
if (!result) { throw bad_any_cast(); }
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
return *result;
2024-01-06 13:54:58 +08:00
}
#if any_CPP11_OR_GREATER
2024-01-06 13:56:12 +08:00
template<class ValueType
2024-01-06 13:54:58 +08:00
#if any_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
2024-01-06 13:56:12 +08:00
any_REQUIRES_T(std::is_reference<ValueType>::value
|| std::is_copy_constructible<ValueType>::value)
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
>
any_nodiscard inline ValueType
any_cast(any &&operand)
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
const ValueType *result =
any_cast<typename std11::remove_reference<ValueType>::type>(&operand);
2024-01-06 13:54:58 +08:00
#if any_CONFIG_NO_EXCEPTIONS
2024-01-06 13:56:12 +08:00
assert(result);
2024-01-06 13:54:58 +08:00
#else
2024-01-06 13:56:12 +08:00
if (!result) { throw bad_any_cast(); }
2024-01-06 13:54:58 +08:00
#endif
2024-01-06 13:56:12 +08:00
return *result;
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
#endif// any_CPP11_OR_GREATER
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
template<class ValueType>
any_nodiscard inline ValueType const *
any_cast(any const *operand) any_noexcept
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return operand != any_nullptr && operand->type() == typeid(ValueType)
? operand->to_ptr<ValueType>()
: any_nullptr;
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
template<class ValueType>
any_nodiscard inline ValueType *
any_cast(any *operand) any_noexcept
2024-01-06 13:54:58 +08:00
{
2024-01-06 13:56:12 +08:00
return operand != any_nullptr && operand->type() == typeid(ValueType)
? operand->to_ptr<ValueType>()
: any_nullptr;
2024-01-06 13:54:58 +08:00
}
2024-01-06 13:56:12 +08:00
}// namespace any_lite
2024-01-06 13:54:58 +08:00
using namespace any_lite;
2024-01-06 13:56:12 +08:00
}// namespace nonstd
namespace ulib {
using namespace nonstd::any_lite;
}
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#endif// any_USES_STD_ANY
2024-01-06 13:54:58 +08:00
2024-01-06 13:56:12 +08:00
#endif// NONSTD_ANY_LITE_HPP