// 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 // 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) // // Demonstration of autoregistering serializable any hack, // 2008, troy d. straszheim // // Tested with boost 1.36.0 on 2008-08-09 with gcc 4.2 // #include #include #include #include #include #include "boost/config.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { class any { friend class boost::serialization::access; public: // structors any() : content(0) { } template any(const ValueType & value) : content(new holder(value)) { } any(const any & other) : content(other.content ? other.content->clone() : 0) { } ~any() { delete content; } public: // modifiers any & swap(any & rhs) { std::swap(content, rhs.content); return *this; } template any & operator=(const ValueType & rhs) { any(rhs).swap(*this); return *this; } any & operator=(const any & rhs) { any(rhs).swap(*this); return *this; } public: // queries bool empty() const { return !content; } const std::type_info & type() const { return content ? content->type() : typeid(void); } template void serialize(Archive& ar, unsigned version) { ar & boost::serialization::make_nvp("placeholder", content); } #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; virtual placeholder * clone() const = 0; template void serialize(Archive& ar, unsigned version) { } }; template class holder : public placeholder { public: // structors holder(const ValueType & value) : held(value) { reg; } public: // queries virtual const std::type_info & type() const { return typeid(ValueType); } virtual placeholder * clone() const { return new holder(held); } template void serialize(Archive& ar, unsigned version) { ar & boost::serialization::make_nvp("base", boost::serialization::base_object(*this)); ar & boost::serialization::make_nvp("held", held); } public: // representation ValueType held; private: holder & operator=(const holder &); // intentionally left unimplemented static ::boost::archive::detail::guid_initializer< holder > const & reg; }; #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS private: // representation template friend ValueType * any_cast(any *); template friend ValueType * unsafe_any_cast(any *); #else public: // representation (public so any_cast can be non-friend) #endif placeholder * content; }; template ::boost::archive::detail::guid_initializer< any::holder > const& any::holder::reg = ::boost::serialization::singleton< ::boost::archive::detail::guid_initializer< holder > >::get_mutable_instance().export_guid(typeid(holder).name()); 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"; } }; template ValueType * any_cast(any * operand) { return operand && operand->type() == typeid(ValueType) ? &static_cast *>(operand->content)->held : 0; } template inline const ValueType * any_cast(const any * operand) { return any_cast(const_cast(operand)); } template ValueType any_cast(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 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(any * operand) { return &static_cast *>(operand->content)->held; } template inline const ValueType * unsafe_any_cast(const any * operand) { return unsafe_any_cast(const_cast(operand)); } } namespace boost { namespace serialization { template inline void save_construct_data(Archive & ar, const ::boost::any::holder * h, const unsigned int file_version) { // save data required to construct instance ar & boost::serialization::make_nvp("held", h->held); } template inline void load_construct_data(Archive & ar, ::boost::any::holder * h, const unsigned int version) { // retrieve data from archive required to construct new instance Held held; ar & boost::serialization::make_nvp("held", held); // invoke inplace constructor to initialize instance of my_class ::new(h)::boost::any::holder(held); } } } // namespace ... int main(int, char**) { std::vector v; v.push_back('a'); v.push_back('b'); boost::any a, b, c, d; a = 1; b = 3.14159; c = std::string("hooah"); d = v; std::ofstream ofs("anys.xml"); { boost::archive::xml_oarchive xoa(ofs); xoa & boost::serialization::make_nvp("a", a); xoa & boost::serialization::make_nvp("b", b); xoa & boost::serialization::make_nvp("c", c); xoa & boost::serialization::make_nvp("d", d); } ofs.close(); boost::any e, f, g, h; std::ifstream ifs("anys.xml"); boost::archive::xml_iarchive xia(ifs); xia & boost::serialization::make_nvp("e", e); std::cout << " should be 1: " << boost::any_cast(e) << "\n"; xia & boost::serialization::make_nvp("f", f); std::cout << " should be 3.14159: " << boost::any_cast(f) << "\n"; xia & boost::serialization::make_nvp("g", g); std::cout << " should be 'hooah': " << boost::any_cast(g) << "\n"; xia & boost::serialization::make_nvp("h", h); v = boost::any_cast >(h); std::cout << " should be [a, b]: [" << v[0] << "," << v[1] << "]\n"; ifs.close(); }