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