Chris@101
|
1 /* Copyright 2003-2013 Joaquin M Lopez Munoz.
|
Chris@16
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 *
|
Chris@16
|
6 * See http://www.boost.org/libs/multi_index for library home page.
|
Chris@16
|
7 */
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_MULTI_INDEX_IDENTITY_HPP
|
Chris@16
|
10 #define BOOST_MULTI_INDEX_IDENTITY_HPP
|
Chris@16
|
11
|
Chris@101
|
12 #if defined(_MSC_VER)
|
Chris@16
|
13 #pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/config.hpp>
|
Chris@16
|
17 #include <boost/mpl/if.hpp>
|
Chris@16
|
18 #include <boost/multi_index/identity_fwd.hpp>
|
Chris@16
|
19 #include <boost/type_traits/is_const.hpp>
|
Chris@16
|
20 #include <boost/type_traits/remove_const.hpp>
|
Chris@16
|
21 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
24 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
25 #endif
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost{
|
Chris@16
|
28
|
Chris@16
|
29 template<class Type> class reference_wrapper; /* fwd decl. */
|
Chris@16
|
30
|
Chris@16
|
31 namespace multi_index{
|
Chris@16
|
32
|
Chris@16
|
33 namespace detail{
|
Chris@16
|
34
|
Chris@16
|
35 /* identity is a do-nothing key extractor that returns the [const] Type&
|
Chris@16
|
36 * object passed.
|
Chris@16
|
37 * Additionally, identity is overloaded to support referece_wrappers
|
Chris@16
|
38 * of Type and "chained pointers" to Type's. By chained pointer to Type we
|
Chris@16
|
39 * mean a type P such that, given a p of type P
|
Chris@16
|
40 * *...n...*x is convertible to Type&, for some n>=1.
|
Chris@16
|
41 * Examples of chained pointers are raw and smart pointers, iterators and
|
Chris@16
|
42 * arbitrary combinations of these (vg. Type** or auto_ptr<Type*>.)
|
Chris@16
|
43 */
|
Chris@16
|
44
|
Chris@16
|
45 template<typename Type>
|
Chris@16
|
46 struct const_identity_base
|
Chris@16
|
47 {
|
Chris@16
|
48 typedef Type result_type;
|
Chris@16
|
49
|
Chris@16
|
50 template<typename ChainedPtr>
|
Chris@16
|
51
|
Chris@16
|
52 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
53 typename disable_if<is_convertible<const ChainedPtr&,Type&>,Type&>::type
|
Chris@16
|
54 #else
|
Chris@16
|
55 Type&
|
Chris@16
|
56 #endif
|
Chris@16
|
57
|
Chris@16
|
58 operator()(const ChainedPtr& x)const
|
Chris@16
|
59 {
|
Chris@16
|
60 return operator()(*x);
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 Type& operator()(Type& x)const
|
Chris@16
|
64 {
|
Chris@16
|
65 return x;
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 Type& operator()(const reference_wrapper<Type>& x)const
|
Chris@16
|
69 {
|
Chris@16
|
70 return x.get();
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 Type& operator()(
|
Chris@101
|
74 const reference_wrapper<typename remove_const<Type>::type>& x)const
|
Chris@16
|
75 {
|
Chris@16
|
76 return x.get();
|
Chris@16
|
77 }
|
Chris@16
|
78 };
|
Chris@16
|
79
|
Chris@16
|
80 template<typename Type>
|
Chris@16
|
81 struct non_const_identity_base
|
Chris@16
|
82 {
|
Chris@16
|
83 typedef Type result_type;
|
Chris@16
|
84
|
Chris@16
|
85 /* templatized for pointer-like types */
|
Chris@16
|
86
|
Chris@16
|
87 template<typename ChainedPtr>
|
Chris@16
|
88
|
Chris@16
|
89 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
90 typename disable_if<
|
Chris@16
|
91 is_convertible<const ChainedPtr&,const Type&>,Type&>::type
|
Chris@16
|
92 #else
|
Chris@16
|
93 Type&
|
Chris@16
|
94 #endif
|
Chris@16
|
95
|
Chris@16
|
96 operator()(const ChainedPtr& x)const
|
Chris@16
|
97 {
|
Chris@16
|
98 return operator()(*x);
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@101
|
101 const Type& operator()(const Type& x)const
|
Chris@16
|
102 {
|
Chris@16
|
103 return x;
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 Type& operator()(Type& x)const
|
Chris@16
|
107 {
|
Chris@16
|
108 return x;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@101
|
111 const Type& operator()(const reference_wrapper<const Type>& x)const
|
Chris@16
|
112 {
|
Chris@16
|
113 return x.get();
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 Type& operator()(const reference_wrapper<Type>& x)const
|
Chris@16
|
117 {
|
Chris@16
|
118 return x.get();
|
Chris@16
|
119 }
|
Chris@16
|
120 };
|
Chris@16
|
121
|
Chris@16
|
122 } /* namespace multi_index::detail */
|
Chris@16
|
123
|
Chris@16
|
124 template<class Type>
|
Chris@16
|
125 struct identity:
|
Chris@16
|
126 mpl::if_c<
|
Chris@16
|
127 is_const<Type>::value,
|
Chris@16
|
128 detail::const_identity_base<Type>,detail::non_const_identity_base<Type>
|
Chris@16
|
129 >::type
|
Chris@16
|
130 {
|
Chris@16
|
131 };
|
Chris@16
|
132
|
Chris@16
|
133 } /* namespace multi_index */
|
Chris@16
|
134
|
Chris@16
|
135 } /* namespace boost */
|
Chris@16
|
136
|
Chris@16
|
137 #endif
|