1/* Copyright 2003-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/multi_index for library home page.
7 */
8
9#ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_LOADER_HPP
10#define BOOST_MULTI_INDEX_DETAIL_INDEX_LOADER_HPP
11
12#if defined(_MSC_VER)
13#pragma once
14#endif
15
16#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17#include <algorithm>
18#include <boost/core/noncopyable.hpp>
19#include <boost/core/serialization.hpp>
20#include <boost/multi_index/detail/auto_space.hpp>
21#include <boost/multi_index/detail/bad_archive_exception.hpp>
22#include <boost/multi_index/detail/raw_ptr.hpp>
23#include <boost/throw_exception.hpp>
24#include <cstddef>
25
26namespace boost{
27
28namespace multi_index{
29
30namespace detail{
31
32/* Counterpart of index_saver (check index_saver.hpp for serialization
33 * details.)
34 * multi_index_container is in charge of supplying the info about
35 * the base sequence, and each index can subsequently load itself using the
36 * const interface of index_loader.
37 */
38
39template<typename Node,typename FinalNode,typename Allocator>
40class index_loader:private noncopyable
41{
42public:
43 index_loader(const Allocator& al,std::size_t size):
44 spc(al,size),size_(size),n(0),sorted(false)
45 {
46 }
47
48 template<class Archive>
49 void add(Node* node,Archive& ar,const unsigned int)
50 {
51 ar>>core::make_nvp("position",*node);
52 entries()[n++]=node;
53 }
54
55 template<class Archive>
56 void add_track(Node* node,Archive& ar,const unsigned int)
57 {
58 ar>>core::make_nvp("position",*node);
59 }
60
61 /* A rearranger is passed two nodes, and is expected to
62 * reposition the second after the first.
63 * If the first node is 0, then the second should be moved
64 * to the beginning of the sequence.
65 */
66
67 template<typename Rearranger,class Archive>
68 void load(Rearranger r,Archive& ar,const unsigned int)const
69 {
70 FinalNode* prev=unchecked_load_node(ar);
71 if(!prev)return;
72
73 if(!sorted){
74 std::sort(entries(),entries()+size_);
75 sorted=true;
76 }
77
78 check_node(node: prev);
79
80 for(;;){
81 for(;;){
82 FinalNode* node=load_node(ar);
83 if(!node)break;
84
85 if(node==prev)prev=0;
86 r(prev,node);
87
88 prev=node;
89 }
90 prev=load_node(ar);
91 if(!prev)break;
92 }
93 }
94
95private:
96 Node** entries()const{return raw_ptr<Node**>(spc.data());}
97
98 /* We try to delay sorting as much as possible just in case it
99 * is not necessary, hence this version of load_node.
100 */
101
102 template<class Archive>
103 FinalNode* unchecked_load_node(Archive& ar)const
104 {
105 Node* node=0;
106 ar>>core::make_nvp("pointer",node);
107 return static_cast<FinalNode*>(node);
108 }
109
110 template<class Archive>
111 FinalNode* load_node(Archive& ar)const
112 {
113 Node* node=0;
114 ar>>core::make_nvp("pointer",node);
115 check_node(node);
116 return static_cast<FinalNode*>(node);
117 }
118
119 void check_node(Node* node)const
120 {
121 if(node!=0&&!std::binary_search(entries(),entries()+size_,node)){
122 throw_exception(e: bad_archive_exception());
123 }
124 }
125
126 auto_space<Node*,Allocator> spc;
127 std::size_t size_;
128 std::size_t n;
129 mutable bool sorted;
130};
131
132} /* namespace multi_index::detail */
133
134} /* namespace multi_index */
135
136} /* namespace boost */
137
138#endif
139