sl@0: #ifndef BOOST_SERIALIZATION_VARIANT_HPP
sl@0: #define BOOST_SERIALIZATION_VARIANT_HPP
sl@0: 
sl@0: // MS compatible compilers support #pragma once
sl@0: #if defined(_MSC_VER) && (_MSC_VER >= 1020)
sl@0: # pragma once
sl@0: #endif
sl@0: 
sl@0: #if defined(_MSC_VER) && (_MSC_VER <= 1020)
sl@0: #  pragma warning (disable : 4786) // too long name, harmless warning
sl@0: #endif
sl@0: 
sl@0: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
sl@0: // variant.hpp - non-intrusive serialization of variant types
sl@0: //
sl@0: // copyright (c) 2005   
sl@0: // troy d. straszheim <troy@resophonic.com>
sl@0: // http://www.resophonic.com
sl@0: //
sl@0: // Use, modification and distribution is subject to the Boost Software
sl@0: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0: // http://www.boost.org/LICENSE_1_0.txt)
sl@0: //
sl@0: // See http://www.boost.org for updates, documentation, and revision history.
sl@0: //
sl@0: // thanks to Robert Ramey, Peter Dimov, and Richard Crossley.
sl@0: //
sl@0: 
sl@0: #include <boost/mpl/front.hpp>
sl@0: #include <boost/mpl/pop_front.hpp>
sl@0: #include <boost/mpl/eval_if.hpp>
sl@0: #include <boost/mpl/identity.hpp>
sl@0: #include <boost/mpl/size.hpp>
sl@0: #include <boost/mpl/empty.hpp>
sl@0: 
sl@0: #include <boost/throw_exception.hpp>
sl@0: 
sl@0: #include <boost/variant.hpp>
sl@0: 
sl@0: #include <boost/archive/archive_exception.hpp>
sl@0: 
sl@0: #include <boost/serialization/split_free.hpp>
sl@0: #include <boost/serialization/serialization.hpp>
sl@0: 
sl@0: namespace boost {
sl@0: namespace serialization {
sl@0: 
sl@0: template<class Archive>
sl@0: struct variant_save_visitor : boost::static_visitor<> {
sl@0:     variant_save_visitor(Archive& ar) :
sl@0:         m_ar(ar)
sl@0:     {}
sl@0:     template<class T>
sl@0:     void operator()(T const & value) const
sl@0:     {
sl@0:         m_ar << BOOST_SERIALIZATION_NVP(value);
sl@0:     }
sl@0: private:
sl@0:     Archive & m_ar;
sl@0: };
sl@0: 
sl@0: template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
sl@0: void save(
sl@0:     Archive & ar,
sl@0:     boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const & v,
sl@0:     unsigned int version
sl@0: ){
sl@0:     int which = v.which();
sl@0:     ar << BOOST_SERIALIZATION_NVP(which);
sl@0:     typedef BOOST_DEDUCED_TYPENAME  boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types types;
sl@0:     variant_save_visitor<Archive> visitor(ar);
sl@0:     v.apply_visitor(visitor);
sl@0: }
sl@0: 
sl@0: template<class S>
sl@0: struct variant_impl {
sl@0: 
sl@0:     struct load_null {
sl@0:         template<class Archive, class V>
sl@0:         static void invoke(
sl@0:             Archive & ar,
sl@0:             int which,
sl@0:             V & v,
sl@0:             unsigned int version
sl@0:         ){}
sl@0:     };
sl@0: 
sl@0:     struct load_impl {
sl@0:         template<class Archive, class V>
sl@0:         static void invoke(
sl@0:             Archive & ar,
sl@0:             int which,
sl@0:             V & v,
sl@0:             unsigned int version
sl@0:         ){
sl@0:             if(which == 0){
sl@0:                 // note: A non-intrusive implementation (such as this one)
sl@0:                 // necessary has to copy the value.  This wouldn't be necessary
sl@0:                 // with an implementation that de-serialized to the address of the
sl@0:                 // aligned storage included in the variant.
sl@0:                 typedef BOOST_DEDUCED_TYPENAME mpl::front<S>::type head_type;
sl@0:                 head_type value;
sl@0:                 ar >> BOOST_SERIALIZATION_NVP(value);
sl@0:                 v = value;
sl@0:                 ar.reset_object_address(& boost::get<head_type>(v), & value);
sl@0:                 return;
sl@0:             }
sl@0:             typedef BOOST_DEDUCED_TYPENAME mpl::pop_front<S>::type type;
sl@0:             variant_impl<type>::load(ar, which - 1, v, version);
sl@0:         }
sl@0:     };
sl@0: 
sl@0:     template<class Archive, class V>
sl@0:     static void load(
sl@0:         Archive & ar,
sl@0:         int which,
sl@0:         V & v,
sl@0:         unsigned int version
sl@0:     ){
sl@0:         typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<mpl::empty<S>,
sl@0:             mpl::identity<load_null>,
sl@0:             mpl::identity<load_impl>
sl@0:         >::type typex;
sl@0:         typex::invoke(ar, which, v, version);
sl@0:     }
sl@0: 
sl@0: };
sl@0: 
sl@0: template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
sl@0: void load(
sl@0:     Archive & ar, 
sl@0:     boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& v,
sl@0:     unsigned int version
sl@0: ){
sl@0:     int which;
sl@0:     typedef BOOST_DEDUCED_TYPENAME boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types types;
sl@0:     ar >> BOOST_SERIALIZATION_NVP(which);
sl@0:     if(which >=  mpl::size<types>::value)
sl@0:         // this might happen if a type was removed from the list of variant types
sl@0:         boost::throw_exception(
sl@0:             boost::archive::archive_exception(
sl@0:                 boost::archive::archive_exception::unsupported_version
sl@0:             )
sl@0:         );
sl@0:     variant_impl<types>::load(ar, which, v, version);
sl@0: }
sl@0: 
sl@0: template<class Archive,BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
sl@0: inline void serialize(
sl@0:     Archive & ar,
sl@0:     boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> & v,
sl@0:     unsigned int file_version
sl@0: ){
sl@0:     split_free(ar,v,file_version);
sl@0: }
sl@0: 
sl@0: } // namespace serialization
sl@0: } // namespace boost
sl@0: 
sl@0: #endif //BOOST_SERIALIZATION_VARIANT_HPP
sl@0: