Release 1.39.0
[boost.git] / Boost_1_39_0 / boost / multi_index / detail / index_loader.hpp
blob001c01bb204d735ee863a2054a6fe3007e282372
1 /* Copyright 2003-2008 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)
6 * See http://www.boost.org/libs/multi_index for library home page.
7 */
9 #ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_LOADER_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_INDEX_LOADER_HPP
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
13 #pragma once
14 #endif
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <algorithm>
18 #include <boost/archive/archive_exception.hpp>
19 #include <boost/noncopyable.hpp>
20 #include <boost/multi_index/detail/auto_space.hpp>
21 #include <boost/serialization/nvp.hpp>
22 #include <boost/throw_exception.hpp>
23 #include <cstddef>
25 namespace boost{
27 namespace multi_index{
29 namespace detail{
31 /* Counterpart of index_saver (check index_saver.hpp for serialization
32 * details.)* multi_index_container is in charge of supplying the info about
33 * the base sequence, and each index can subsequently load itself using the
34 * const interface of index_loader.
37 template<typename Node,typename FinalNode,typename Allocator>
38 class index_loader:private noncopyable
40 public:
41 index_loader(const Allocator& al,std::size_t size):
42 spc(al,size),size_(size),n(0),sorted(false)
46 template<class Archive>
47 void add(Node* node,Archive& ar,const unsigned int)
49 ar>>serialization::make_nvp("position",*node);
50 entries()[n++]=node;
53 template<class Archive>
54 void add_track(Node* node,Archive& ar,const unsigned int)
56 ar>>serialization::make_nvp("position",*node);
59 /* A rearranger is passed two nodes, and is expected to
60 * reposition the second after the first.
61 * If the first node is 0, then the second should be moved
62 * to the beginning of the sequence.
65 template<typename Rearranger,class Archive>
66 void load(Rearranger r,Archive& ar,const unsigned int)const
68 FinalNode* prev=unchecked_load_node(ar);
69 if(!prev)return;
71 if(!sorted){
72 std::sort(entries(),entries()+size_);
73 sorted=true;
76 check_node(prev);
78 for(;;){
79 for(;;){
80 FinalNode* node=load_node(ar);
81 if(!node)break;
83 if(node==prev)prev=0;
84 r(prev,node);
86 prev=node;
88 prev=load_node(ar);
89 if(!prev)break;
93 private:
94 Node** entries()const{return &*spc.data();}
96 /* We try to delay sorting as much as possible just in case it
97 * is not necessary, hence this version of load_node.
100 template<class Archive>
101 FinalNode* unchecked_load_node(Archive& ar)const
103 Node* node=0;
104 ar>>serialization::make_nvp("pointer",node);
105 return static_cast<FinalNode*>(node);
108 template<class Archive>
109 FinalNode* load_node(Archive& ar)const
111 Node* node=0;
112 ar>>serialization::make_nvp("pointer",node);
113 check_node(node);
114 return static_cast<FinalNode*>(node);
117 void check_node(Node* node)const
119 if(node!=0&&!std::binary_search(entries(),entries()+size_,node)){
120 throw_exception(
121 archive::archive_exception(
122 archive::archive_exception::other_exception));
126 auto_space<Node*,Allocator> spc;
127 std::size_t size_;
128 std::size_t n;
129 mutable bool sorted;
132 } /* namespace multi_index::detail */
134 } /* namespace multi_index */
136 } /* namespace boost */
138 #endif