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
71 template<
typename ValueType >
72 Any(
const ValueType& value )
73 : content( new holder< ValueType >( value ))
78 LUNCHBOX_API
Any(
const Any& other );
87 LUNCHBOX_API Any&
swap( Any& rhs );
90 template<
typename ValueType >
93 Any( rhs ).swap( *
this );
104 LUNCHBOX_API
bool empty()
const;
111 LUNCHBOX_API
const std::type_info&
type()
const;
117 LUNCHBOX_API
bool operator == (
const Any& rhs )
const;
123 bool operator != (
const Any& rhs )
const {
return !(*
this == rhs); }
131 virtual ~placeholder() {}
133 virtual bool operator == (
const placeholder& rhs )
const = 0;
136 {
return !(*
this == rhs); }
138 virtual const std::type_info&
type()
const = 0;
140 virtual placeholder* clone()
const = 0;
143 friend class boost::serialization::access;
144 template<
class Archive >
145 void serialize( Archive&,
const unsigned int ) {}
148 BOOST_SERIALIZATION_ASSUME_ABSTRACT(placeholder)
150 template< typename ValueType >
151 class holder : public placeholder
159 explicit holder(
const ValueType& value )
164 virtual const std::type_info&
type()
const
166 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
176 return new holder( held );
184 friend class boost::serialization::access;
185 template<
class Archive >
186 void serialize( Archive & ar,
const unsigned int )
189 ar & boost::serialization::base_object< placeholder >( *this );
196 template<
typename ValueType >
199 template<
typename ValueType >
202 friend class boost::serialization::access;
203 template<
class Archive >
204 void serialize( Archive & ar,
const unsigned int )
209 boost::shared_ptr< placeholder > content;
216 LUNCHBOX_API
bad_any_cast(
const std::string& from,
const std::string& to );
218 virtual const char* what()
const throw() {
return data; }
230 template<
typename ValueType >
234 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
235 std::strcmp(operand->
type().name(),
typeid(ValueType).name()) == 0
237 operand->
type() ==
typeid(ValueType)
239 ? &
static_cast<Any::holder<ValueType> *
>(operand->content.get())->held
249 template<
typename ValueType >
252 return any_cast<ValueType>(
const_cast<Any *
>(operand));
262 template<
typename ValueType >
265 typedef typename boost::remove_reference< ValueType >::type nonref;
267 nonref* result =
any_cast< nonref >( &operand );
269 boost::throw_exception(
271 demangleTypeID(
typeid( nonref ).name( ))));
282 template<
typename ValueType >
285 typedef typename boost::remove_reference< ValueType >::type nonref;
287 return any_cast<
const nonref& >(
const_cast< Any&
>( operand ));
296 template<
typename ValueType >
299 return &
static_cast< Any::holder< ValueType >*
>(
300 operand->content.get( ))->held;
309 template<
typename ValueType >
321 template<
typename ValueType >
324 typedef typename boost::remove_reference< ValueType >::type nonref;
334 template<
typename ValueType >
337 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