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