index_node_base.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_NODE_BASE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_INDEX_NODE_BASE_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 <boost/type_traits/aligned_storage.hpp>
  15. #include <boost/type_traits/alignment_of.hpp>
  16. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  17. #include <boost/archive/archive_exception.hpp>
  18. #include <boost/serialization/access.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #endif
  21. namespace boost{
  22. namespace multi_index{
  23. namespace detail{
  24. /* index_node_base tops the node hierarchy of multi_index_container. It holds
  25. * the value of the element contained.
  26. */
  27. template<typename Value>
  28. struct pod_value_holder
  29. {
  30. typename aligned_storage<
  31. sizeof(Value),
  32. alignment_of<Value>::value
  33. >::type space;
  34. };
  35. template<typename Value,typename Allocator>
  36. struct index_node_base:private pod_value_holder<Value>
  37. {
  38. typedef index_node_base base_type; /* used for serialization purposes */
  39. typedef Value value_type;
  40. typedef Allocator allocator_type;
  41. value_type& value()
  42. {
  43. return *static_cast<value_type*>(
  44. static_cast<void*>(&this->space));
  45. }
  46. const value_type& value()const
  47. {
  48. return *static_cast<const value_type*>(
  49. static_cast<const void*>(&this->space));
  50. }
  51. static index_node_base* from_value(const value_type* p)
  52. {
  53. return static_cast<index_node_base *>(
  54. reinterpret_cast<pod_value_holder<Value>*>( /* std 9.2.17 */
  55. const_cast<value_type*>(p)));
  56. }
  57. private:
  58. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  59. friend class boost::serialization::access;
  60. /* nodes do not emit any kind of serialization info. They are
  61. * fed to Boost.Serialization so that pointers to nodes are
  62. * tracked correctly.
  63. */
  64. template<class Archive>
  65. void serialize(Archive&,const unsigned int)
  66. {
  67. }
  68. #endif
  69. };
  70. template<typename Node,typename Value>
  71. Node* node_from_value(
  72. const Value* p
  73. BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Node))
  74. {
  75. typedef typename Node::allocator_type allocator_type;
  76. return static_cast<Node*>(
  77. index_node_base<Value,allocator_type>::from_value(p));
  78. }
  79. } /* namespace multi_index::detail */
  80. } /* namespace multi_index */
  81. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  82. /* Index nodes never get constructed directly by Boost.Serialization,
  83. * as archives are always fed pointers to previously existent
  84. * nodes. So, if this is called it means we are dealing with a
  85. * somehow invalid archive.
  86. */
  87. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  88. namespace serialization{
  89. #else
  90. namespace multi_index{
  91. namespace detail{
  92. #endif
  93. template<class Archive,typename Value,typename Allocator>
  94. inline void load_construct_data(
  95. Archive&,boost::multi_index::detail::index_node_base<Value,Allocator>*,
  96. const unsigned int)
  97. {
  98. throw_exception(
  99. archive::archive_exception(archive::archive_exception::other_exception));
  100. }
  101. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  102. } /* namespace serialization */
  103. #else
  104. } /* namespace multi_index::detail */
  105. } /* namespace multi_index */
  106. #endif
  107. #endif
  108. } /* namespace boost */
  109. #endif