1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 <tuple>
20 
21 #include <folly/Conv.h>
22 #include <folly/Optional.h>
23 #include <folly/functional/Invoke.h>
24 
25 namespace folly {
26 
27 /**
28  * Given a map and a key, return the value corresponding to the key in the map,
29  * or a given default value if the key doesn't exist in the map.
30  */
31 template <typename Map, typename Key>
get_default(const Map & map,const Key & key)32 typename Map::mapped_type get_default(const Map& map, const Key& key) {
33   auto pos = map.find(key);
34   return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{});
35 }
36 template <
37     class Map,
38     typename Key = typename Map::key_type,
39     typename Value = typename Map::mapped_type,
40     typename std::enable_if<!is_invocable_v<Value>>::type* = nullptr>
get_default(const Map & map,const Key & key,Value && dflt)41 typename Map::mapped_type get_default(
42     const Map& map, const Key& key, Value&& dflt) {
43   using M = typename Map::mapped_type;
44   auto pos = map.find(key);
45   return (pos != map.end()) ? pos->second
46                             : static_cast<M>(static_cast<Value&&>(dflt));
47 }
48 
49 /**
50  * Give a map and a key, return the value corresponding to the key in the map,
51  * or a given default value if the key doesn't exist in the map.
52  */
53 template <
54     class Map,
55     typename Key = typename Map::key_type,
56     typename Func,
57     typename = typename std::enable_if<
58         is_invocable_r_v<typename Map::mapped_type, Func>>::type>
get_default(const Map & map,const Key & key,Func && dflt)59 typename Map::mapped_type get_default(
60     const Map& map, const Key& key, Func&& dflt) {
61   auto pos = map.find(key);
62   return pos != map.end() ? pos->second : dflt();
63 }
64 
65 /**
66  * Given a map and a key, return the value corresponding to the key in the map,
67  * or throw an exception of the specified type.
68  */
69 template <
70     class E = std::out_of_range,
71     class Map,
72     typename Key = typename Map::key_type>
73 const typename Map::mapped_type& get_or_throw(
74     const Map& map,
75     const Key& key,
76     const std::string& exceptionStrPrefix = std::string()) {
77   auto pos = map.find(key);
78   if (pos != map.end()) {
79     return pos->second;
80   }
81   throw_exception<E>(folly::to<std::string>(exceptionStrPrefix, key));
82 }
83 
84 template <
85     class E = std::out_of_range,
86     class Map,
87     typename Key = typename Map::key_type>
88 typename Map::mapped_type& get_or_throw(
89     Map& map,
90     const Key& key,
91     const std::string& exceptionStrPrefix = std::string()) {
92   auto pos = map.find(key);
93   if (pos != map.end()) {
94     return pos->second;
95   }
96   throw_exception<E>(folly::to<std::string>(exceptionStrPrefix, key));
97 }
98 
99 /**
100  * Given a map and a key, return a Optional<V> if the key exists and None if the
101  * key does not exist in the map.
102  */
103 template <
104     template <typename> class Optional = folly::Optional,
105     class Map,
106     typename Key = typename Map::key_type>
get_optional(const Map & map,const Key & key)107 Optional<typename Map::mapped_type> get_optional(
108     const Map& map, const Key& key) {
109   auto pos = map.find(key);
110   if (pos != map.end()) {
111     return Optional<typename Map::mapped_type>(pos->second);
112   } else {
113     return {};
114   }
115 }
116 
117 /**
118  * Given a map and a key, return a reference to the value corresponding to the
119  * key in the map, or the given default reference if the key doesn't exist in
120  * the map.
121  */
122 template <class Map, typename Key = typename Map::key_type>
get_ref_default(const Map & map,const Key & key,const typename Map::mapped_type & dflt)123 const typename Map::mapped_type& get_ref_default(
124     const Map& map, const Key& key, const typename Map::mapped_type& dflt) {
125   auto pos = map.find(key);
126   return (pos != map.end() ? pos->second : dflt);
127 }
128 
129 /**
130  * Passing a temporary default value returns a dangling reference when it is
131  * returned. Lifetime extension is broken by the indirection.
132  * The caller must ensure that the default value outlives the reference returned
133  * by get_ref_default().
134  */
135 template <class Map, typename Key = typename Map::key_type>
136 const typename Map::mapped_type& get_ref_default(
137     const Map& map, const Key& key, 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 
145 /**
146  * Given a map and a key, return a reference to the value corresponding to the
147  * key in the map, or the given default reference if the key doesn't exist in
148  * the map.
149  */
150 template <
151     class Map,
152     typename Key = typename Map::key_type,
153     typename Func,
154     typename = typename std::enable_if<
155         is_invocable_r_v<const typename Map::mapped_type&, Func>>::type,
156     typename = typename std::enable_if<
157         std::is_reference<invoke_result_t<Func>>::value>::type>
get_ref_default(const Map & map,const Key & key,Func && dflt)158 const typename Map::mapped_type& get_ref_default(
159     const Map& map, const Key& key, Func&& dflt) {
160   auto pos = map.find(key);
161   return (pos != map.end() ? pos->second : dflt());
162 }
163 
164 /**
165  * Given a map and a key, return a pointer to the value corresponding to the
166  * key in the map, or nullptr if the key doesn't exist in the map.
167  */
168 template <class Map, typename Key = typename Map::key_type>
get_ptr(const Map & map,const Key & key)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 
174 /**
175  * Non-const overload of the above.
176  */
177 template <class Map, typename Key = typename Map::key_type>
get_ptr(Map & map,const Key & key)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>
190 struct NestedMapType {
191   using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
192 };
193 
194 template <class T>
195 struct NestedMapType<T, 1> {
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 
219 /**
220  * Given a map of maps and a path of keys, return a Optional<V> if the nested
221  * key exists and None if the nested keys does not exist in the map.
222  */
223 template <class Map, class Key1, class Key2, class... Keys>
224 auto get_optional(
225     const Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
226     -> folly::Optional<
227         typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type> {
228   auto pos = map.find(key1);
229   return pos != map.end() ? get_optional(pos->second, key2, keys...)
230                           : folly::none;
231 }
232 
233 /**
234  * Given a map of maps and a path of keys, return a pointer to the nested value,
235  * or nullptr if the key doesn't exist in the map.
236  */
237 template <class Map, class Key1, class Key2, class... Keys>
238 auto get_ptr(
239     const Map& map, const Key1& key1, const Key2& key2, const Keys&... keys) ->
240     typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
241   auto pos = map.find(key1);
242   return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
243 }
244 
245 template <class Map, class Key1, class Key2, class... Keys>
246 auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
247     -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
248   auto pos = map.find(key1);
249   return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
250 }
251 
252 /**
253  * Given a map and a path of keys, return the value corresponding to the nested
254  * value, or a given default value if the path doesn't exist in the map.
255  * The default value is the last parameter, and is copied when returned.
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 auto get_default(
264     const Map& map,
265     const Key1& key1,
266     const Key2& key2,
267     const KeysDefault&... keysDefault) ->
268     typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
269   if (const auto* ptr = get_ptr(map, key1)) {
270     return get_default(*ptr, key2, keysDefault...);
271   }
272   return detail::extract_default(keysDefault...);
273 }
274 
275 /**
276  * Given a map and a path of keys, return a reference to the value corresponding
277  * to the nested value, or the given default reference if the path doesn't exist
278  * in the map.
279  * The default value is the last parameter, and must be a lvalue reference.
280  */
281 template <
282     class Map,
283     class Key1,
284     class Key2,
285     class... KeysDefault,
286     typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
287     typename = typename std::enable_if<std::is_lvalue_reference<
288         typename detail::DefaultType<KeysDefault...>::type>::value>::type>
289 auto get_ref_default(
290     const Map& map,
291     const Key1& key1,
292     const Key2& key2,
293     KeysDefault&&... keysDefault) ->
294     typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
295     const& {
296   if (const auto* ptr = get_ptr(map, key1)) {
297     return get_ref_default(*ptr, key2, keysDefault...);
298   }
299   return detail::extract_default(keysDefault...);
300 }
301 } // namespace folly
302