27 #ifndef MOMEMTA_ANY_INCLUDED 28 #define MOMEMTA_ANY_INCLUDED 44 #include <type_traits> 58 template<
typename ValueType>
59 any(
const ValueType & value)
61 typename std::remove_cv<
typename std::decay<const ValueType>::type>::type
67 : content(other.content ? other.content->clone() : 0)
73 : content(other.content)
79 template<
typename ValueType>
81 ,
typename std::enable_if<!std::is_same<any&, ValueType>::value>::type* = 0
82 ,
typename std::enable_if<!std::is_const<ValueType>::value>::type* = 0)
83 : content(
new holder<
typename std::decay<ValueType>::type >(static_cast<ValueType&&>(value)))
94 any & swap(
any & rhs) noexcept
96 std::swap(content, rhs.content);
101 any & operator=(
const any& rhs)
103 any(rhs).swap(*
this);
108 any & operator=(
any&& rhs) noexcept
116 template <
class ValueType>
117 any & operator=(ValueType&& rhs)
119 any(static_cast<ValueType&&>(rhs)).swap(*
this);
125 bool empty()
const noexcept
130 void clear() noexcept
135 const std::type_info& type()
const noexcept
137 return content ? content->type() :
typeid(void);
146 virtual ~placeholder()
152 virtual const std::type_info& type()
const noexcept = 0;
154 virtual placeholder * clone()
const = 0;
158 template<
typename ValueType>
159 class holder :
public placeholder
163 holder(
const ValueType & value)
168 holder(ValueType&& value)
169 : held(static_cast< ValueType&& >(value))
174 virtual const std::type_info& type()
const noexcept
176 return typeid(ValueType);
179 virtual placeholder * clone()
const 181 return new holder(held);
189 holder & operator=(
const holder &);
194 template<
typename ValueType>
195 friend ValueType * any_cast(
any *) noexcept;
197 template<
typename ValueType>
198 friend ValueType * unsafe_any_cast(
any *) noexcept;
200 placeholder * content;
204 inline void swap(
any & lhs,
any & rhs) noexcept
209 class __attribute__((__visibility__("default"))) bad_any_cast :
213 virtual const char * what()
const noexcept
215 return "momemta::bad_any_cast: " 216 "failed conversion using momemta::any_cast";
220 template<
typename ValueType>
221 ValueType * any_cast(
any * operand) noexcept
223 return operand && operand->type() ==
typeid(ValueType)
224 ? &
static_cast<any::holder<typename std::remove_cv<ValueType>::type
> *>(operand->content)->held
228 template<
typename ValueType>
229 inline const ValueType * any_cast(
const any * operand) noexcept
231 return any_cast<ValueType>(
const_cast<any *
>(operand));
234 template<
typename ValueType>
235 ValueType any_cast(
any & operand)
237 typedef typename std::remove_reference<ValueType>::type nonref;
240 nonref * result = any_cast<nonref>(&operand);
242 throw bad_any_cast();
248 typedef typename std::conditional<
249 std::is_reference<ValueType>::value,
251 typename std::add_lvalue_reference<ValueType>::type
254 return static_cast<ref_type
>(*result);
257 template<
typename ValueType>
258 inline ValueType any_cast(
const any & operand)
260 typedef typename std::remove_reference<ValueType>::type nonref;
261 return any_cast<
const nonref &>(
const_cast<any &
>(operand));
264 template<
typename ValueType>
265 inline ValueType any_cast(
any&& operand)
268 std::is_rvalue_reference<ValueType&&>::value
269 || std::is_const<
typename std::remove_reference<ValueType>::type >::value,
270 "momemta::any_cast shall not be used for getting nonconst references to temporary objects" 272 return any_cast<ValueType>(operand);
281 template<
typename ValueType>
282 inline ValueType * unsafe_any_cast(
any * operand) noexcept
284 return &
static_cast<any::holder<ValueType> *
>(operand->content)->held;
287 template<
typename ValueType>
288 inline const ValueType * unsafe_any_cast(
const any * operand) noexcept
290 return unsafe_any_cast<ValueType>(
const_cast<any *
>(operand));