index_loader.hpp 3.3 KB

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