• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2006-2015 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/serialization/nvp.hpp>
21 #include <boost/serialization/split_free.hpp>
22 #include <boost/throw_exception.hpp>
23 #include <memory>
24 
25 /* Serialization routines for flyweight<T>.
26  */
27 
28 namespace boost{
29 
30 namespace serialization{
31 
32 template<
33   class Archive,
34   typename T,typename Arg1,typename Arg2,typename Arg3
35 >
serialize(Archive & ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> & f,const unsigned int version)36 inline void serialize(
37   Archive& ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3>& f,
38   const unsigned int version)
39 {
40   split_free(ar,f,version);
41 }
42 
43 template<
44   class Archive,
45   typename T,typename Arg1,typename Arg2,typename Arg3
46 >
save(Archive & ar,const::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> & f,const unsigned int)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 >
load(Archive & ar,::boost::flyweights::flyweight<T,Arg1,Arg2,Arg3> & f,const unsigned int version)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(
84       archive::archive_exception(archive::archive_exception::other_exception));
85   }
86   else if(n==hlp.size()){
87     ::boost::flyweights::detail::archive_constructed<key_type> k(
88       "key",ar,version);
89     hlp.push_back(flyweight(k.get()));
90   }
91   f=hlp[n];
92 }
93 
94 } /* namespace serialization */
95 
96 } /* namespace boost */
97 
98 #endif
99