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.
29 template <class Map, typename Key = typename Map::key_type>
30 typename Map::mapped_type get_default(
33 const typename Map::mapped_type& dflt = 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.
44 typename Key = typename Map::key_type,
46 typename = typename std::enable_if<std::is_convertible<
47 typename std::result_of<Func()>::type,
48 typename Map::mapped_type>::value>::type>
49 typename Map::mapped_type
50 get_default(const Map& map, const Key& key, Func&& dflt) {
51 auto pos = map.find(key);
52 return pos != map.end() ? pos->second : dflt();
56 * Given a map and a key, return the value corresponding to the key in the map,
57 * or throw an exception of the specified type.
60 class E = std::out_of_range,
62 typename Key = typename Map::key_type>
63 const typename Map::mapped_type& get_or_throw(
66 const std::string& exceptionStrPrefix = std::string()) {
67 auto pos = map.find(key);
68 if (pos != map.end()) {
71 throw E(folly::to<std::string>(exceptionStrPrefix, key));
75 class E = std::out_of_range,
77 typename Key = typename Map::key_type>
78 typename Map::mapped_type& get_or_throw(
81 const std::string& exceptionStrPrefix = std::string()) {
82 auto pos = map.find(key);
83 if (pos != map.end()) {
86 throw E(folly::to<std::string>(exceptionStrPrefix, key));
90 * Given a map and a key, return a Optional<V> if the key exists and None if the
91 * key does not exist in the map.
93 template <class Map, typename Key = typename Map::key_type>
94 folly::Optional<typename Map::mapped_type> get_optional(
97 auto pos = map.find(key);
98 if (pos != map.end()) {
99 return folly::Optional<typename Map::mapped_type>(pos->second);
106 * Given a map and a key, return a reference to the value corresponding to the
107 * key in the map, or the given default reference if the key doesn't exist in
110 template <class Map, typename Key = typename Map::key_type>
111 const typename Map::mapped_type& get_ref_default(
114 const typename Map::mapped_type& dflt) {
115 auto pos = map.find(key);
116 return (pos != map.end() ? pos->second : dflt);
120 * Passing a temporary default value returns a dangling reference when it is
121 * returned. Lifetime extension is broken by the indirection.
122 * The caller must ensure that the default value outlives the reference returned
123 * by get_ref_default().
125 template <class Map, typename Key = typename Map::key_type>
126 const typename Map::mapped_type& get_ref_default(
129 typename Map::mapped_type&& dflt) = delete;
131 template <class Map, typename Key = typename Map::key_type>
132 const typename Map::mapped_type& get_ref_default(
135 const typename Map::mapped_type&& dflt) = delete;
138 * Given a map and a key, return a reference to the value corresponding to the
139 * key in the map, or the given default reference if the key doesn't exist in
144 typename Key = typename Map::key_type,
146 typename = typename std::enable_if<std::is_convertible<
147 typename std::result_of<Func()>::type,
148 const typename Map::mapped_type&>::value>::type,
149 typename = typename std::enable_if<
150 std::is_reference<typename std::result_of<Func()>::type>::value>::type>
151 const typename Map::mapped_type&
152 get_ref_default(const Map& map, const Key& key, Func&& dflt) {
153 auto pos = map.find(key);
154 return (pos != map.end() ? pos->second : dflt());
158 * Given a map and a key, return a pointer to the value corresponding to the
159 * key in the map, or nullptr if the key doesn't exist in the map.
161 template <class Map, typename Key = typename Map::key_type>
162 const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) {
163 auto pos = map.find(key);
164 return (pos != map.end() ? &pos->second : nullptr);
168 * Non-const overload of the above.
170 template <class Map, typename Key = typename Map::key_type>
171 typename Map::mapped_type* get_ptr(Map& map, const Key& key) {
172 auto pos = map.find(key);
173 return (pos != map.end() ? &pos->second : nullptr);
176 // TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
177 // the minimum supported versions.
182 class = typename std::enable_if<(pathLength > 0)>::type>
183 struct NestedMapType {
184 using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
188 struct NestedMapType<T, 1> {
189 using type = typename T::mapped_type;
192 template <typename... KeysDefault>
195 template <typename Default>
196 struct DefaultType<Default> {
197 using type = Default;
200 template <typename Key, typename... KeysDefault>
201 struct DefaultType<Key, KeysDefault...> {
202 using type = typename DefaultType<KeysDefault...>::type;
205 template <class... KeysDefault>
206 auto extract_default(const KeysDefault&... keysDefault) ->
207 typename DefaultType<KeysDefault...>::type const& {
208 return std::get<sizeof...(KeysDefault)-1>(std::tie(keysDefault...));
213 * Given a map of maps and a path of keys, return a pointer to the nested value,
214 * or nullptr if the key doesn't exist in the map.
216 template <class Map, class Key1, class Key2, class... Keys>
221 const Keys&... keys) ->
222 typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
223 auto pos = map.find(key1);
224 return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
227 template <class Map, class Key1, class Key2, class... Keys>
228 auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
229 -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
230 auto pos = map.find(key1);
231 return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
235 * Given a map and a path of keys, return the value corresponding to the nested
236 * value, or a given default value if the path doesn't exist in the map.
237 * The default value is the last parameter, and is copied when returned.
243 class... KeysDefault,
244 typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
249 const KeysDefault&... keysDefault) ->
250 typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
251 if (const auto* ptr = get_ptr(map, key1)) {
252 return get_default(*ptr, key2, keysDefault...);
254 return detail::extract_default(keysDefault...);
258 * Given a map and a path of keys, return a reference to the value corresponding
259 * to the nested value, or the given default reference if the path doesn't exist
261 * The default value is the last parameter, and must be a lvalue reference.
267 class... KeysDefault,
268 typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
269 typename = typename std::enable_if<std::is_lvalue_reference<
270 typename detail::DefaultType<KeysDefault...>::type>::value>::type>
271 auto get_ref_default(
275 KeysDefault&&... keysDefault) ->
276 typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
278 if (const auto* ptr = get_ptr(map, key1)) {
279 return get_ref_default(*ptr, key2, keysDefault...);
281 return detail::extract_default(keysDefault...);