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
77 template<
typename ValueType >
Any(
const ValueType& value )
78 : content( new holder< ValueType >( value ))
82 LUNCHBOX_API
Any(
const Any& other );
91 LUNCHBOX_API Any&
swap( Any& rhs );
94 template<
typename ValueType >
97 Any( rhs ).swap( *
this );
108 LUNCHBOX_API
bool empty()
const;
115 LUNCHBOX_API
const std::type_info&
type()
const;
121 LUNCHBOX_API
bool operator == (
const Any& rhs )
const;
127 bool operator != (
const Any& rhs )
const {
return !(*
this == rhs); }
135 virtual ~placeholder() {}
137 virtual bool operator == (
const placeholder& rhs )
const = 0;
140 {
return !(*
this == rhs); }
142 virtual const std::type_info&
type()
const = 0;
144 virtual placeholder* clone()
const = 0;
147 friend class boost::serialization::access;
148 template<
class Archive >
149 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
170 return typeid(ValueType);
173 virtual bool operator == (
const placeholder& rhs )
const
175 return held ==
static_cast< const holder&
>( rhs ).held;
178 virtual placeholder* clone()
const
180 return new holder( held );
188 friend class boost::serialization::access;
189 template<
class Archive >
190 void serialize( Archive & ar,
const unsigned int )
193 ar & boost::serialization::base_object< placeholder >( *this );
200 template<
typename ValueType >
203 template<
typename ValueType >
206 friend class boost::serialization::access;
207 template<
class Archive >
208 void serialize( Archive & ar,
const unsigned int )
213 boost::shared_ptr< placeholder > content;
220 LUNCHBOX_API
bad_any_cast(
const std::string& from,
const std::string& to );
222 virtual const char* what()
const throw() {
return data; }
234 template<
typename ValueType >
238 #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
239 std::strcmp(operand->
type().name(),
typeid(ValueType).name()) == 0
241 operand->
type() ==
typeid(ValueType)
243 ? &
static_cast<Any::holder<ValueType> *
>(operand->content.get())->held
253 template<
typename ValueType >
256 return any_cast<ValueType>(
const_cast<Any *
>(operand));
266 template<
typename ValueType >
269 typedef typename boost::remove_reference< ValueType >::type nonref;
271 nonref* result =
any_cast< nonref >( &operand );
273 boost::throw_exception(
275 demangleTypeID(
typeid( nonref ).name( ))));
286 template<
typename ValueType >
289 typedef typename boost::remove_reference< ValueType >::type nonref;
291 return any_cast<
const nonref& >(
const_cast< Any&
>( operand ));
300 template<
typename ValueType >
303 return &
static_cast< Any::holder< ValueType >*
>(
304 operand->content.get( ))->held;
313 template<
typename ValueType >
325 template<
typename ValueType >
328 typedef typename boost::remove_reference< ValueType >::type nonref;
338 template<
typename ValueType >
341 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.
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.
Abstraction layer and common utilities for multi-threaded programming.
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