// See http://www.boost.org/libs/any for Documentation. #ifndef BOOST_DOUBLE_SIZED_ANY_INCLUDED #define BOOST_DOUBLE_SIZED_ANY_INCLUDED // what: variant type boost::any // who: contributed by Kevlin Henney, // with features contributed and bugs found by // Ed Brey, Mark Rodgers, Peter Dimov, and James Curran // when: July 2001 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95 #include #include #include "boost/config.hpp" #include #include #include #include // See boost/python/type_id.hpp // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp # if (defined(__GNUC__) && __GNUC__ >= 3) \ || defined(_AIX) \ || ( defined(__sgi) && defined(__host_mips)) \ || (defined(__hpux) && defined(__HP_aCC)) \ || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC)) # define BOOST_AUX_ANY_TYPE_ID_NAME #include # endif namespace boost { class doubleSized_any { public: // structors doubleSized_any() : content(0), id(0) { } template doubleSized_any(const ValueType & value) : content(new holder(value)), id(&typeid(ValueType)) { } doubleSized_any(const doubleSized_any & other) : content(other.content ? other.content->clone() : 0), id(other.id) { } ~doubleSized_any() { delete content; } public: // modifiers doubleSized_any & swap(doubleSized_any & rhs) { std::swap(content, rhs.content); std::swap(id, rhs.id); return *this; } template doubleSized_any & operator=(const ValueType & rhs) { doubleSized_any(rhs).swap(*this); return *this; } doubleSized_any & operator=(doubleSized_any rhs) { rhs.swap(*this); return *this; } public: // queries bool empty() const { return !content; } const std::type_info & type() const { return id ? *id : typeid(void); } #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // types #else public: // types (public so any_cast can be non-friend) #endif class placeholder { public: // structors virtual ~placeholder() { } public: // queries virtual placeholder * clone() const = 0; }; template class holder : public placeholder { public: // structors holder(const ValueType & value) : held(value) { } public: // queries virtual placeholder * clone() const { return new holder(held); } public: // representation ValueType held; private: // intentionally left unimplemented holder & operator=(const holder &); }; #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // representation template friend ValueType * any_cast(doubleSized_any *); template friend ValueType * unsafe_any_cast(doubleSized_any *); #else public: // representation (public so any_cast can be non-friend) #endif placeholder * content; const std::type_info * id; }; #ifndef BOOST_ANY_INCLUDED class bad_any_cast : public std::bad_cast { public: virtual const char * what() const throw() { return "boost::bad_any_cast: " "failed conversion using boost::any_cast"; } }; #endif template ValueType * any_cast(doubleSized_any * operand) { return operand && #ifdef BOOST_AUX_ANY_TYPE_ID_NAME std::strcmp(operand->id->name(), typeid(ValueType).name()) == 0 #else operand->id == &typeid(ValueType) #endif ? &static_cast *>(operand->content)->held : 0; } template inline const ValueType * any_cast(const doubleSized_any * operand) { return any_cast(const_cast(operand)); } template ValueType any_cast(doubleSized_any & operand) { typedef BOOST_DEDUCED_TYPENAME remove_reference::type nonref; #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // If 'nonref' is still reference type, it means the user has not // specialized 'remove_reference'. // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro // to generate specialization of remove_reference for your class // See type traits library documentation for details BOOST_STATIC_ASSERT(!is_reference::value); #endif nonref * result = any_cast(&operand); if(!result) boost::throw_exception(bad_any_cast()); return *result; } template inline ValueType any_cast(const doubleSized_any & operand) { typedef BOOST_DEDUCED_TYPENAME remove_reference::type nonref; #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // The comment in the above version of 'any_cast' explains when this // assert is fired and what to do. BOOST_STATIC_ASSERT(!is_reference::value); #endif return any_cast(const_cast(operand)); } // Note: The "unsafe" versions of any_cast are not part of the // public interface and may be removed at any time. They are // required where we know what type is stored in the any and can't // use typeid() comparison, e.g., when our types may travel across // different shared libraries. template inline ValueType * unsafe_any_cast(doubleSized_any * operand) { return &static_cast *>(operand->content)->held; } template inline const ValueType * unsafe_any_cast(const doubleSized_any * operand) { return unsafe_any_cast(const_cast(operand)); } } // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #endif