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>
25 * Given a map and a key, return the value corresponding to the key in the map,
26 * or a given default value if the key doesn't exist in the map.
29 typename Map::mapped_type get_default(
30 const Map& map, const typename Map::key_type& key,
31 const typename Map::mapped_type& dflt =
32 typename Map::mapped_type()) {
33 auto pos = map.find(key);
34 return (pos != map.end() ? pos->second : dflt);
38 * Give a map and a key, return the value corresponding to the key in the map,
39 * or a given default value if the key doesn't exist in the map.
44 typename = typename std::enable_if<std::is_convertible<
45 typename std::result_of<Func()>::type,
46 typename Map::mapped_type>::value>::type>
47 typename Map::mapped_type
48 get_default(const Map& map, const typename Map::key_type& key, Func&& dflt) {
49 auto pos = map.find(key);
50 return pos != map.end() ? pos->second : dflt();
54 * Given a map and a key, return the value corresponding to the key in the map,
55 * or throw an exception of the specified type.
57 template <class E = std::out_of_range, class Map>
58 const typename Map::mapped_type& get_or_throw(
60 const typename Map::key_type& key,
61 const std::string& exceptionStrPrefix = std::string()) {
62 auto pos = map.find(key);
63 if (pos != map.end()) {
66 throw E(folly::to<std::string>(exceptionStrPrefix, key));
69 template <class E = std::out_of_range, class Map>
70 typename Map::mapped_type& get_or_throw(
72 const typename Map::key_type& key,
73 const std::string& exceptionStrPrefix = std::string()) {
74 auto pos = map.find(key);
75 if (pos != map.end()) {
78 throw E(folly::to<std::string>(exceptionStrPrefix, key));
82 * Given a map and a key, return a Optional<V> if the key exists and None if the
83 * key does not exist in the map.
86 folly::Optional<typename Map::mapped_type> get_optional(
87 const Map& map, const typename Map::key_type& key) {
88 auto pos = map.find(key);
89 if (pos != map.end()) {
90 return folly::Optional<typename Map::mapped_type>(pos->second);
97 * Given a map and a key, return a reference to the value corresponding to the
98 * key in the map, or the given default reference if the key doesn't exist in
102 const typename Map::mapped_type& get_ref_default(
103 const Map& map, const typename Map::key_type& key,
104 const typename Map::mapped_type& dflt) {
105 auto pos = map.find(key);
106 return (pos != map.end() ? pos->second : dflt);
110 * Passing a temporary default value returns a dangling reference when it is
111 * returned. Lifetime extension is broken by the indirection.
112 * The caller must ensure that the default value outlives the reference returned
113 * by get_ref_default().
116 const typename Map::mapped_type& get_ref_default(
118 const typename Map::key_type& key,
119 typename Map::mapped_type&& dflt) = delete;
122 const typename Map::mapped_type& get_ref_default(
124 const typename Map::key_type& key,
125 const typename Map::mapped_type&& dflt) = delete;
128 * Given a map and a key, return a reference to the value corresponding to the
129 * key in the map, or the given default reference if the key doesn't exist in
135 typename = typename std::enable_if<std::is_convertible<
136 typename std::result_of<Func()>::type,
137 const typename Map::mapped_type&>::value>::type,
138 typename = typename std::enable_if<
139 std::is_reference<typename std::result_of<Func()>::type>::value>::type>
140 const typename Map::mapped_type& get_ref_default(
142 const typename Map::key_type& key,
144 auto pos = map.find(key);
145 return (pos != map.end() ? pos->second : dflt());
149 * Given a map and a key, return a pointer to the value corresponding to the
150 * key in the map, or nullptr if the key doesn't exist in the map.
153 const typename Map::mapped_type* get_ptr(
154 const Map& map, const typename Map::key_type& key) {
155 auto pos = map.find(key);
156 return (pos != map.end() ? &pos->second : nullptr);
160 * Non-const overload of the above.
163 typename Map::mapped_type* get_ptr(
164 Map& map, const typename Map::key_type& key) {
165 auto pos = map.find(key);
166 return (pos != map.end() ? &pos->second : nullptr);
173 class = typename std::enable_if<(pathLength > 0)>::type>
174 struct NestedMapType {
175 using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
179 struct NestedMapType<T, 1> {
180 using type = typename T::mapped_type;
185 * Given a map of maps and a path of keys, return a pointer to the nested value,
186 * or nullptr if the key doesn't exist in the map.
188 template <class Map, class Key1, class Key2, class... Keys>
193 const Keys&... keys) ->
194 typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
195 auto pos = map.find(key1);
196 return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
199 template <class Map, class Key1, class Key2, class... Keys>
200 auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
201 -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
202 auto pos = map.find(key1);
203 return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;