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