index_loader.hpp 3.4 KB

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