5 #ifndef BOOST_ANY_HEADER
6 #define BOOST_ANY_HEADER
10 #include <type_traits>
18 template<
typename ValueType>
19 any(
const ValueType & value): content(new holder<ValueType>(value)) {}
21 any(
const any & other): content(other.content ? other.content->clone() : 0) {}
24 any(any&& other): content(other.content) {
29 template<
typename ValueType>
30 any(ValueType&& value,
typename std::enable_if<!std::is_same<any&, ValueType>::value >::type* = 0)
31 : content(new holder< typename std::remove_reference<ValueType>::type >(static_cast<ValueType&&>(value))) {}
39 any & swap(any & rhs) {
40 std::swap(content, rhs.content);
44 any & operator=(
const any& rhs) {
50 any & operator=(any&& rhs) {
57 template <
class ValueType>
58 any & operator=(ValueType&& rhs) {
59 any(static_cast<ValueType&&>(rhs)).swap(*
this);
69 const std::type_info & type()
const {
70 return content ? content->type() :
typeid(void);
76 virtual ~placeholder() {}
78 virtual const std::type_info & type()
const = 0;
79 virtual placeholder * clone()
const = 0;
82 template<
typename ValueType>
83 class holder :
public placeholder {
85 holder(
const ValueType & value): held(value) {}
86 holder(ValueType&& value): held(static_cast< ValueType&& >(value)) {}
88 virtual const std::type_info & type()
const {
89 return typeid(ValueType);
91 virtual placeholder * clone()
const {
92 return new holder(held);
98 holder & operator=(
const holder &);
103 template<
typename ValueType>
104 friend ValueType * any_cast(any *);
106 template<
typename ValueType>
107 friend ValueType * unsafe_any_cast(any *);
109 placeholder * content;
112 inline void swap(any & lhs, any & rhs) {
116 class bad_any_cast :
public std::bad_cast {
118 virtual const char * what() const noexcept {
119 return "boost::bad_any_cast: "
120 "failed conversion using boost::any_cast";
124 template<
typename ValueType>
125 ValueType * any_cast(any * operand) {
126 return operand && operand->type() ==
typeid(ValueType) ? &
static_cast<any::holder<ValueType> *
>(operand->content)->held: 0;
129 template<
typename ValueType>
130 inline const ValueType * any_cast(
const any * operand) {
131 return any_cast<ValueType>(
const_cast<any *
>(operand));
134 template<
typename ValueType>
135 ValueType any_cast(any & operand) {
136 typedef typename std::remove_reference<ValueType>::type nonref;
138 nonref * result = any_cast<nonref>(&operand);
140 throw(bad_any_cast());
144 template<
typename ValueType>
145 inline ValueType any_cast(
const any & operand) {
146 typedef typename std::remove_reference<ValueType>::type nonref;
147 return any_cast<
const nonref &>(
const_cast<any &
>(operand));
155 template<
typename ValueType>
156 inline ValueType * unsafe_any_cast(any * operand) {
157 return &
static_cast<any::holder<ValueType> *
>(operand->content)->held;
160 template<
typename ValueType>
161 inline const ValueType * unsafe_any_cast(
const any * operand) {
162 return unsafe_any_cast<ValueType>(
const_cast<any *
>(operand));
167 #endif //BOOST_ANY_HEADER