Use simpler tags for ctor dispatch in exception_wrapper
[folly.git] / folly / MapUtil.h
1 /*
2  * Copyright 2017 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>
21 #include <folly/functional/Invoke.h>
22 #include <tuple>
23
24 namespace folly {
25
26 /**
27  * Given a map and a key, return the value corresponding to the key in the map,
28  * or a given default value if the key doesn't exist in the map.
29  */
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,
39     typename std::enable_if<!is_invocable<Value>::value>::type* = nullptr>
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
47 /**
48  * Give a map and a key, return the value corresponding to the key in the map,
49  * or a given default value if the key doesn't exist in the map.
50  */
51 template <
52     class Map,
53     typename Key = typename Map::key_type,
54     typename Func,
55     typename = typename std::enable_if<std::is_convertible<
56         typename std::result_of<Func()>::type,
57         typename Map::mapped_type>::value>::type>
58 typename Map::mapped_type
59 get_default(const Map& map, const Key& key, Func&& dflt) {
60   auto pos = map.find(key);
61   return pos != map.end() ? pos->second : dflt();
62 }
63
64 /**
65  * Given a map and a key, return the value corresponding to the key in the map,
66  * or throw an exception of the specified type.
67  */
68 template <
69     class E = std::out_of_range,
70     class Map,
71     typename Key = typename Map::key_type>
72 const typename Map::mapped_type& get_or_throw(
73     const Map& map,
74     const Key& key,
75     const std::string& exceptionStrPrefix = std::string()) {
76   auto pos = map.find(key);
77   if (pos != map.end()) {
78     return pos->second;
79   }
80   throw E(folly::to<std::string>(exceptionStrPrefix, key));
81 }
82
83 template <
84     class E = std::out_of_range,
85     class Map,
86     typename Key = typename Map::key_type>
87 typename Map::mapped_type& get_or_throw(
88     Map& map,
89     const Key& key,
90     const std::string& exceptionStrPrefix = std::string()) {
91   auto pos = map.find(key);
92   if (pos != map.end()) {
93     return pos->second;
94   }
95   throw E(folly::to<std::string>(exceptionStrPrefix, key));
96 }
97
98 /**
99  * Given a map and a key, return a Optional<V> if the key exists and None if the
100  * key does not exist in the map.
101  */
102 template <class Map, typename Key = typename Map::key_type>
103 folly::Optional<typename Map::mapped_type> get_optional(
104     const Map& map,
105     const Key& key) {
106   auto pos = map.find(key);
107   if (pos != map.end()) {
108     return folly::Optional<typename Map::mapped_type>(pos->second);
109   } else {
110     return folly::none;
111   }
112 }
113
114 /**
115  * Given a map and a key, return a reference to the value corresponding to the
116  * key in the map, or the given default reference if the key doesn't exist in
117  * the map.
118  */
119 template <class Map, typename Key = typename Map::key_type>
120 const typename Map::mapped_type& get_ref_default(
121     const Map& map,
122     const Key& key,
123     const typename Map::mapped_type& dflt) {
124   auto pos = map.find(key);
125   return (pos != map.end() ? pos->second : dflt);
126 }
127
128 /**
129  * Passing a temporary default value returns a dangling reference when it is
130  * returned. Lifetime extension is broken by the indirection.
131  * The caller must ensure that the default value outlives the reference returned
132  * by get_ref_default().
133  */
134 template <class Map, typename Key = typename Map::key_type>
135 const typename Map::mapped_type& get_ref_default(
136     const Map& map,
137     const Key& key,
138     typename Map::mapped_type&& dflt) = delete;
139
140 template <class Map, typename Key = typename Map::key_type>
141 const typename Map::mapped_type& get_ref_default(
142     const Map& map,
143     const Key& key,
144     const typename Map::mapped_type&& dflt) = delete;
145
146 /**
147  * Given a map and a key, return a reference to the value corresponding to the
148  * key in the map, or the given default reference if the key doesn't exist in
149  * the map.
150  */
151 template <
152     class Map,
153     typename Key = typename Map::key_type,
154     typename Func,
155     typename = typename std::enable_if<std::is_convertible<
156         typename std::result_of<Func()>::type,
157         const typename Map::mapped_type&>::value>::type,
158     typename = typename std::enable_if<
159         std::is_reference<typename std::result_of<Func()>::type>::value>::type>
160 const typename Map::mapped_type&
161 get_ref_default(const Map& map, const Key& key, Func&& dflt) {
162   auto pos = map.find(key);
163   return (pos != map.end() ? pos->second : dflt());
164 }
165
166 /**
167  * Given a map and a key, return a pointer to the value corresponding to the
168  * key in the map, or nullptr if the key doesn't exist in the map.
169  */
170 template <class Map, typename Key = typename Map::key_type>
171 const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) {
172   auto pos = map.find(key);
173   return (pos != map.end() ? &pos->second : nullptr);
174 }
175
176 /**
177  * Non-const overload of the above.
178  */
179 template <class Map, typename Key = typename Map::key_type>
180 typename Map::mapped_type* get_ptr(Map& map, const Key& key) {
181   auto pos = map.find(key);
182   return (pos != map.end() ? &pos->second : nullptr);
183 }
184
185 // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
186 // the minimum supported versions.
187 namespace detail {
188 template <
189     class T,
190     size_t pathLength,
191     class = typename std::enable_if<(pathLength > 0)>::type>
192 struct NestedMapType {
193   using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
194 };
195
196 template <class T>
197 struct NestedMapType<T, 1> {
198   using type = typename T::mapped_type;
199 };
200
201 template <typename... KeysDefault>
202 struct DefaultType;
203
204 template <typename Default>
205 struct DefaultType<Default> {
206   using type = Default;
207 };
208
209 template <typename Key, typename... KeysDefault>
210 struct DefaultType<Key, KeysDefault...> {
211   using type = typename DefaultType<KeysDefault...>::type;
212 };
213
214 template <class... KeysDefault>
215 auto extract_default(const KeysDefault&... keysDefault) ->
216     typename DefaultType<KeysDefault...>::type const& {
217   return std::get<sizeof...(KeysDefault)-1>(std::tie(keysDefault...));
218 }
219 } // namespace detail
220
221 /**
222  * Given a map of maps and a path of keys, return a pointer to the nested value,
223  * or nullptr if the key doesn't exist in the map.
224  */
225 template <class Map, class Key1, class Key2, class... Keys>
226 auto get_ptr(
227     const Map& map,
228     const Key1& key1,
229     const Key2& key2,
230     const Keys&... keys) ->
231     typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
232   auto pos = map.find(key1);
233   return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
234 }
235
236 template <class Map, class Key1, class Key2, class... Keys>
237 auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
238     -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
239   auto pos = map.find(key1);
240   return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
241 }
242
243 /**
244  * Given a map and a path of keys, return the value corresponding to the nested
245  * value, or a given default value if the path doesn't exist in the map.
246  * The default value is the last parameter, and is copied when returned.
247  */
248 template <
249     class Map,
250     class Key1,
251     class Key2,
252     class... KeysDefault,
253     typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
254 auto get_default(
255     const Map& map,
256     const Key1& key1,
257     const Key2& key2,
258     const KeysDefault&... keysDefault) ->
259     typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
260   if (const auto* ptr = get_ptr(map, key1)) {
261     return get_default(*ptr, key2, keysDefault...);
262   }
263   return detail::extract_default(keysDefault...);
264 }
265
266 /**
267  * Given a map and a path of keys, return a reference to the value corresponding
268  * to the nested value, or the given default reference if the path doesn't exist
269  * in the map.
270  * The default value is the last parameter, and must be a lvalue reference.
271  */
272 template <
273     class Map,
274     class Key1,
275     class Key2,
276     class... KeysDefault,
277     typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
278     typename = typename std::enable_if<std::is_lvalue_reference<
279         typename detail::DefaultType<KeysDefault...>::type>::value>::type>
280 auto get_ref_default(
281     const Map& map,
282     const Key1& key1,
283     const Key2& key2,
284     KeysDefault&&... keysDefault) ->
285     typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
286     const& {
287   if (const auto* ptr = get_ptr(map, key1)) {
288     return get_ref_default(*ptr, key2, keysDefault...);
289   }
290   return detail::extract_default(keysDefault...);
291 }
292 } // namespace folly