safe_mode.hpp 21 KB

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