blob: 37e7aba949a94a0a65efcbb4b7a52234814e7c09 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001
2// Copyright (C) 2005-2011 Daniel James
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
Jeff Thompson3d613fd2013-10-15 15:39:04 -07006#ifndef NDNBOOST_UNORDERED_DETAIL_EXTRACT_KEY_HPP_INCLUDED
7#define NDNBOOST_UNORDERED_DETAIL_EXTRACT_KEY_HPP_INCLUDED
Jeff Thompsona28eed82013-08-22 16:21:10 -07008
9#include <ndnboost/unordered/detail/table.hpp>
10
11namespace ndnboost {
12namespace unordered {
13namespace detail {
14
15 // key extractors
16 //
17 // no throw
18 //
19 // 'extract_key' is called with the emplace parameters to return a
20 // key if available or 'no_key' is one isn't and will need to be
21 // constructed. This could be done by overloading the emplace implementation
22 // for the different cases, but that's a bit tricky on compilers without
23 // variadic templates.
24
25 struct no_key {
26 no_key() {}
27 template <class T> no_key(T const&) {}
28 };
29
30 template <typename Key, typename T>
31 struct is_key {
32 template <typename T2>
33 static choice1::type test(T2 const&);
34 static choice2::type test(Key const&);
35
36 enum { value = sizeof(test(ndnboost::unordered::detail::make<T>())) ==
37 sizeof(choice2::type) };
38
39 typedef typename ndnboost::detail::if_true<value>::
Jeff Thompson3d613fd2013-10-15 15:39:04 -070040 NDNBOOST_NESTED_TEMPLATE then<Key const&, no_key>::type type;
Jeff Thompsona28eed82013-08-22 16:21:10 -070041 };
42
43 template <class ValueType>
44 struct set_extractor
45 {
46 typedef ValueType value_type;
47 typedef ValueType key_type;
48
49 static key_type const& extract(key_type const& v)
50 {
51 return v;
52 }
53
54 static no_key extract()
55 {
56 return no_key();
57 }
58
Jeff Thompson3d613fd2013-10-15 15:39:04 -070059#if !defined(NDNBOOST_NO_CXX11_VARIADIC_TEMPLATES)
Jeff Thompsona28eed82013-08-22 16:21:10 -070060 template <class... Args>
61 static no_key extract(Args const&...)
62 {
63 return no_key();
64 }
65#else
66 template <class Arg>
67 static no_key extract(Arg const&)
68 {
69 return no_key();
70 }
71
72 template <class Arg1, class Arg2>
73 static no_key extract(Arg1 const&, Arg2 const&)
74 {
75 return no_key();
76 }
77#endif
78 };
79
80 template <class Key, class ValueType>
81 struct map_extractor
82 {
83 typedef ValueType value_type;
84 typedef typename ndnboost::remove_const<Key>::type key_type;
85
86 static key_type const& extract(value_type const& v)
87 {
88 return v.first;
89 }
90
91 template <class Second>
92 static key_type const& extract(std::pair<key_type, Second> const& v)
93 {
94 return v.first;
95 }
96
97 template <class Second>
98 static key_type const& extract(
99 std::pair<key_type const, Second> const& v)
100 {
101 return v.first;
102 }
103
104 template <class Arg1>
105 static key_type const& extract(key_type const& k, Arg1 const&)
106 {
107 return k;
108 }
109
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700110#if !defined(NDNBOOST_NO_CXX11_VARIADIC_TEMPLATES)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700111 template <class... Args>
112 static no_key extract(Args const&...)
113 {
114 return no_key();
115 }
116#else
117
118 static no_key extract()
119 {
120 return no_key();
121 }
122
123 template <class Arg>
124 static no_key extract(Arg const&)
125 {
126 return no_key();
127 }
128
129 template <class Arg, class Arg1>
130 static no_key extract(Arg const&, Arg1 const&)
131 {
132 return no_key();
133 }
134#endif
135
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700136#if !defined(NDNBOOST_NO_CXX11_VARIADIC_TEMPLATES)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700137
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138#define NDNBOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700139 template <typename T2> \
140 static no_key extract(ndnboost::unordered::piecewise_construct_t, \
141 namespace_ tuple<> const&, T2 const&) \
142 { \
143 return no_key(); \
144 } \
145 \
146 template <typename T, typename T2> \
147 static typename is_key<key_type, T>::type \
148 extract(ndnboost::unordered::piecewise_construct_t, \
149 namespace_ tuple<T> const& k, T2 const&) \
150 { \
151 return typename is_key<key_type, T>::type( \
152 namespace_ get<0>(k)); \
153 }
154
155#else
156
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700157#define NDNBOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \
Jeff Thompsona28eed82013-08-22 16:21:10 -0700158 static no_key extract(ndnboost::unordered::piecewise_construct_t, \
159 namespace_ tuple<> const&) \
160 { \
161 return no_key(); \
162 } \
163 \
164 template <typename T> \
165 static typename is_key<key_type, T>::type \
166 extract(ndnboost::unordered::piecewise_construct_t, \
167 namespace_ tuple<T> const& k) \
168 { \
169 return typename is_key<key_type, T>::type( \
170 namespace_ get<0>(k)); \
171 }
172
173#endif
174
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700175NDNBOOST_UNORDERED_KEY_FROM_TUPLE(ndnboost::)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700176
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700177#if !defined(NDNBOOST_NO_CXX11_HDR_TUPLE)
178NDNBOOST_UNORDERED_KEY_FROM_TUPLE(std::)
Jeff Thompsona28eed82013-08-22 16:21:10 -0700179#endif
180 };
181}}}
182
183#endif