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>
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.
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);
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.
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();
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.
58 template <class E = std::out_of_range, class Map>
59 const typename Map::mapped_type& get_or_throw(
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()) {
67 throw E(folly::to<std::string>(exceptionStrPrefix, key));
70 template <class E = std::out_of_range, class Map>
71 typename Map::mapped_type& get_or_throw(
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()) {
79 throw E(folly::to<std::string>(exceptionStrPrefix, key));
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.
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);
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
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);
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().
117 const typename Map::mapped_type& get_ref_default(
119 const typename Map::key_type& key,
120 typename Map::mapped_type&& dflt) = delete;
123 const typename Map::mapped_type& get_ref_default(
125 const typename Map::key_type& key,
126 const typename Map::mapped_type&& dflt) = delete;
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
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(
143 const typename Map::key_type& key,
145 auto pos = map.find(key);
146 return (pos != map.end() ? pos->second : dflt());
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.
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);
161 * Non-const overload of the above.
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);
170 // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
171 // the minimum supported versions.
176 class = typename std::enable_if<(pathLength > 0)>::type>
177 struct NestedMapType {
178 using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
182 struct NestedMapType<T, 1> {
183 using type = typename T::mapped_type;
186 template <typename... KeysDefault>
189 template <typename Default>
190 struct DefaultType<Default> {
191 using type = Default;
194 template <typename Key, typename... KeysDefault>
195 struct DefaultType<Key, KeysDefault...> {
196 using type = typename DefaultType<KeysDefault...>::type;
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...));
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.
210 template <class Map, class Key1, class Key2, class... Keys>
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;
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;
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.
237 class... KeysDefault,
238 typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
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...);
248 return detail::extract_default(keysDefault...);
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
255 * The default value is the last parameter, and must be a lvalue reference.
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(
269 KeysDefault&&... keysDefault) ->
270 typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
272 if (const auto* ptr = get_ptr(map, key1)) {
273 return get_ref_default(*ptr, key2, keysDefault...);
275 return detail::extract_default(keysDefault...);