#ifndef BOOST_ANY2_INCLUDED #define BOOST_ANY2_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 "boost/smart_ptr.hpp" namespace boost { class any2 { public: // structors any2() : content(0) { } template any2(const ValueType & value) : content(new holder(value)) { } any2(const any2 & other) : content(other.content) { } ~any2() { } public: // modifiers any2 & swap(any2 & rhs) { std::swap(content, rhs.content); return *this; } template any2 & operator=(const ValueType & rhs) { any2(rhs).swap(*this); return *this; } any2 & operator=(const any2 & rhs) { any2(rhs).swap(*this); return *this; } public: // queries bool empty() const { return !content.get(); } const std::type_info & type() const { return content.get() ? content->type() : 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 const std::type_info & type() const = 0; }; template class holder : public placeholder { public: // structors holder(const ValueType & value) : held(value) { } public: // queries virtual const std::type_info & type() const { return typeid(ValueType); } public: // representation ValueType held; }; #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // representation template friend ValueType * any_cast(any2 *); #else public: // representation (public so any_cast can be non-friend) #endif shared_ptr content; }; class bad_any_cast2 : public std::bad_cast { public: virtual const char * what() const throw() { return "boost::bad_any_cast2: " "failed conversion using boost::any_cast"; } }; template ValueType * any_cast(any2 * operand) { return operand && operand->type() == typeid(ValueType) ? &static_cast *>(operand->content)->held : 0; } template const ValueType * any_cast(const any2 * operand) { return any_cast(const_cast(operand)); } template ValueType any_cast(const any2 & operand) { const ValueType * result = any_cast(&operand); if(!result) throw bad_any_cast(); return *result; } } // Copyright Kevlin Henney, 2000, 2001. All rights reserved. // // Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // permissions notice appear in all copies and derivatives, and that no // charge may be made for the software and its documentation except to cover // cost of distribution. // // This software is provided "as is" without express or implied warranty. #endif