25 #ifndef LUNCHBOX_ANY_H 26 #define LUNCHBOX_ANY_H 29 #include <lunchbox/debug.h> 31 #include <boost/shared_ptr.hpp> 32 #include <boost/type_traits/remove_reference.hpp> 35 #include <boost/serialization/access.hpp> 36 #include <boost/serialization/assume_abstract.hpp> 37 #include <boost/serialization/base_object.hpp> 38 #include <boost/serialization/extended_type_info.hpp> 39 #include <boost/serialization/shared_ptr.hpp> 40 #include <boost/serialization/singleton.hpp> 41 #include <boost/serialization/type_info_implementation.hpp> 45 #if (defined(__GNUC__) && __GNUC__ >= 3) || defined(_AIX) || \ 46 (defined(__sgi) && defined(__host_mips)) || \ 47 (defined(__hpux) && defined(__HP_aCC)) || \ 48 (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC)) 49 #define BOOST_AUX_ANY_TYPE_ID_NAME 73 template <
typename ValueType>
74 Any(
const ValueType& value)
75 : content(new holder<ValueType>(value))
80 LUNCHBOX_API
Any(
const Any& other);
89 LUNCHBOX_API Any&
swap(Any& rhs);
92 template <
typename ValueType>
106 LUNCHBOX_API
bool empty()
const;
113 LUNCHBOX_API
const std::type_info&
type()
const;
119 LUNCHBOX_API
bool operator==(
const Any& rhs)
const;
125 bool operator!=(
const Any& rhs)
const {
return !(*
this == rhs); }
132 virtual ~placeholder() {}
133 virtual bool operator==(
const placeholder& rhs)
const = 0;
137 return !(*
this == rhs);
140 virtual const std::type_info&
type()
const = 0;
142 virtual placeholder* clone()
const = 0;
145 friend class boost::serialization::access;
146 template <
class Archive>
147 void serialize(Archive&,
const unsigned int)
152 BOOST_SERIALIZATION_ASSUME_ABSTRACT(placeholder)
154 template <
typename ValueType>
155 class holder :
public placeholder
163 explicit holder(
const ValueType& value)
168 virtual const std::type_info&
type()
const {
return typeid(ValueType); }
169 virtual bool operator==(
const placeholder& rhs)
const 171 return held ==
static_cast<const holder&
>(rhs).held;
174 virtual placeholder* clone()
const {
return new holder(held); }
180 friend class boost::serialization::access;
181 template <
class Archive>
182 void serialize(Archive& ar,
const unsigned int )
185 ar& boost::serialization::base_object<placeholder>(*this);
192 template <
typename ValueType>
195 template <
typename ValueType>
198 friend class boost::serialization::access;
199 template <
class Archive>
200 void serialize(Archive& ar,
const unsigned int )
205 boost::shared_ptr<placeholder> content;
212 LUNCHBOX_API
bad_any_cast(
const std::string& from,
const std::string& to);
214 virtual const char* what()
const throw() {
return data; }
225 template <
typename ValueType>
229 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME 230 std::strcmp(operand->
type().name(),
231 typeid(ValueType).name()) == 0
233 operand->
type() ==
typeid(ValueType)
235 ? &
static_cast<Any::holder<ValueType>*
>(operand->content.get())
246 template <
typename ValueType>
249 return any_cast<ValueType>(
const_cast<Any*
>(operand));
259 template <
typename ValueType>
262 typedef typename boost::remove_reference<ValueType>::type nonref;
264 nonref* result =
any_cast<nonref>(&operand);
266 boost::throw_exception(
268 demangleTypeID(
typeid(nonref).name())));
279 template <
typename ValueType>
282 typedef typename boost::remove_reference<ValueType>::type nonref;
284 return any_cast<
const nonref&>(
const_cast<Any&
>(operand));
293 template <
typename ValueType>
296 return &
static_cast<Any::holder<ValueType>*
>(operand->content.get())->held;
305 template <
typename ValueType>
317 template <
typename ValueType>
320 typedef typename boost::remove_reference<ValueType>::type nonref;
330 template <
typename ValueType>
333 typedef typename boost::remove_reference<ValueType>::type nonref;
Defines export visibility macros for library Lunchbox.
Any & operator=(const ValueType &rhs)
Assign a new value to this Any.
friend ValueType * unsafe_any_cast(Any *)
Retrieve the value stored in an Any without type checking.
Any(const ValueType &value)
Construct a new Any with the given value.
bool operator==(const Any &rhs) const
Any()
Construct a new, empty Any.
A specialization for exceptions thrown by an unsuccessful any_cast.
Any & swap(Any &rhs)
Exchange the content of this and rhs.
const std::type_info & type() const
friend ValueType * any_cast(Any *)
Retrieve the value stored in an Any including type checking.
Abstraction layer and common utilities for multi-threaded programming.
bool operator!=(const Any &rhs) const
A class which can hold instances of any type.