proxygen
MapUtil.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/Conv.h>
20 #include <folly/Optional.h>
22 #include <tuple>
23 
24 namespace folly {
25 
30 template <typename Map, typename Key>
31 typename Map::mapped_type get_default(const Map& map, const Key& key) {
32  auto pos = map.find(key);
33  return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{});
34 }
35 template <
36  class Map,
37  typename Key = typename Map::key_type,
38  typename Value = typename Map::mapped_type,
40 typename Map::mapped_type
41 get_default(const Map& map, const Key& key, Value&& dflt) {
42  using M = typename Map::mapped_type;
43  auto pos = map.find(key);
44  return (pos != map.end()) ? (pos->second) : M(std::forward<Value>(dflt));
45 }
46 
51 template <
52  class Map,
53  typename Key = typename Map::key_type,
54  typename Func,
55  typename = typename std::enable_if<
57 typename Map::mapped_type
58 get_default(const Map& map, const Key& key, Func&& dflt) {
59  auto pos = map.find(key);
60  return pos != map.end() ? pos->second : dflt();
61 }
62 
67 template <
68  class E = std::out_of_range,
69  class Map,
70  typename Key = typename Map::key_type>
71 const typename Map::mapped_type& get_or_throw(
72  const Map& map,
73  const Key& key,
74  const std::string& exceptionStrPrefix = std::string()) {
75  auto pos = map.find(key);
76  if (pos != map.end()) {
77  return pos->second;
78  }
79  throw E(folly::to<std::string>(exceptionStrPrefix, key));
80 }
81 
82 template <
83  class E = std::out_of_range,
84  class Map,
85  typename Key = typename Map::key_type>
86 typename Map::mapped_type& get_or_throw(
87  Map& map,
88  const Key& key,
89  const std::string& exceptionStrPrefix = std::string()) {
90  auto pos = map.find(key);
91  if (pos != map.end()) {
92  return pos->second;
93  }
94  throw E(folly::to<std::string>(exceptionStrPrefix, key));
95 }
96 
101 template <class Map, typename Key = typename Map::key_type>
103  const Map& map,
104  const Key& key) {
105  auto pos = map.find(key);
106  if (pos != map.end()) {
107  return folly::Optional<typename Map::mapped_type>(pos->second);
108  } else {
109  return folly::none;
110  }
111 }
112 
118 template <class Map, typename Key = typename Map::key_type>
119 const typename Map::mapped_type& get_ref_default(
120  const Map& map,
121  const Key& key,
122  const typename Map::mapped_type& dflt) {
123  auto pos = map.find(key);
124  return (pos != map.end() ? pos->second : dflt);
125 }
126 
133 template <class Map, typename Key = typename Map::key_type>
134 const typename Map::mapped_type& get_ref_default(
135  const Map& map,
136  const Key& key,
137  typename Map::mapped_type&& dflt) = delete;
138 
139 template <class Map, typename Key = typename Map::key_type>
140 const typename Map::mapped_type& get_ref_default(
141  const Map& map,
142  const Key& key,
143  const typename Map::mapped_type&& dflt) = delete;
144 
150 template <
151  class Map,
152  typename Key = typename Map::key_type,
153  typename Func,
154  typename = typename std::enable_if<
156  typename = typename std::enable_if<
157  std::is_reference<invoke_result_t<Func>>::value>::type>
158 const typename Map::mapped_type&
159 get_ref_default(const Map& map, const Key& key, Func&& dflt) {
160  auto pos = map.find(key);
161  return (pos != map.end() ? pos->second : dflt());
162 }
163 
168 template <class Map, typename Key = typename Map::key_type>
169 const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) {
170  auto pos = map.find(key);
171  return (pos != map.end() ? &pos->second : nullptr);
172 }
173 
177 template <class Map, typename Key = typename Map::key_type>
178 typename Map::mapped_type* get_ptr(Map& map, const Key& key) {
179  auto pos = map.find(key);
180  return (pos != map.end() ? &pos->second : nullptr);
181 }
182 
183 // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
184 // the minimum supported versions.
185 namespace detail {
186 template <
187  class T,
188  size_t pathLength,
189  class = typename std::enable_if<(pathLength > 0)>::type>
191  using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
192 };
193 
194 template <class T>
196  using type = typename T::mapped_type;
197 };
198 
199 template <typename... KeysDefault>
200 struct DefaultType;
201 
202 template <typename Default>
203 struct DefaultType<Default> {
204  using type = Default;
205 };
206 
207 template <typename Key, typename... KeysDefault>
208 struct DefaultType<Key, KeysDefault...> {
209  using type = typename DefaultType<KeysDefault...>::type;
210 };
211 
212 template <class... KeysDefault>
213 auto extract_default(const KeysDefault&... keysDefault) ->
214  typename DefaultType<KeysDefault...>::type const& {
215  return std::get<sizeof...(KeysDefault) - 1>(std::tie(keysDefault...));
216 }
217 } // namespace detail
218 
223 template <class Map, class Key1, class Key2, class... Keys>
225  const Map& map,
226  const Key1& key1,
227  const Key2& key2,
228  const Keys&... keys)
229  -> folly::Optional<
230  typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type> {
231  auto pos = map.find(key1);
232  return pos != map.end() ? get_optional(pos->second, key2, keys...)
233  : folly::none;
234 }
235 
240 template <class Map, class Key1, class Key2, class... Keys>
241 auto get_ptr(
242  const Map& map,
243  const Key1& key1,
244  const Key2& key2,
245  const Keys&... keys) ->
246  typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
247  auto pos = map.find(key1);
248  return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
249 }
250 
251 template <class Map, class Key1, class Key2, class... Keys>
252 auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
253  -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
254  auto pos = map.find(key1);
255  return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
256 }
257 
263 template <
264  class Map,
265  class Key1,
266  class Key2,
267  class... KeysDefault,
268  typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
270  const Map& map,
271  const Key1& key1,
272  const Key2& key2,
273  const KeysDefault&... keysDefault) ->
274  typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
275  if (const auto* ptr = get_ptr(map, key1)) {
276  return get_default(*ptr, key2, keysDefault...);
277  }
278  return detail::extract_default(keysDefault...);
279 }
280 
287 template <
288  class Map,
289  class Key1,
290  class Key2,
291  class... KeysDefault,
292  typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
293  typename = typename std::enable_if<std::is_lvalue_reference<
296  const Map& map,
297  const Key1& key1,
298  const Key2& key2,
299  KeysDefault&&... keysDefault) ->
300  typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
301  const& {
302  if (const auto* ptr = get_ptr(map, key1)) {
303  return get_ref_default(*ptr, key2, keysDefault...);
304  }
305  return detail::extract_default(keysDefault...);
306 }
307 } // namespace folly
const Map::mapped_type * get_ptr(const Map &map, const Key &key)
Definition: MapUtil.h:169
void * ptr
Map::mapped_type get_default(const Map &map, const Key &key)
Definition: MapUtil.h:31
PskType type
const Map::mapped_type & get_ref_default(const Map &map, const Key &key, const typename Map::mapped_type &dflt)
Definition: MapUtil.h:119
internal::KeyMatcher< M > Key(M inner_matcher)
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::unordered_map< int64_t, VecT > Map
typename NestedMapType< T, pathLength-1 >::type::mapped_type type
Definition: MapUtil.h:191
Function< void()> Func
Definition: Executor.h:27
typename T::mapped_type type
Definition: MapUtil.h:196
bool Value(const T &value, M matcher)
folly::Optional< typename Map::mapped_type > get_optional(const Map &map, const Key &key)
Definition: MapUtil.h:102
Definition: Traits.h:594
static const char *const value
Definition: Conv.cpp:50
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
const char * string
Definition: Conv.cpp:212
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
const Map::mapped_type & get_or_throw(const Map &map, const Key &key, const std::string &exceptionStrPrefix=std::string())
Definition: MapUtil.h:71
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
typename DefaultType< KeysDefault... >::type type
Definition: MapUtil.h:209
auto extract_default(const KeysDefault &...keysDefault) -> typename DefaultType< KeysDefault... >::type const &
Definition: MapUtil.h:213
constexpr None none
Definition: Optional.h:87