source: NonGTP/Boost/boost/serialization/collections_load_imp.hpp @ 857

Revision 857, 5.8 KB checked in by igarcia, 19 years ago (diff)
Line 
1#ifndef  BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
2#define BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9#if defined(_MSC_VER) && (_MSC_VER <= 1020)
10#  pragma warning (disable : 4786) // too long name, harmless warning
11#endif
12
13/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
14// collections_load_imp.hpp: serialization for loading stl collections
15
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)
20
21//  See http://www.boost.org for updates, documentation, and revision history.
22
23// helper function templates for serialization of collections
24
25#include <cassert>
26#include <boost/config.hpp>
27#include <boost/detail/workaround.hpp>
28
29#include <boost/aligned_storage.hpp>
30
31#include <boost/serialization/access.hpp>
32#include <boost/serialization/nvp.hpp>
33#include <boost/serialization/serialization.hpp>
34#include <boost/serialization/detail/stack_constructor.hpp>
35
36namespace boost{
37namespace serialization {
38namespace stl {
39
40//////////////////////////////////////////////////////////////////////
41// implementation of serialization for STL containers
42//
43
44// sequential container input
45template<class Archive, class Container>
46struct archive_input_seq
47{
48    inline void operator()(Archive &ar, Container &s)
49    {
50        detail::stack_construct<Archive, BOOST_DEDUCED_TYPENAME Container::value_type> t(ar);
51        // borland fails silently w/o full namespace
52        ar >> boost::serialization::make_nvp("item", t.reference());
53        s.push_back(t.reference());
54        ar.reset_object_address(& s.back() , & t.reference());
55    }
56};
57
58// map input
59template<class Archive, class Container>
60struct archive_input_map
61{
62    inline void operator()(Archive &ar, Container &s)
63    {
64        detail::stack_construct<Archive, BOOST_DEDUCED_TYPENAME Container::value_type> t(ar);
65        // borland fails silently w/o full namespace
66        ar >> boost::serialization::make_nvp("item", t.reference());
67        std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
68            s.insert(t.reference());
69        assert(result.second); // make sure we inserted a new element
70        ar.reset_object_address(& (* result.first), & t.reference());
71    }
72};
73
74// set input
75template<class Archive, class Container>
76struct archive_input_set
77{
78    inline void operator()(Archive &ar, Container &s)
79    {   
80        typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
81        detail::stack_construct<Archive, type> t(ar);
82        // borland fails silently w/o full namespace
83        ar >> boost::serialization::make_nvp("item", t.reference());
84        std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
85            s.insert(t.reference());
86        assert(result.second); // make sure we inserted a new element
87        ar.reset_object_address(& (* result.first), & t.reference());
88    }
89};
90
91// multimap input
92template<class Archive, class Container>
93struct archive_input_multimap
94{
95    inline void operator()(Archive &ar, Container &s)
96    {
97        detail::stack_construct<Archive, BOOST_DEDUCED_TYPENAME Container::value_type> t(ar);
98        // borland fails silently w/o full namespace
99        ar >> boost::serialization::make_nvp("item", t.reference());
100        BOOST_DEDUCED_TYPENAME Container::const_iterator result
101            = s.insert(t.reference());
102        ar.reset_object_address(& (* result), & t.reference());
103    }
104};
105
106// multiset input
107template<class Archive, class Container>
108struct archive_input_multiset
109{
110    inline void operator()(Archive &ar, Container &s)
111    {   
112        typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
113        detail::stack_construct<Archive, type> t(ar);
114        // borland fails silently w/o full namespace
115        ar >> boost::serialization::make_nvp("item", t.reference());
116        BOOST_DEDUCED_TYPENAME Container::const_iterator result
117            = s.insert(t.reference());
118        ar.reset_object_address(& (* result), & t.reference());
119    }
120};
121
122template<class Container>
123class reserve_imp
124{
125public:
126    void operator()(Container &s, unsigned int count) const {
127        s.reserve(count);
128    }
129};
130
131template<class Container>
132class no_reserve_imp
133{
134public:
135    void operator()(Container & /* s */, unsigned int /* count */) const{}
136};
137
138template<class Archive, class Container, class InputFunction, class R>
139inline void rebuild_collection(Archive & ar, Container &s)
140{
141    s.clear();
142    // retrieve number of elements
143    unsigned int count;
144    ar >> BOOST_SERIALIZATION_NVP(count);
145    R rx;
146    rx(s, count);
147    InputFunction ifunc;
148    while(count-- > 0){
149        ifunc(ar, s);
150    }
151}
152
153template<class Archive, class Container>
154inline void copy_collection(Archive & ar, Container &s)
155{
156    // retrieve number of elements
157    unsigned int count;
158    ar >> BOOST_SERIALIZATION_NVP(count);
159    assert(count == s.size());
160    BOOST_DEDUCED_TYPENAME Container::iterator it = s.begin();
161    while(count-- > 0){
162        ar >> boost::serialization::make_nvp("item", *it++);
163    }
164}
165
166template<class Archive, class Container, class InputFunction, class R>
167inline void load_collection(Archive & ar, Container &s){
168//    if(0 != (ar.get_flags() & boost::archive::no_object_creation))
169//        copy_collection<Archive, Container>(ar, s);
170//    else
171        rebuild_collection<Archive, Container, InputFunction, R>(ar, s);
172}
173
174
175} // namespace stl
176} // namespace serialization
177} // namespace boost
178
179#endif //BOOST_SERIALIZATION_COLLECTIONS_LOAD_IMP_HPP
Note: See TracBrowser for help on using the repository browser.