fix doc example typo
[boost.git] / boost / serialization / collections_load_imp.hpp
blob04e50519938238fa8717616b11f74b286f3ea080
1 #ifndef BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
2 #define BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
6 # pragma once
7 #endif
9 #if defined(_MSC_VER) && (_MSC_VER <= 1020)
10 # pragma warning (disable : 4786) // too long name, harmless warning
11 #endif
13 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
14 // collections_load_imp.hpp: serialization for loading stl collections
16 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
17 // Use, modification and distribution is subject to the Boost Software
18 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 // See http://www.boost.org for updates, documentation, and revision history.
23 // helper function templates for serialization of collections
25 #include <cassert>
26 #include <cstddef> // size_t
27 #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
28 #if defined(BOOST_NO_STDC_NAMESPACE)
29 namespace std{
30 using ::size_t;
31 } // namespace std
32 #endif
33 #include <boost/detail/workaround.hpp>
35 #include <boost/serialization/access.hpp>
36 #include <boost/serialization/nvp.hpp>
37 #include <boost/serialization/detail/stack_constructor.hpp>
38 #include <boost/serialization/collection_size_type.hpp>
41 namespace boost{
42 namespace serialization {
43 namespace stl {
45 //////////////////////////////////////////////////////////////////////
46 // implementation of serialization for STL containers
49 // sequential container input
50 template<class Archive, class Container>
51 struct archive_input_seq
53 inline void operator()(
54 Archive &ar,
55 Container &s,
56 const unsigned int v
58 typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
59 detail::stack_construct<Archive, type> t(ar, v);
60 // borland fails silently w/o full namespace
61 ar >> boost::serialization::make_nvp("item", t.reference());
62 s.push_back(t.reference());
63 ar.reset_object_address(& s.back() , & t.reference());
67 // map input
68 template<class Archive, class Container>
69 struct archive_input_map
71 inline void operator()(
72 Archive &ar,
73 Container &s,
74 const unsigned int v
76 typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
77 detail::stack_construct<Archive, type> t(ar, v);
78 // borland fails silently w/o full namespace
79 ar >> boost::serialization::make_nvp("item", t.reference());
80 std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
81 s.insert(t.reference());
82 // note: the following presumes that the map::value_type was NOT tracked
83 // in the archive. This is the usual case, but here there is no way
84 // to determine that.
85 if(result.second){
86 ar.reset_object_address(
87 & (result.first->second),
88 & t.reference().second
94 // multimap input
95 template<class Archive, class Container>
96 struct archive_input_multimap
98 inline void operator()(
99 Archive &ar,
100 Container &s,
101 const unsigned int v
103 typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
104 detail::stack_construct<Archive, type> t(ar, v);
105 // borland fails silently w/o full namespace
106 ar >> boost::serialization::make_nvp("item", t.reference());
107 BOOST_DEDUCED_TYPENAME Container::const_iterator result
108 = s.insert(t.reference());
109 // note: the following presumes that the map::value_type was NOT tracked
110 // in the archive. This is the usual case, but here there is no way
111 // to determine that.
112 ar.reset_object_address(
113 & result->second,
114 & t.reference()
119 // set input
120 template<class Archive, class Container>
121 struct archive_input_set
123 inline void operator()(
124 Archive &ar,
125 Container &s,
126 const unsigned int v
128 typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
129 detail::stack_construct<Archive, type> t(ar, v);
130 // borland fails silently w/o full namespace
131 ar >> boost::serialization::make_nvp("item", t.reference());
132 std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
133 s.insert(t.reference());
134 if(result.second)
135 ar.reset_object_address(& (* result.first), & t.reference());
139 // multiset input
140 template<class Archive, class Container>
141 struct archive_input_multiset
143 inline void operator()(
144 Archive &ar,
145 Container &s,
146 const unsigned int v
148 typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
149 detail::stack_construct<Archive, type> t(ar, v);
150 // borland fails silently w/o full namespace
151 ar >> boost::serialization::make_nvp("item", t.reference());
152 BOOST_DEDUCED_TYPENAME Container::const_iterator result
153 = s.insert(t.reference());
154 ar.reset_object_address(& (* result), & t.reference());
158 template<class Container>
159 class reserve_imp
161 public:
162 void operator()(Container &s, std::size_t count) const {
163 s.reserve(count);
167 template<class Container>
168 class no_reserve_imp
170 public:
171 void operator()(Container & /* s */, std::size_t /* count */) const{}
174 template<class Archive, class Container, class InputFunction, class R>
175 inline void load_collection(Archive & ar, Container &s)
177 s.clear();
178 // retrieve number of elements
179 collection_size_type count;
180 unsigned int item_version;
181 ar >> BOOST_SERIALIZATION_NVP(count);
182 if(3 < ar.get_library_version())
183 ar >> BOOST_SERIALIZATION_NVP(item_version);
184 else
185 item_version = 0;
186 R rx;
187 rx(s, count);
188 std::size_t c = count;
189 InputFunction ifunc;
190 while(c-- > 0){
191 ifunc(ar, s, item_version);
195 } // namespace stl
196 } // namespace serialization
197 } // namespace boost
199 #endif //BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP