2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/Conv.h>
20 #include <folly/Optional.h>
21 #include <folly/functional/Invoke.h>
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.
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{});
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));
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.
53 typename Key = typename Map::key_type,
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();
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.
69 class E = std::out_of_range,
71 typename Key = typename Map::key_type>
72 const typename Map::mapped_type& get_or_throw(
75 const std::string& exceptionStrPrefix = std::string()) {
76 auto pos = map.find(key);
77 if (pos != map.end()) {
80 throw E(folly::to<std::string>(exceptionStrPrefix, key));
84 class E = std::out_of_range,
86 typename Key = typename Map::key_type>
87 typename Map::mapped_type& get_or_throw(
90 const std::string& exceptionStrPrefix = std::string()) {
91 auto pos = map.find(key);
92 if (pos != map.end()) {
95 throw E(folly::to<std::string>(exceptionStrPrefix, key));
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.
102 template <class Map, typename Key = typename Map::key_type>
103 folly::Optional<typename Map::mapped_type> get_optional(
106 auto pos = map.find(key);
107 if (pos != map.end()) {
108 return folly::Optional<typename Map::mapped_type>(pos->second);
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
119 template <class Map, typename Key = typename Map::key_type>
120 const typename Map::mapped_type& get_ref_default(
123 const typename Map::mapped_type& dflt) {
124 auto pos = map.find(key);
125 return (pos != map.end() ? pos->second : dflt);
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().
134 template <class Map, typename Key = typename Map::key_type>
135 const typename Map::mapped_type& get_ref_default(
138 typename Map::mapped_type&& dflt) = delete;
140 template <class Map, typename Key = typename Map::key_type>
141 const typename Map::mapped_type& get_ref_default(
144 const typename Map::mapped_type&& dflt) = delete;
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
153 typename Key = typename Map::key_type,
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());
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.
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);
177 * Non-const overload of the above.
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);
185 // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
186 // the minimum supported versions.
191 class = typename std::enable_if<(pathLength > 0)>::type>
192 struct NestedMapType {
193 using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
197 struct NestedMapType<T, 1> {
198 using type = typename T::mapped_type;
201 template <typename... KeysDefault>
204 template <typename Default>
205 struct DefaultType<Default> {
206 using type = Default;
209 template <typename Key, typename... KeysDefault>
210 struct DefaultType<Key, KeysDefault...> {
211 using type = typename DefaultType<KeysDefault...>::type;
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...));
219 } // namespace detail
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.
225 template <class Map, class Key1, class Key2, class... Keys>
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;
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;
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.
252 class... KeysDefault,
253 typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
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...);
263 return detail::extract_default(keysDefault...);
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
270 * The default value is the last parameter, and must be a lvalue reference.
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(
284 KeysDefault&&... keysDefault) ->
285 typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
287 if (const auto* ptr = get_ptr(map, key1)) {
288 return get_ref_default(*ptr, key2, keysDefault...);
290 return detail::extract_default(keysDefault...);