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>
21#include <folly/functional/Invoke.h>
22#include <tuple>
23
24namespace 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 */
30template <typename Map, typename Key>
31typename 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}
35template <
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>
40typename Map::mapped_type
41get_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 */
51template <
52 class Map,
53 typename Key = typename Map::key_type,
54 typename Func,
55 typename = typename std::enable_if<
56 is_invocable_r<typename Map::mapped_type, Func>::value>::type>
57typename Map::mapped_type
58get_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
63/**
64 * Given a map and a key, return the value corresponding to the key in the map,
65 * or throw an exception of the specified type.
66 */
67template <
68 class E = std::out_of_range,
69 class Map,
70 typename Key = typename Map::key_type>
71const 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_exception<E>(folly::to<std::string>(exceptionStrPrefix, key));
80}
81
82template <
83 class E = std::out_of_range,
84 class Map,
85 typename Key = typename Map::key_type>
86typename 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_exception<E>(folly::to<std::string>(exceptionStrPrefix, key));
95}
96
97/**
98 * Given a map and a key, return a Optional<V> if the key exists and None if the
99 * key does not exist in the map.
100 */
101template <class Map, typename Key = typename Map::key_type>
102folly::Optional<typename Map::mapped_type> get_optional(
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
113/**
114 * Given a map and a key, return a reference to the value corresponding to the
115 * key in the map, or the given default reference if the key doesn't exist in
116 * the map.
117 */
118template <class Map, typename Key = typename Map::key_type>
119const 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
127/**
128 * Passing a temporary default value returns a dangling reference when it is
129 * returned. Lifetime extension is broken by the indirection.
130 * The caller must ensure that the default value outlives the reference returned
131 * by get_ref_default().
132 */
133template <class Map, typename Key = typename Map::key_type>
134const typename Map::mapped_type& get_ref_default(
135 const Map& map,
136 const Key& key,
137 typename Map::mapped_type&& dflt) = delete;
138
139template <class Map, typename Key = typename Map::key_type>
140const 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 */
150template <
151 class Map,
152 typename Key = typename Map::key_type,
153 typename Func,
154 typename = typename std::enable_if<
155 is_invocable_r<const typename Map::mapped_type&, Func>::value>::type,
156 typename = typename std::enable_if<
157 std::is_reference<invoke_result_t<Func>>::value>::type>
158const typename Map::mapped_type&
159get_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
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 */
168template <class Map, typename Key = typename Map::key_type>
169const 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 */
177template <class Map, typename Key = typename Map::key_type>
178typename 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.
185namespace detail {
186template <
187 class T,
188 size_t pathLength,
189 class = typename std::enable_if<(pathLength > 0)>::type>
190struct NestedMapType {
191 using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
192};
193
194template <class T>
195struct NestedMapType<T, 1> {
196 using type = typename T::mapped_type;
197};
198
199template <typename... KeysDefault>
200struct DefaultType;
201
202template <typename Default>
203struct DefaultType<Default> {
204 using type = Default;
205};
206
207template <typename Key, typename... KeysDefault>
208struct DefaultType<Key, KeysDefault...> {
209 using type = typename DefaultType<KeysDefault...>::type;
210};
211
212template <class... KeysDefault>
213auto 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 */
223template <class Map, class Key1, class Key2, class... Keys>
224auto get_optional(
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
236/**
237 * Given a map of maps and a path of keys, return a pointer to the nested value,
238 * or nullptr if the key doesn't exist in the map.
239 */
240template <class Map, class Key1, class Key2, class... Keys>
241auto 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
251template <class Map, class Key1, class Key2, class... Keys>
252auto 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
258/**
259 * Given a map and a path of keys, return the value corresponding to the nested
260 * value, or a given default value if the path doesn't exist in the map.
261 * The default value is the last parameter, and is copied when returned.
262 */
263template <
264 class Map,
265 class Key1,
266 class Key2,
267 class... KeysDefault,
268 typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
269auto get_default(
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
281/**
282 * Given a map and a path of keys, return a reference to the value corresponding
283 * to the nested value, or the given default reference if the path doesn't exist
284 * in the map.
285 * The default value is the last parameter, and must be a lvalue reference.
286 */
287template <
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<
294 typename detail::DefaultType<KeysDefault...>::type>::value>::type>
295auto get_ref_default(
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
308