Chris@16
|
1 /* Copyright 2003-2013 Joaquin M Lopez Munoz.
|
Chris@16
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 *
|
Chris@16
|
6 * See http://www.boost.org/libs/multi_index for library home page.
|
Chris@16
|
7 */
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_MULTI_INDEX_DETAIL_SAFE_MODE_HPP
|
Chris@16
|
10 #define BOOST_MULTI_INDEX_DETAIL_SAFE_MODE_HPP
|
Chris@16
|
11
|
Chris@101
|
12 #if defined(_MSC_VER)
|
Chris@16
|
13 #pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 /* Safe mode machinery, in the spirit of Cay Hortmann's "Safe STL"
|
Chris@16
|
17 * (http://www.horstmann.com/safestl.html).
|
Chris@16
|
18 * In this mode, containers of type Container are derived from
|
Chris@16
|
19 * safe_container<Container>, and their corresponding iterators
|
Chris@16
|
20 * are wrapped with safe_iterator. These classes provide
|
Chris@16
|
21 * an internal record of which iterators are at a given moment associated
|
Chris@16
|
22 * to a given container, and properly mark the iterators as invalid
|
Chris@16
|
23 * when the container gets destroyed.
|
Chris@16
|
24 * Iterators are chained in a single attached list, whose header is
|
Chris@16
|
25 * kept by the container. More elaborate data structures would yield better
|
Chris@16
|
26 * performance, but I decided to keep complexity to a minimum since
|
Chris@16
|
27 * speed is not an issue here.
|
Chris@16
|
28 * Safe mode iterators automatically check that only proper operations
|
Chris@16
|
29 * are performed on them: for instance, an invalid iterator cannot be
|
Chris@16
|
30 * dereferenced. Additionally, a set of utilty macros and functions are
|
Chris@16
|
31 * provided that serve to implement preconditions and cooperate with
|
Chris@16
|
32 * the framework within the container.
|
Chris@16
|
33 * Iterators can also be unchecked, i.e. they do not have info about
|
Chris@16
|
34 * which container they belong in. This situation arises when the iterator
|
Chris@16
|
35 * is restored from a serialization archive: only information on the node
|
Chris@16
|
36 * is available, and it is not possible to determine to which container
|
Chris@16
|
37 * the iterator is associated to. The only sensible policy is to assume
|
Chris@16
|
38 * unchecked iterators are valid, though this can certainly generate false
|
Chris@16
|
39 * positive safe mode checks.
|
Chris@16
|
40 * This is not a full-fledged safe mode framework, and is only intended
|
Chris@16
|
41 * for use within the limits of Boost.MultiIndex.
|
Chris@16
|
42 */
|
Chris@16
|
43
|
Chris@16
|
44 /* Assertion macros. These resolve to no-ops if
|
Chris@16
|
45 * !defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE).
|
Chris@16
|
46 */
|
Chris@16
|
47
|
Chris@16
|
48 #if !defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
|
Chris@16
|
49 #undef BOOST_MULTI_INDEX_SAFE_MODE_ASSERT
|
Chris@16
|
50 #define BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(expr,error_code) ((void)0)
|
Chris@16
|
51 #else
|
Chris@16
|
52 #if !defined(BOOST_MULTI_INDEX_SAFE_MODE_ASSERT)
|
Chris@16
|
53 #include <boost/assert.hpp>
|
Chris@16
|
54 #define BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(expr,error_code) BOOST_ASSERT(expr)
|
Chris@16
|
55 #endif
|
Chris@16
|
56 #endif
|
Chris@16
|
57
|
Chris@16
|
58 #define BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(it) \
|
Chris@16
|
59 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
60 safe_mode::check_valid_iterator(it), \
|
Chris@16
|
61 safe_mode::invalid_iterator);
|
Chris@16
|
62
|
Chris@16
|
63 #define BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(it) \
|
Chris@16
|
64 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
65 safe_mode::check_dereferenceable_iterator(it), \
|
Chris@16
|
66 safe_mode::not_dereferenceable_iterator);
|
Chris@16
|
67
|
Chris@16
|
68 #define BOOST_MULTI_INDEX_CHECK_INCREMENTABLE_ITERATOR(it) \
|
Chris@16
|
69 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
70 safe_mode::check_incrementable_iterator(it), \
|
Chris@16
|
71 safe_mode::not_incrementable_iterator);
|
Chris@16
|
72
|
Chris@16
|
73 #define BOOST_MULTI_INDEX_CHECK_DECREMENTABLE_ITERATOR(it) \
|
Chris@16
|
74 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
75 safe_mode::check_decrementable_iterator(it), \
|
Chris@16
|
76 safe_mode::not_decrementable_iterator);
|
Chris@16
|
77
|
Chris@16
|
78 #define BOOST_MULTI_INDEX_CHECK_IS_OWNER(it,cont) \
|
Chris@16
|
79 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
80 safe_mode::check_is_owner(it,cont), \
|
Chris@16
|
81 safe_mode::not_owner);
|
Chris@16
|
82
|
Chris@16
|
83 #define BOOST_MULTI_INDEX_CHECK_SAME_OWNER(it0,it1) \
|
Chris@16
|
84 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
85 safe_mode::check_same_owner(it0,it1), \
|
Chris@16
|
86 safe_mode::not_same_owner);
|
Chris@16
|
87
|
Chris@16
|
88 #define BOOST_MULTI_INDEX_CHECK_VALID_RANGE(it0,it1) \
|
Chris@16
|
89 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
90 safe_mode::check_valid_range(it0,it1), \
|
Chris@16
|
91 safe_mode::invalid_range);
|
Chris@16
|
92
|
Chris@16
|
93 #define BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(it,it0,it1) \
|
Chris@16
|
94 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
95 safe_mode::check_outside_range(it,it0,it1), \
|
Chris@16
|
96 safe_mode::inside_range);
|
Chris@16
|
97
|
Chris@16
|
98 #define BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(it,n) \
|
Chris@16
|
99 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
100 safe_mode::check_in_bounds(it,n), \
|
Chris@16
|
101 safe_mode::out_of_bounds);
|
Chris@16
|
102
|
Chris@16
|
103 #define BOOST_MULTI_INDEX_CHECK_DIFFERENT_CONTAINER(cont0,cont1) \
|
Chris@16
|
104 BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
|
Chris@16
|
105 safe_mode::check_different_container(cont0,cont1), \
|
Chris@16
|
106 safe_mode::same_container);
|
Chris@16
|
107
|
Chris@16
|
108 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
|
Chris@16
|
109 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
Chris@16
|
110 #include <algorithm>
|
Chris@16
|
111 #include <boost/detail/iterator.hpp>
|
Chris@16
|
112 #include <boost/multi_index/detail/access_specifier.hpp>
|
Chris@16
|
113 #include <boost/multi_index/detail/iter_adaptor.hpp>
|
Chris@16
|
114 #include <boost/multi_index/safe_mode_errors.hpp>
|
Chris@16
|
115 #include <boost/noncopyable.hpp>
|
Chris@16
|
116
|
Chris@16
|
117 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
|
Chris@16
|
118 #include <boost/serialization/split_member.hpp>
|
Chris@101
|
119 #include <boost/serialization/version.hpp>
|
Chris@16
|
120 #endif
|
Chris@16
|
121
|
Chris@16
|
122 #if defined(BOOST_HAS_THREADS)
|
Chris@16
|
123 #include <boost/detail/lightweight_mutex.hpp>
|
Chris@16
|
124 #endif
|
Chris@16
|
125
|
Chris@16
|
126 namespace boost{
|
Chris@16
|
127
|
Chris@16
|
128 namespace multi_index{
|
Chris@16
|
129
|
Chris@16
|
130 namespace safe_mode{
|
Chris@16
|
131
|
Chris@16
|
132 /* Checking routines. Assume the best for unchecked iterators
|
Chris@16
|
133 * (i.e. they pass the checking when there is not enough info
|
Chris@16
|
134 * to know.)
|
Chris@16
|
135 */
|
Chris@16
|
136
|
Chris@16
|
137 template<typename Iterator>
|
Chris@16
|
138 inline bool check_valid_iterator(const Iterator& it)
|
Chris@16
|
139 {
|
Chris@16
|
140 return it.valid()||it.unchecked();
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 template<typename Iterator>
|
Chris@16
|
144 inline bool check_dereferenceable_iterator(const Iterator& it)
|
Chris@16
|
145 {
|
Chris@16
|
146 return (it.valid()&&it!=it.owner()->end())||it.unchecked();
|
Chris@16
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 template<typename Iterator>
|
Chris@16
|
150 inline bool check_incrementable_iterator(const Iterator& it)
|
Chris@16
|
151 {
|
Chris@16
|
152 return (it.valid()&&it!=it.owner()->end())||it.unchecked();
|
Chris@16
|
153 }
|
Chris@16
|
154
|
Chris@16
|
155 template<typename Iterator>
|
Chris@16
|
156 inline bool check_decrementable_iterator(const Iterator& it)
|
Chris@16
|
157 {
|
Chris@16
|
158 return (it.valid()&&it!=it.owner()->begin())||it.unchecked();
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 template<typename Iterator>
|
Chris@16
|
162 inline bool check_is_owner(
|
Chris@16
|
163 const Iterator& it,const typename Iterator::container_type& cont)
|
Chris@16
|
164 {
|
Chris@16
|
165 return (it.valid()&&it.owner()==&cont)||it.unchecked();
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 template<typename Iterator>
|
Chris@16
|
169 inline bool check_same_owner(const Iterator& it0,const Iterator& it1)
|
Chris@16
|
170 {
|
Chris@16
|
171 return (it0.valid()&&it1.valid()&&it0.owner()==it1.owner())||
|
Chris@16
|
172 it0.unchecked()||it1.unchecked();
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 template<typename Iterator>
|
Chris@16
|
176 inline bool check_valid_range(const Iterator& it0,const Iterator& it1)
|
Chris@16
|
177 {
|
Chris@16
|
178 if(!check_same_owner(it0,it1))return false;
|
Chris@16
|
179
|
Chris@16
|
180 if(it0.valid()){
|
Chris@16
|
181 Iterator last=it0.owner()->end();
|
Chris@16
|
182 if(it1==last)return true;
|
Chris@16
|
183
|
Chris@16
|
184 for(Iterator first=it0;first!=last;++first){
|
Chris@16
|
185 if(first==it1)return true;
|
Chris@16
|
186 }
|
Chris@16
|
187 return false;
|
Chris@16
|
188 }
|
Chris@16
|
189 return true;
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 template<typename Iterator>
|
Chris@16
|
193 inline bool check_outside_range(
|
Chris@16
|
194 const Iterator& it,const Iterator& it0,const Iterator& it1)
|
Chris@16
|
195 {
|
Chris@16
|
196 if(!check_same_owner(it0,it1))return false;
|
Chris@16
|
197
|
Chris@16
|
198 if(it0.valid()){
|
Chris@16
|
199 Iterator last=it0.owner()->end();
|
Chris@16
|
200 bool found=false;
|
Chris@16
|
201
|
Chris@16
|
202 Iterator first=it0;
|
Chris@16
|
203 for(;first!=last;++first){
|
Chris@16
|
204 if(first==it1)break;
|
Chris@16
|
205
|
Chris@16
|
206 /* crucial that this check goes after previous break */
|
Chris@16
|
207
|
Chris@16
|
208 if(first==it)found=true;
|
Chris@16
|
209 }
|
Chris@16
|
210 if(first!=it1)return false;
|
Chris@16
|
211 return !found;
|
Chris@16
|
212 }
|
Chris@16
|
213 return true;
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 template<typename Iterator,typename Difference>
|
Chris@16
|
217 inline bool check_in_bounds(const Iterator& it,Difference n)
|
Chris@16
|
218 {
|
Chris@16
|
219 if(it.unchecked())return true;
|
Chris@16
|
220 if(!it.valid()) return false;
|
Chris@16
|
221 if(n>0) return it.owner()->end()-it>=n;
|
Chris@16
|
222 else return it.owner()->begin()-it<=n;
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 template<typename Container>
|
Chris@16
|
226 inline bool check_different_container(
|
Chris@16
|
227 const Container& cont0,const Container& cont1)
|
Chris@16
|
228 {
|
Chris@16
|
229 return &cont0!=&cont1;
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 /* Invalidates all iterators equivalent to that given. Safe containers
|
Chris@16
|
233 * must call this when deleting elements: the safe mode framework cannot
|
Chris@16
|
234 * perform this operation automatically without outside help.
|
Chris@16
|
235 */
|
Chris@16
|
236
|
Chris@16
|
237 template<typename Iterator>
|
Chris@16
|
238 inline void detach_equivalent_iterators(Iterator& it)
|
Chris@16
|
239 {
|
Chris@16
|
240 if(it.valid()){
|
Chris@16
|
241 {
|
Chris@16
|
242 #if defined(BOOST_HAS_THREADS)
|
Chris@16
|
243 boost::detail::lightweight_mutex::scoped_lock lock(it.cont->mutex);
|
Chris@16
|
244 #endif
|
Chris@16
|
245
|
Chris@16
|
246 Iterator *prev_,*next_;
|
Chris@16
|
247 for(
|
Chris@16
|
248 prev_=static_cast<Iterator*>(&it.cont->header);
|
Chris@16
|
249 (next_=static_cast<Iterator*>(prev_->next))!=0;){
|
Chris@16
|
250 if(next_!=&it&&*next_==it){
|
Chris@16
|
251 prev_->next=next_->next;
|
Chris@16
|
252 next_->cont=0;
|
Chris@16
|
253 }
|
Chris@16
|
254 else prev_=next_;
|
Chris@16
|
255 }
|
Chris@16
|
256 }
|
Chris@16
|
257 it.detach();
|
Chris@16
|
258 }
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 template<typename Container> class safe_container; /* fwd decl. */
|
Chris@16
|
262
|
Chris@16
|
263 } /* namespace multi_index::safe_mode */
|
Chris@16
|
264
|
Chris@16
|
265 namespace detail{
|
Chris@16
|
266
|
Chris@16
|
267 class safe_container_base; /* fwd decl. */
|
Chris@16
|
268
|
Chris@16
|
269 class safe_iterator_base
|
Chris@16
|
270 {
|
Chris@16
|
271 public:
|
Chris@16
|
272 bool valid()const{return cont!=0;}
|
Chris@16
|
273 bool unchecked()const{return unchecked_;}
|
Chris@16
|
274
|
Chris@16
|
275 inline void detach();
|
Chris@16
|
276
|
Chris@16
|
277 void uncheck()
|
Chris@16
|
278 {
|
Chris@16
|
279 detach();
|
Chris@16
|
280 unchecked_=true;
|
Chris@16
|
281 }
|
Chris@16
|
282
|
Chris@16
|
283 protected:
|
Chris@16
|
284 safe_iterator_base():cont(0),next(0),unchecked_(false){}
|
Chris@16
|
285
|
Chris@16
|
286 explicit safe_iterator_base(safe_container_base* cont_):
|
Chris@16
|
287 unchecked_(false)
|
Chris@16
|
288 {
|
Chris@16
|
289 attach(cont_);
|
Chris@16
|
290 }
|
Chris@16
|
291
|
Chris@16
|
292 safe_iterator_base(const safe_iterator_base& it):
|
Chris@16
|
293 unchecked_(it.unchecked_)
|
Chris@16
|
294 {
|
Chris@16
|
295 attach(it.cont);
|
Chris@16
|
296 }
|
Chris@16
|
297
|
Chris@16
|
298 safe_iterator_base& operator=(const safe_iterator_base& it)
|
Chris@16
|
299 {
|
Chris@16
|
300 unchecked_=it.unchecked_;
|
Chris@16
|
301 safe_container_base* new_cont=it.cont;
|
Chris@16
|
302 if(cont!=new_cont){
|
Chris@16
|
303 detach();
|
Chris@16
|
304 attach(new_cont);
|
Chris@16
|
305 }
|
Chris@16
|
306 return *this;
|
Chris@16
|
307 }
|
Chris@16
|
308
|
Chris@16
|
309 ~safe_iterator_base()
|
Chris@16
|
310 {
|
Chris@16
|
311 detach();
|
Chris@16
|
312 }
|
Chris@16
|
313
|
Chris@16
|
314 const safe_container_base* owner()const{return cont;}
|
Chris@16
|
315
|
Chris@16
|
316 BOOST_MULTI_INDEX_PRIVATE_IF_MEMBER_TEMPLATE_FRIENDS:
|
Chris@16
|
317 friend class safe_container_base;
|
Chris@16
|
318
|
Chris@16
|
319 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
Chris@16
|
320 template<typename> friend class safe_mode::safe_container;
|
Chris@16
|
321 template<typename Iterator> friend
|
Chris@16
|
322 void safe_mode::detach_equivalent_iterators(Iterator&);
|
Chris@16
|
323 #endif
|
Chris@16
|
324
|
Chris@16
|
325 inline void attach(safe_container_base* cont_);
|
Chris@16
|
326
|
Chris@16
|
327 safe_container_base* cont;
|
Chris@16
|
328 safe_iterator_base* next;
|
Chris@16
|
329 bool unchecked_;
|
Chris@16
|
330 };
|
Chris@16
|
331
|
Chris@16
|
332 class safe_container_base:private noncopyable
|
Chris@16
|
333 {
|
Chris@16
|
334 public:
|
Chris@16
|
335 safe_container_base(){}
|
Chris@16
|
336
|
Chris@16
|
337 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
|
Chris@16
|
338 friend class safe_iterator_base;
|
Chris@16
|
339
|
Chris@16
|
340 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
Chris@16
|
341 template<typename Iterator> friend
|
Chris@16
|
342 void safe_mode::detach_equivalent_iterators(Iterator&);
|
Chris@16
|
343 #endif
|
Chris@16
|
344
|
Chris@16
|
345 ~safe_container_base()
|
Chris@16
|
346 {
|
Chris@16
|
347 /* Detaches all remaining iterators, which by now will
|
Chris@16
|
348 * be those pointing to the end of the container.
|
Chris@16
|
349 */
|
Chris@16
|
350
|
Chris@16
|
351 for(safe_iterator_base* it=header.next;it;it=it->next)it->cont=0;
|
Chris@16
|
352 header.next=0;
|
Chris@16
|
353 }
|
Chris@16
|
354
|
Chris@16
|
355 void swap(safe_container_base& x)
|
Chris@16
|
356 {
|
Chris@16
|
357 for(safe_iterator_base* it0=header.next;it0;it0=it0->next)it0->cont=&x;
|
Chris@16
|
358 for(safe_iterator_base* it1=x.header.next;it1;it1=it1->next)it1->cont=this;
|
Chris@16
|
359 std::swap(header.cont,x.header.cont);
|
Chris@16
|
360 std::swap(header.next,x.header.next);
|
Chris@16
|
361 }
|
Chris@16
|
362
|
Chris@16
|
363 safe_iterator_base header;
|
Chris@16
|
364
|
Chris@16
|
365 #if defined(BOOST_HAS_THREADS)
|
Chris@16
|
366 boost::detail::lightweight_mutex mutex;
|
Chris@16
|
367 #endif
|
Chris@16
|
368 };
|
Chris@16
|
369
|
Chris@16
|
370 void safe_iterator_base::attach(safe_container_base* cont_)
|
Chris@16
|
371 {
|
Chris@16
|
372 cont=cont_;
|
Chris@16
|
373 if(cont){
|
Chris@16
|
374 #if defined(BOOST_HAS_THREADS)
|
Chris@16
|
375 boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
|
Chris@16
|
376 #endif
|
Chris@16
|
377
|
Chris@16
|
378 next=cont->header.next;
|
Chris@16
|
379 cont->header.next=this;
|
Chris@16
|
380 }
|
Chris@16
|
381 }
|
Chris@16
|
382
|
Chris@16
|
383 void safe_iterator_base::detach()
|
Chris@16
|
384 {
|
Chris@16
|
385 if(cont){
|
Chris@16
|
386 #if defined(BOOST_HAS_THREADS)
|
Chris@16
|
387 boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
|
Chris@16
|
388 #endif
|
Chris@16
|
389
|
Chris@16
|
390 safe_iterator_base *prev_,*next_;
|
Chris@16
|
391 for(prev_=&cont->header;(next_=prev_->next)!=this;prev_=next_){}
|
Chris@16
|
392 prev_->next=next;
|
Chris@16
|
393 cont=0;
|
Chris@16
|
394 }
|
Chris@16
|
395 }
|
Chris@16
|
396
|
Chris@16
|
397 } /* namespace multi_index::detail */
|
Chris@16
|
398
|
Chris@16
|
399 namespace safe_mode{
|
Chris@16
|
400
|
Chris@16
|
401 /* In order to enable safe mode on a container:
|
Chris@16
|
402 * - The container must derive from safe_container<container_type>,
|
Chris@16
|
403 * - iterators must be generated via safe_iterator, which adapts a
|
Chris@16
|
404 * preexistent unsafe iterator class.
|
Chris@16
|
405 */
|
Chris@16
|
406
|
Chris@16
|
407 template<typename Container>
|
Chris@16
|
408 class safe_container;
|
Chris@16
|
409
|
Chris@16
|
410 template<typename Iterator,typename Container>
|
Chris@16
|
411 class safe_iterator:
|
Chris@16
|
412 public detail::iter_adaptor<safe_iterator<Iterator,Container>,Iterator>,
|
Chris@16
|
413 public detail::safe_iterator_base
|
Chris@16
|
414 {
|
Chris@16
|
415 typedef detail::iter_adaptor<safe_iterator,Iterator> super;
|
Chris@16
|
416 typedef detail::safe_iterator_base safe_super;
|
Chris@16
|
417
|
Chris@16
|
418 public:
|
Chris@16
|
419 typedef Container container_type;
|
Chris@16
|
420 typedef typename Iterator::reference reference;
|
Chris@16
|
421 typedef typename Iterator::difference_type difference_type;
|
Chris@16
|
422
|
Chris@16
|
423 safe_iterator(){}
|
Chris@16
|
424 explicit safe_iterator(safe_container<container_type>* cont_):
|
Chris@16
|
425 safe_super(cont_){}
|
Chris@16
|
426 template<typename T0>
|
Chris@16
|
427 safe_iterator(const T0& t0,safe_container<container_type>* cont_):
|
Chris@16
|
428 super(Iterator(t0)),safe_super(cont_){}
|
Chris@16
|
429 template<typename T0,typename T1>
|
Chris@16
|
430 safe_iterator(
|
Chris@16
|
431 const T0& t0,const T1& t1,safe_container<container_type>* cont_):
|
Chris@16
|
432 super(Iterator(t0,t1)),safe_super(cont_){}
|
Chris@16
|
433
|
Chris@16
|
434 safe_iterator& operator=(const safe_iterator& x)
|
Chris@16
|
435 {
|
Chris@16
|
436 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
|
Chris@16
|
437 this->base_reference()=x.base_reference();
|
Chris@16
|
438 safe_super::operator=(x);
|
Chris@16
|
439 return *this;
|
Chris@16
|
440 }
|
Chris@16
|
441
|
Chris@16
|
442 const container_type* owner()const
|
Chris@16
|
443 {
|
Chris@16
|
444 return
|
Chris@16
|
445 static_cast<const container_type*>(
|
Chris@16
|
446 static_cast<const safe_container<container_type>*>(
|
Chris@16
|
447 this->safe_super::owner()));
|
Chris@16
|
448 }
|
Chris@16
|
449
|
Chris@16
|
450 /* get_node is not to be used by the user */
|
Chris@16
|
451
|
Chris@16
|
452 typedef typename Iterator::node_type node_type;
|
Chris@16
|
453
|
Chris@16
|
454 node_type* get_node()const{return this->base_reference().get_node();}
|
Chris@16
|
455
|
Chris@16
|
456 private:
|
Chris@16
|
457 friend class boost::multi_index::detail::iter_adaptor_access;
|
Chris@16
|
458
|
Chris@16
|
459 reference dereference()const
|
Chris@16
|
460 {
|
Chris@16
|
461 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
462 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(*this);
|
Chris@16
|
463 return *(this->base_reference());
|
Chris@16
|
464 }
|
Chris@16
|
465
|
Chris@16
|
466 bool equal(const safe_iterator& x)const
|
Chris@16
|
467 {
|
Chris@16
|
468 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
469 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
|
Chris@16
|
470 BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
|
Chris@16
|
471 return this->base_reference()==x.base_reference();
|
Chris@16
|
472 }
|
Chris@16
|
473
|
Chris@16
|
474 void increment()
|
Chris@16
|
475 {
|
Chris@16
|
476 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
477 BOOST_MULTI_INDEX_CHECK_INCREMENTABLE_ITERATOR(*this);
|
Chris@16
|
478 ++(this->base_reference());
|
Chris@16
|
479 }
|
Chris@16
|
480
|
Chris@16
|
481 void decrement()
|
Chris@16
|
482 {
|
Chris@16
|
483 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
484 BOOST_MULTI_INDEX_CHECK_DECREMENTABLE_ITERATOR(*this);
|
Chris@16
|
485 --(this->base_reference());
|
Chris@16
|
486 }
|
Chris@16
|
487
|
Chris@16
|
488 void advance(difference_type n)
|
Chris@16
|
489 {
|
Chris@16
|
490 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
491 BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(*this,n);
|
Chris@16
|
492 this->base_reference()+=n;
|
Chris@16
|
493 }
|
Chris@16
|
494
|
Chris@16
|
495 difference_type distance_to(const safe_iterator& x)const
|
Chris@16
|
496 {
|
Chris@16
|
497 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
498 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
|
Chris@16
|
499 BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
|
Chris@16
|
500 return x.base_reference()-this->base_reference();
|
Chris@16
|
501 }
|
Chris@16
|
502
|
Chris@16
|
503 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
|
Chris@16
|
504 /* Serialization. Note that Iterator::save and Iterator:load
|
Chris@16
|
505 * are assumed to be defined and public: at first sight it seems
|
Chris@16
|
506 * like we could have resorted to the public serialization interface
|
Chris@16
|
507 * for doing the forwarding to the adapted iterator class:
|
Chris@16
|
508 * ar<<base_reference();
|
Chris@16
|
509 * ar>>base_reference();
|
Chris@16
|
510 * but this would cause incompatibilities if a saving
|
Chris@16
|
511 * program is in safe mode and the loading program is not, or
|
Chris@16
|
512 * viceversa --in safe mode, the archived iterator data is one layer
|
Chris@16
|
513 * deeper, this is especially relevant with XML archives.
|
Chris@16
|
514 * It'd be nice if Boost.Serialization provided some forwarding
|
Chris@16
|
515 * facility for use by adaptor classes.
|
Chris@16
|
516 */
|
Chris@16
|
517
|
Chris@16
|
518 friend class boost::serialization::access;
|
Chris@16
|
519
|
Chris@16
|
520 BOOST_SERIALIZATION_SPLIT_MEMBER()
|
Chris@16
|
521
|
Chris@16
|
522 template<class Archive>
|
Chris@16
|
523 void save(Archive& ar,const unsigned int version)const
|
Chris@16
|
524 {
|
Chris@16
|
525 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
|
Chris@16
|
526 this->base_reference().save(ar,version);
|
Chris@16
|
527 }
|
Chris@16
|
528
|
Chris@16
|
529 template<class Archive>
|
Chris@16
|
530 void load(Archive& ar,const unsigned int version)
|
Chris@16
|
531 {
|
Chris@16
|
532 this->base_reference().load(ar,version);
|
Chris@16
|
533 safe_super::uncheck();
|
Chris@16
|
534 }
|
Chris@16
|
535 #endif
|
Chris@16
|
536 };
|
Chris@16
|
537
|
Chris@16
|
538 template<typename Container>
|
Chris@16
|
539 class safe_container:public detail::safe_container_base
|
Chris@16
|
540 {
|
Chris@16
|
541 typedef detail::safe_container_base super;
|
Chris@16
|
542
|
Chris@16
|
543 public:
|
Chris@16
|
544 void detach_dereferenceable_iterators()
|
Chris@16
|
545 {
|
Chris@16
|
546 typedef typename Container::iterator iterator;
|
Chris@16
|
547
|
Chris@16
|
548 iterator end_=static_cast<Container*>(this)->end();
|
Chris@16
|
549 iterator *prev_,*next_;
|
Chris@16
|
550 for(
|
Chris@16
|
551 prev_=static_cast<iterator*>(&this->header);
|
Chris@16
|
552 (next_=static_cast<iterator*>(prev_->next))!=0;){
|
Chris@16
|
553 if(*next_!=end_){
|
Chris@16
|
554 prev_->next=next_->next;
|
Chris@16
|
555 next_->cont=0;
|
Chris@16
|
556 }
|
Chris@16
|
557 else prev_=next_;
|
Chris@16
|
558 }
|
Chris@16
|
559 }
|
Chris@16
|
560
|
Chris@16
|
561 void swap(safe_container<Container>& x)
|
Chris@16
|
562 {
|
Chris@16
|
563 super::swap(x);
|
Chris@16
|
564 }
|
Chris@16
|
565 };
|
Chris@16
|
566
|
Chris@16
|
567 } /* namespace multi_index::safe_mode */
|
Chris@16
|
568
|
Chris@16
|
569 } /* namespace multi_index */
|
Chris@16
|
570
|
Chris@101
|
571 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
|
Chris@101
|
572 namespace serialization{
|
Chris@101
|
573 template<typename Iterator,typename Container>
|
Chris@101
|
574 struct version<
|
Chris@101
|
575 boost::multi_index::safe_mode::safe_iterator<Iterator,Container>
|
Chris@101
|
576 >
|
Chris@101
|
577 {
|
Chris@101
|
578 BOOST_STATIC_CONSTANT(
|
Chris@101
|
579 int,value=boost::serialization::version<Iterator>::value);
|
Chris@101
|
580 };
|
Chris@101
|
581 } /* namespace serialization */
|
Chris@101
|
582 #endif
|
Chris@101
|
583
|
Chris@16
|
584 } /* namespace boost */
|
Chris@16
|
585
|
Chris@16
|
586 #endif /* BOOST_MULTI_INDEX_ENABLE_SAFE_MODE */
|
Chris@16
|
587
|
Chris@16
|
588 #endif
|