safe_mode.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /* Copyright 2003-2013 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 of type Container are derived from
  16. * safe_container<Container>, and their corresponding iterators
  17. * are wrapped with safe_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_SAME_OWNER(it0,it1) \
  73. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  74. safe_mode::check_same_owner(it0,it1), \
  75. safe_mode::not_same_owner);
  76. #define BOOST_MULTI_INDEX_CHECK_VALID_RANGE(it0,it1) \
  77. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  78. safe_mode::check_valid_range(it0,it1), \
  79. safe_mode::invalid_range);
  80. #define BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(it,it0,it1) \
  81. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  82. safe_mode::check_outside_range(it,it0,it1), \
  83. safe_mode::inside_range);
  84. #define BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(it,n) \
  85. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  86. safe_mode::check_in_bounds(it,n), \
  87. safe_mode::out_of_bounds);
  88. #define BOOST_MULTI_INDEX_CHECK_DIFFERENT_CONTAINER(cont0,cont1) \
  89. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  90. safe_mode::check_different_container(cont0,cont1), \
  91. safe_mode::same_container);
  92. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  93. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  94. #include <algorithm>
  95. #include <boost/detail/iterator.hpp>
  96. #include <boost/multi_index/detail/access_specifier.hpp>
  97. #include <boost/multi_index/detail/iter_adaptor.hpp>
  98. #include <boost/multi_index/safe_mode_errors.hpp>
  99. #include <boost/noncopyable.hpp>
  100. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  101. #include <boost/serialization/split_member.hpp>
  102. #include <boost/serialization/version.hpp>
  103. #endif
  104. #if defined(BOOST_HAS_THREADS)
  105. #include <boost/detail/lightweight_mutex.hpp>
  106. #endif
  107. namespace boost{
  108. namespace multi_index{
  109. namespace safe_mode{
  110. /* Checking routines. Assume the best for unchecked iterators
  111. * (i.e. they pass the checking when there is not enough info
  112. * to know.)
  113. */
  114. template<typename Iterator>
  115. inline bool check_valid_iterator(const Iterator& it)
  116. {
  117. return it.valid()||it.unchecked();
  118. }
  119. template<typename Iterator>
  120. inline bool check_dereferenceable_iterator(const Iterator& it)
  121. {
  122. return (it.valid()&&it!=it.owner()->end())||it.unchecked();
  123. }
  124. template<typename Iterator>
  125. inline bool check_incrementable_iterator(const Iterator& it)
  126. {
  127. return (it.valid()&&it!=it.owner()->end())||it.unchecked();
  128. }
  129. template<typename Iterator>
  130. inline bool check_decrementable_iterator(const Iterator& it)
  131. {
  132. return (it.valid()&&it!=it.owner()->begin())||it.unchecked();
  133. }
  134. template<typename Iterator>
  135. inline bool check_is_owner(
  136. const Iterator& it,const typename Iterator::container_type& cont)
  137. {
  138. return (it.valid()&&it.owner()==&cont)||it.unchecked();
  139. }
  140. template<typename Iterator>
  141. inline bool check_same_owner(const Iterator& it0,const Iterator& it1)
  142. {
  143. return (it0.valid()&&it1.valid()&&it0.owner()==it1.owner())||
  144. it0.unchecked()||it1.unchecked();
  145. }
  146. template<typename Iterator>
  147. inline bool check_valid_range(const Iterator& it0,const Iterator& it1)
  148. {
  149. if(!check_same_owner(it0,it1))return false;
  150. if(it0.valid()){
  151. Iterator last=it0.owner()->end();
  152. if(it1==last)return true;
  153. for(Iterator first=it0;first!=last;++first){
  154. if(first==it1)return true;
  155. }
  156. return false;
  157. }
  158. return true;
  159. }
  160. template<typename Iterator>
  161. inline bool check_outside_range(
  162. const Iterator& it,const Iterator& it0,const Iterator& it1)
  163. {
  164. if(!check_same_owner(it0,it1))return false;
  165. if(it0.valid()){
  166. Iterator last=it0.owner()->end();
  167. bool found=false;
  168. Iterator first=it0;
  169. for(;first!=last;++first){
  170. if(first==it1)break;
  171. /* crucial that this check goes after previous break */
  172. if(first==it)found=true;
  173. }
  174. if(first!=it1)return false;
  175. return !found;
  176. }
  177. return true;
  178. }
  179. template<typename Iterator,typename Difference>
  180. inline bool check_in_bounds(const Iterator& it,Difference n)
  181. {
  182. if(it.unchecked())return true;
  183. if(!it.valid()) return false;
  184. if(n>0) return it.owner()->end()-it>=n;
  185. else return it.owner()->begin()-it<=n;
  186. }
  187. template<typename Container>
  188. inline bool check_different_container(
  189. const Container& cont0,const Container& cont1)
  190. {
  191. return &cont0!=&cont1;
  192. }
  193. /* Invalidates all iterators equivalent to that given. Safe containers
  194. * must call this when deleting elements: the safe mode framework cannot
  195. * perform this operation automatically without outside help.
  196. */
  197. template<typename Iterator>
  198. inline void detach_equivalent_iterators(Iterator& it)
  199. {
  200. if(it.valid()){
  201. {
  202. #if defined(BOOST_HAS_THREADS)
  203. boost::detail::lightweight_mutex::scoped_lock lock(it.cont->mutex);
  204. #endif
  205. Iterator *prev_,*next_;
  206. for(
  207. prev_=static_cast<Iterator*>(&it.cont->header);
  208. (next_=static_cast<Iterator*>(prev_->next))!=0;){
  209. if(next_!=&it&&*next_==it){
  210. prev_->next=next_->next;
  211. next_->cont=0;
  212. }
  213. else prev_=next_;
  214. }
  215. }
  216. it.detach();
  217. }
  218. }
  219. template<typename Container> class safe_container; /* fwd decl. */
  220. } /* namespace multi_index::safe_mode */
  221. namespace detail{
  222. class safe_container_base; /* fwd decl. */
  223. class safe_iterator_base
  224. {
  225. public:
  226. bool valid()const{return cont!=0;}
  227. bool unchecked()const{return unchecked_;}
  228. inline void detach();
  229. void uncheck()
  230. {
  231. detach();
  232. unchecked_=true;
  233. }
  234. protected:
  235. safe_iterator_base():cont(0),next(0),unchecked_(false){}
  236. explicit safe_iterator_base(safe_container_base* cont_):
  237. unchecked_(false)
  238. {
  239. attach(cont_);
  240. }
  241. safe_iterator_base(const safe_iterator_base& it):
  242. unchecked_(it.unchecked_)
  243. {
  244. attach(it.cont);
  245. }
  246. safe_iterator_base& operator=(const safe_iterator_base& it)
  247. {
  248. unchecked_=it.unchecked_;
  249. safe_container_base* new_cont=it.cont;
  250. if(cont!=new_cont){
  251. detach();
  252. attach(new_cont);
  253. }
  254. return *this;
  255. }
  256. ~safe_iterator_base()
  257. {
  258. detach();
  259. }
  260. const safe_container_base* owner()const{return cont;}
  261. BOOST_MULTI_INDEX_PRIVATE_IF_MEMBER_TEMPLATE_FRIENDS:
  262. friend class safe_container_base;
  263. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  264. template<typename> friend class safe_mode::safe_container;
  265. template<typename Iterator> friend
  266. void safe_mode::detach_equivalent_iterators(Iterator&);
  267. #endif
  268. inline void attach(safe_container_base* cont_);
  269. safe_container_base* cont;
  270. safe_iterator_base* next;
  271. bool unchecked_;
  272. };
  273. class safe_container_base:private noncopyable
  274. {
  275. public:
  276. safe_container_base(){}
  277. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
  278. friend class safe_iterator_base;
  279. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  280. template<typename Iterator> friend
  281. void safe_mode::detach_equivalent_iterators(Iterator&);
  282. #endif
  283. ~safe_container_base()
  284. {
  285. /* Detaches all remaining iterators, which by now will
  286. * be those pointing to the end of the container.
  287. */
  288. for(safe_iterator_base* it=header.next;it;it=it->next)it->cont=0;
  289. header.next=0;
  290. }
  291. void swap(safe_container_base& x)
  292. {
  293. for(safe_iterator_base* it0=header.next;it0;it0=it0->next)it0->cont=&x;
  294. for(safe_iterator_base* it1=x.header.next;it1;it1=it1->next)it1->cont=this;
  295. std::swap(header.cont,x.header.cont);
  296. std::swap(header.next,x.header.next);
  297. }
  298. safe_iterator_base header;
  299. #if defined(BOOST_HAS_THREADS)
  300. boost::detail::lightweight_mutex mutex;
  301. #endif
  302. };
  303. void safe_iterator_base::attach(safe_container_base* cont_)
  304. {
  305. cont=cont_;
  306. if(cont){
  307. #if defined(BOOST_HAS_THREADS)
  308. boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
  309. #endif
  310. next=cont->header.next;
  311. cont->header.next=this;
  312. }
  313. }
  314. void safe_iterator_base::detach()
  315. {
  316. if(cont){
  317. #if defined(BOOST_HAS_THREADS)
  318. boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
  319. #endif
  320. safe_iterator_base *prev_,*next_;
  321. for(prev_=&cont->header;(next_=prev_->next)!=this;prev_=next_){}
  322. prev_->next=next;
  323. cont=0;
  324. }
  325. }
  326. } /* namespace multi_index::detail */
  327. namespace safe_mode{
  328. /* In order to enable safe mode on a container:
  329. * - The container must derive from safe_container<container_type>,
  330. * - iterators must be generated via safe_iterator, which adapts a
  331. * preexistent unsafe iterator class.
  332. */
  333. template<typename Container>
  334. class safe_container;
  335. template<typename Iterator,typename Container>
  336. class safe_iterator:
  337. public detail::iter_adaptor<safe_iterator<Iterator,Container>,Iterator>,
  338. public detail::safe_iterator_base
  339. {
  340. typedef detail::iter_adaptor<safe_iterator,Iterator> super;
  341. typedef detail::safe_iterator_base safe_super;
  342. public:
  343. typedef Container container_type;
  344. typedef typename Iterator::reference reference;
  345. typedef typename Iterator::difference_type difference_type;
  346. safe_iterator(){}
  347. explicit safe_iterator(safe_container<container_type>* cont_):
  348. safe_super(cont_){}
  349. template<typename T0>
  350. safe_iterator(const T0& t0,safe_container<container_type>* cont_):
  351. super(Iterator(t0)),safe_super(cont_){}
  352. template<typename T0,typename T1>
  353. safe_iterator(
  354. const T0& t0,const T1& t1,safe_container<container_type>* cont_):
  355. super(Iterator(t0,t1)),safe_super(cont_){}
  356. safe_iterator& operator=(const safe_iterator& x)
  357. {
  358. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  359. this->base_reference()=x.base_reference();
  360. safe_super::operator=(x);
  361. return *this;
  362. }
  363. const container_type* owner()const
  364. {
  365. return
  366. static_cast<const container_type*>(
  367. static_cast<const safe_container<container_type>*>(
  368. this->safe_super::owner()));
  369. }
  370. /* get_node is not to be used by the user */
  371. typedef typename Iterator::node_type node_type;
  372. node_type* get_node()const{return this->base_reference().get_node();}
  373. private:
  374. friend class boost::multi_index::detail::iter_adaptor_access;
  375. reference dereference()const
  376. {
  377. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  378. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(*this);
  379. return *(this->base_reference());
  380. }
  381. bool equal(const safe_iterator& x)const
  382. {
  383. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  384. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  385. BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
  386. return this->base_reference()==x.base_reference();
  387. }
  388. void increment()
  389. {
  390. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  391. BOOST_MULTI_INDEX_CHECK_INCREMENTABLE_ITERATOR(*this);
  392. ++(this->base_reference());
  393. }
  394. void decrement()
  395. {
  396. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  397. BOOST_MULTI_INDEX_CHECK_DECREMENTABLE_ITERATOR(*this);
  398. --(this->base_reference());
  399. }
  400. void advance(difference_type n)
  401. {
  402. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  403. BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(*this,n);
  404. this->base_reference()+=n;
  405. }
  406. difference_type distance_to(const safe_iterator& x)const
  407. {
  408. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  409. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  410. BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
  411. return x.base_reference()-this->base_reference();
  412. }
  413. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  414. /* Serialization. Note that Iterator::save and Iterator:load
  415. * are assumed to be defined and public: at first sight it seems
  416. * like we could have resorted to the public serialization interface
  417. * for doing the forwarding to the adapted iterator class:
  418. * ar<<base_reference();
  419. * ar>>base_reference();
  420. * but this would cause incompatibilities if a saving
  421. * program is in safe mode and the loading program is not, or
  422. * viceversa --in safe mode, the archived iterator data is one layer
  423. * deeper, this is especially relevant with XML archives.
  424. * It'd be nice if Boost.Serialization provided some forwarding
  425. * facility for use by adaptor classes.
  426. */
  427. friend class boost::serialization::access;
  428. BOOST_SERIALIZATION_SPLIT_MEMBER()
  429. template<class Archive>
  430. void save(Archive& ar,const unsigned int version)const
  431. {
  432. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  433. this->base_reference().save(ar,version);
  434. }
  435. template<class Archive>
  436. void load(Archive& ar,const unsigned int version)
  437. {
  438. this->base_reference().load(ar,version);
  439. safe_super::uncheck();
  440. }
  441. #endif
  442. };
  443. template<typename Container>
  444. class safe_container:public detail::safe_container_base
  445. {
  446. typedef detail::safe_container_base super;
  447. public:
  448. void detach_dereferenceable_iterators()
  449. {
  450. typedef typename Container::iterator iterator;
  451. iterator end_=static_cast<Container*>(this)->end();
  452. iterator *prev_,*next_;
  453. for(
  454. prev_=static_cast<iterator*>(&this->header);
  455. (next_=static_cast<iterator*>(prev_->next))!=0;){
  456. if(*next_!=end_){
  457. prev_->next=next_->next;
  458. next_->cont=0;
  459. }
  460. else prev_=next_;
  461. }
  462. }
  463. void swap(safe_container<Container>& x)
  464. {
  465. super::swap(x);
  466. }
  467. };
  468. } /* namespace multi_index::safe_mode */
  469. } /* namespace multi_index */
  470. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  471. namespace serialization{
  472. template<typename Iterator,typename Container>
  473. struct version<
  474. boost::multi_index::safe_mode::safe_iterator<Iterator,Container>
  475. >
  476. {
  477. BOOST_STATIC_CONSTANT(
  478. int,value=boost::serialization::version<Iterator>::value);
  479. };
  480. } /* namespace serialization */
  481. #endif
  482. } /* namespace boost */
  483. #endif /* BOOST_MULTI_INDEX_ENABLE_SAFE_MODE */
  484. #endif