Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 6 | #ifndef BOOST_UNORDERED_DETAIL_EXTRACT_KEY_HPP_INCLUDED |
| 7 | #define BOOST_UNORDERED_DETAIL_EXTRACT_KEY_HPP_INCLUDED |
| 8 | |
| 9 | #include <ndnboost/unordered/detail/table.hpp> |
| 10 | |
| 11 | namespace ndnboost { |
| 12 | namespace unordered { |
| 13 | namespace 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>:: |
| 40 | BOOST_NESTED_TEMPLATE then<Key const&, no_key>::type type; |
| 41 | }; |
| 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 | |
| 59 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
| 60 | 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 | |
| 110 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
| 111 | 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 | |
| 136 | #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) |
| 137 | |
| 138 | #define BOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \ |
| 139 | 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 | |
| 157 | #define BOOST_UNORDERED_KEY_FROM_TUPLE(namespace_) \ |
| 158 | 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 | |
| 175 | BOOST_UNORDERED_KEY_FROM_TUPLE(ndnboost::) |
| 176 | |
| 177 | #if !defined(BOOST_NO_CXX11_HDR_TUPLE) |
| 178 | BOOST_UNORDERED_KEY_FROM_TUPLE(std::) |
| 179 | #endif |
| 180 | }; |
| 181 | }}} |
| 182 | |
| 183 | #endif |