| 1 | /* Copyright 2006-2023 Joaquin M Lopez Munoz. |
| 2 | * Distributed under the Boost Software License, Version 1.0. |
| 3 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | * http://www.boost.org/LICENSE_1_0.txt) |
| 5 | * |
| 6 | * See http://www.boost.org/libs/flyweight for library home page. |
| 7 | */ |
| 8 | |
| 9 | #ifndef BOOST_FLYWEIGHT_SERIALIZE_HPP |
| 10 | #define BOOST_FLYWEIGHT_SERIALIZE_HPP |
| 11 | |
| 12 | #if defined(_MSC_VER)&&(_MSC_VER>=1200) |
| 13 | #pragma once |
| 14 | #endif |
| 15 | |
| 16 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ |
| 17 | #include <boost/flyweight/flyweight_fwd.hpp> |
| 18 | #include <boost/flyweight/detail/archive_constructed.hpp> |
| 19 | #include <boost/flyweight/detail/serialization_helper.hpp> |
| 20 | #include <boost/core/serialization.hpp> |
| 21 | #include <boost/throw_exception.hpp> |
| 22 | #include <memory> |
| 23 | #include <stdexcept> |
| 24 | |
| 25 | /* Serialization routines for flyweight<T>. |
| 26 | */ |
| 27 | |
| 28 | namespace boost{ |
| 29 | |
| 30 | namespace flyweights{ |
| 31 | |
| 32 | template< |
| 33 | class Archive, |
| 34 | typename T,typename Arg1,typename Arg2,typename Arg3 |
| 35 | > |
| 36 | inline void serialize( |
| 37 | Archive& ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f, |
| 38 | const unsigned int version) |
| 39 | { |
| 40 | core::split_free(ar,f,version); |
| 41 | } |
| 42 | |
| 43 | template< |
| 44 | class Archive, |
| 45 | typename T,typename Arg1,typename Arg2,typename Arg3 |
| 46 | > |
| 47 | void save( |
| 48 | Archive& ar,const ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f, |
| 49 | const unsigned int /*version*/) |
| 50 | { |
| 51 | typedef ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> flyweight; |
| 52 | typedef ::boost::flyweights::detail::save_helper<flyweight> helper; |
| 53 | typedef typename helper::size_type size_type; |
| 54 | |
| 55 | helper& hlp=ar.template get_helper<helper>(); |
| 56 | |
| 57 | size_type n=hlp.find(f); |
| 58 | ar<<make_nvp("item" ,n); |
| 59 | if(n==hlp.size()){ |
| 60 | ar<<make_nvp("key" ,f.get_key()); |
| 61 | hlp.push_back(f); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | template< |
| 66 | class Archive, |
| 67 | typename T,typename Arg1,typename Arg2,typename Arg3 |
| 68 | > |
| 69 | void load( |
| 70 | Archive& ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f, |
| 71 | const unsigned int version) |
| 72 | { |
| 73 | typedef ::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> flyweight; |
| 74 | typedef typename flyweight::key_type key_type; |
| 75 | typedef ::boost::flyweights::detail::load_helper<flyweight> helper; |
| 76 | typedef typename helper::size_type size_type; |
| 77 | |
| 78 | helper& hlp=ar.template get_helper<helper>(); |
| 79 | |
| 80 | size_type n=0; |
| 81 | ar>>make_nvp("item" ,n); |
| 82 | if(n>hlp.size()){ |
| 83 | throw_exception(e: std::runtime_error("Invalid or corrupted archive" )); |
| 84 | } |
| 85 | else if(n==hlp.size()){ |
| 86 | ::boost::flyweights::detail::archive_constructed<key_type> k( |
| 87 | "key" ,ar,version); |
| 88 | hlp.push_back(flyweight(k.get())); |
| 89 | } |
| 90 | f=hlp[n]; |
| 91 | } |
| 92 | |
| 93 | } /* namespace flyweights */ |
| 94 | |
| 95 | } /* namespace boost */ |
| 96 | |
| 97 | #endif |
| 98 | |