/Src/Dependencies/Boost/boost/multi_index/detail/index_loader.hpp

http://hadesmem.googlecode.com/ · C++ Header · 138 lines · 91 code · 27 blank · 20 comment · 8 complexity · bc519744d279660b52458baadfdddd78 MD5 · raw file

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