index_node_base.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright 2003-2016 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)
  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. #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
  42. value_type& value()
  43. {
  44. return *reinterpret_cast<value_type*>(&this->space);
  45. }
  46. const value_type& value()const
  47. {
  48. return *reinterpret_cast<const value_type*>(&this->space);
  49. }
  50. #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
  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(const Value* p)
  72. {
  73. typedef typename Node::allocator_type allocator_type;
  74. return static_cast<Node*>(
  75. index_node_base<Value,allocator_type>::from_value(p));
  76. }
  77. } /* namespace multi_index::detail */
  78. } /* namespace multi_index */
  79. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  80. /* Index nodes never get constructed directly by Boost.Serialization,
  81. * as archives are always fed pointers to previously existent
  82. * nodes. So, if this is called it means we are dealing with a
  83. * somehow invalid archive.
  84. */
  85. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  86. namespace serialization{
  87. #else
  88. namespace multi_index{
  89. namespace detail{
  90. #endif
  91. template<class Archive,typename Value,typename Allocator>
  92. inline void load_construct_data(
  93. Archive&,boost::multi_index::detail::index_node_base<Value,Allocator>*,
  94. const unsigned int)
  95. {
  96. throw_exception(
  97. archive::archive_exception(archive::archive_exception::other_exception));
  98. }
  99. #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
  100. } /* namespace serialization */
  101. #else
  102. } /* namespace multi_index::detail */
  103. } /* namespace multi_index */
  104. #endif
  105. #endif
  106. } /* namespace boost */
  107. #endif