26 #ifndef LUNCHBOX_ANY_H
27 #define LUNCHBOX_ANY_H
30 #include <lunchbox/debug.h>
32 #include <boost/type_traits/remove_reference.hpp>
33 #include <boost/shared_ptr.hpp>
36 #include <boost/serialization/singleton.hpp>
37 #include <boost/serialization/extended_type_info.hpp>
38 #include <boost/serialization/access.hpp>
39 #include <boost/serialization/assume_abstract.hpp>
40 #include <boost/serialization/base_object.hpp>
41 #include <boost/serialization/type_info_implementation.hpp>
42 #include <boost/serialization/shared_ptr.hpp>
46 # if (defined(__GNUC__) && __GNUC__ >= 3) \
48 || ( defined(__sgi) && defined(__host_mips)) \
49 || (defined(__hpux) && defined(__HP_aCC)) \
50 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
51 # define BOOST_AUX_ANY_TYPE_ID_NAME
76 template<
typename ValueType >
77 Any(
const ValueType& value )
78 : content( new holder< ValueType >( value ))
83 LUNCHBOX_API
Any(
const Any& other );
92 LUNCHBOX_API Any&
swap( Any& rhs );
95 template<
typename ValueType >
98 Any( rhs ).swap( *
this );
109 LUNCHBOX_API
bool empty()
const;
116 LUNCHBOX_API
const std::type_info&
type()
const;
122 LUNCHBOX_API
bool operator == (
const Any& rhs )
const;
128 bool operator != (
const Any& rhs )
const {
return !(*
this == rhs); }
136 virtual ~placeholder() {}
138 virtual bool operator == (
const placeholder& rhs )
const = 0;
141 {
return !(*
this == rhs); }
143 virtual const std::type_info&
type()
const = 0;
145 virtual placeholder* clone()
const = 0;
148 friend class boost::serialization::access;
149 template<
class Archive >
150 void serialize( Archive&,
const unsigned int ) {}
153 BOOST_SERIALIZATION_ASSUME_ABSTRACT(placeholder)
155 template< typename ValueType >
156 class holder : public placeholder
164 explicit holder(
const ValueType& value )
169 virtual const std::type_info&
type()
const
171 return typeid(ValueType);
174 virtual bool operator == (
const placeholder& rhs )
const
176 return held ==
static_cast< const holder&
>( rhs ).held;
179 virtual placeholder* clone()
const
181 return new holder( held );
189 friend class boost::serialization::access;
190 template<
class Archive >
191 void serialize( Archive & ar,
const unsigned int )
194 ar & boost::serialization::base_object< placeholder >( *this );
201 template<
typename ValueType >
204 template<
typename ValueType >
207 friend class boost::serialization::access;
208 template<
class Archive >
209 void serialize( Archive & ar,
const unsigned int )
214 boost::shared_ptr< placeholder > content;
221 LUNCHBOX_API
bad_any_cast(
const std::string& from,
const std::string& to );
223 virtual const char* what()
const throw() {
return data; }
235 template<
typename ValueType >
239 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
240 std::strcmp(operand->
type().name(),
typeid(ValueType).name()) == 0
242 operand->
type() ==
typeid(ValueType)
244 ? &
static_cast<Any::holder<ValueType> *
>(operand->content.get())->held
254 template<
typename ValueType >
257 return any_cast<ValueType>(
const_cast<Any *
>(operand));
267 template<
typename ValueType >
270 typedef typename boost::remove_reference< ValueType >::type nonref;
272 nonref* result =
any_cast< nonref >( &operand );
274 boost::throw_exception(
276 demangleTypeID(
typeid( nonref ).name( ))));
287 template<
typename ValueType >
290 typedef typename boost::remove_reference< ValueType >::type nonref;
292 return any_cast<
const nonref& >(
const_cast< Any&
>( operand ));
301 template<
typename ValueType >
304 return &
static_cast< Any::holder< ValueType >*
>(
305 operand->content.get( ))->held;
314 template<
typename ValueType >
326 template<
typename ValueType >
329 typedef typename boost::remove_reference< ValueType >::type nonref;
339 template<
typename ValueType >
342 typedef typename boost::remove_reference< ValueType >::type nonref;
Defines export visibility macros for Lunchbox.
Any & operator=(const ValueType &rhs)
Assign a new value to this Any.
LUNCHBOX_API const std::type_info & type() const
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.
LUNCHBOX_API bool empty() const
LUNCHBOX_API Any & swap(Any &rhs)
Exchange the content of this and rhs.
A specialization for exceptions thrown by an unsuccessful any_cast.
LUNCHBOX_API Any()
Construct a new, empty Any.
LUNCHBOX_API ~Any()
Destruct this Any.
friend ValueType * any_cast(Any *)
Retrieve the value stored in an Any including type checking.
bool operator!=(const Any &rhs) const
A class which can hold instances of any type.
ValueType * unsafe_any_cast(Any *operand)
Retrieve the value stored in an Any without type checking.
ValueType * any_cast(Any *operand)
Retrieve the value stored in an Any including type checking.
LUNCHBOX_API bool operator==(const Any &rhs) const