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>
34 #include <boost/serialization/access.hpp>
35 #include <boost/serialization/assume_abstract.hpp>
36 #include <boost/serialization/base_object.hpp>
37 #include <boost/serialization/shared_ptr.hpp>
41 # if (defined(__GNUC__) && __GNUC__ >= 3) \
43 || ( defined(__sgi) && defined(__host_mips)) \
44 || (defined(__hpux) && defined(__HP_aCC)) \
45 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
46 # define BOOST_AUX_ANY_TYPE_ID_NAME
70 template<
typename ValueType >
71 Any(
const ValueType& value )
72 : content( new holder< ValueType >( value ))
77 LUNCHBOX_API
Any(
const Any& other );
86 LUNCHBOX_API Any&
swap( Any& rhs );
89 template<
typename ValueType >
92 Any( rhs ).swap( *
this );
103 LUNCHBOX_API
bool empty()
const;
110 LUNCHBOX_API
const std::type_info&
type()
const;
116 LUNCHBOX_API
bool operator == (
const Any& rhs )
const;
122 bool operator != (
const Any& rhs )
const {
return !(*
this == rhs); }
130 virtual ~placeholder() {}
132 virtual bool operator == (
const placeholder& rhs )
const = 0;
135 {
return !(*
this == rhs); }
137 virtual const std::type_info&
type()
const = 0;
139 virtual placeholder* clone()
const = 0;
142 friend class boost::serialization::access;
143 template<
class Archive >
144 void serialize( Archive & ar,
const unsigned int version )
149 BOOST_SERIALIZATION_ASSUME_ABSTRACT(placeholder)
151 template< typename ValueType >
152 class holder : public placeholder
160 holder(
const ValueType& value )
165 virtual const std::type_info&
type()
const
167 return typeid(ValueType);
170 virtual bool operator == (
const placeholder& rhs )
const
172 return held ==
static_cast< const holder&
>( rhs ).held;
175 virtual placeholder* clone()
const
177 return new holder( held );
185 friend class boost::serialization::access;
186 template<
class Archive >
187 void serialize( Archive & ar,
const unsigned int version )
190 ar & boost::serialization::base_object< placeholder >( *this );
197 template<
typename ValueType >
200 template<
typename ValueType >
203 friend class boost::serialization::access;
204 template<
class Archive >
205 void serialize( Archive & ar,
const unsigned int version )
210 boost::shared_ptr< placeholder > content;
217 LUNCHBOX_API
bad_any_cast(
const std::string& from,
const std::string& to );
219 virtual const char* what()
const throw() {
return data; }
231 template<
typename ValueType >
235 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
236 std::strcmp(operand->
type().name(),
typeid(ValueType).name()) == 0
238 operand->
type() ==
typeid(ValueType)
240 ? &
static_cast<Any::holder<ValueType> *
>(operand->content.get())->held
250 template<
typename ValueType >
253 return any_cast<ValueType>(
const_cast<Any *
>(operand));
263 template<
typename ValueType >
266 typedef typename boost::remove_reference< ValueType >::type nonref;
268 nonref* result =
any_cast< nonref >( &operand );
270 boost::throw_exception(
272 demangleTypeID(
typeid( nonref ).name( ))));
283 template<
typename ValueType >
286 typedef typename boost::remove_reference< ValueType >::type nonref;
288 return any_cast<
const nonref& >(
const_cast< Any&
>( operand ));
297 template<
typename ValueType >
300 return &
static_cast< Any::holder< ValueType >*
>(
301 operand->content.get( ))->held;
310 template<
typename ValueType >
322 template<
typename ValueType >
325 typedef typename boost::remove_reference< ValueType >::type nonref;
335 template<
typename ValueType >
338 typedef typename boost::remove_reference< ValueType >::type nonref;