2 * Copyright 2016 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 typename Map::mapped_type get_or_throw(
59 const Map& map, const typename Map::key_type& key,
60 const std::string& exceptionStrPrefix = std::string()) {
61 auto pos = map.find(key);
62 if (pos != map.end()) {
65 throw E(folly::to<std::string>(exceptionStrPrefix, key));
69 * Given a map and a key, return a Optional<V> if the key exists and None if the
70 * key does not exist in the map.
73 folly::Optional<typename Map::mapped_type> get_optional(
74 const Map& map, const typename Map::key_type& key) {
75 auto pos = map.find(key);
76 if (pos != map.end()) {
77 return folly::Optional<typename Map::mapped_type>(pos->second);
84 * Given a map and a key, return a reference to the value corresponding to the
85 * key in the map, or the given default reference if the key doesn't exist in
89 const typename Map::mapped_type& get_ref_default(
90 const Map& map, const typename Map::key_type& key,
91 const typename Map::mapped_type& dflt) {
92 auto pos = map.find(key);
93 return (pos != map.end() ? pos->second : dflt);
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
104 typename = typename std::enable_if<std::is_convertible<
105 typename std::result_of<Func()>::type,
106 const typename Map::mapped_type&>::value>::type>
107 const typename Map::mapped_type& get_ref_default(
109 const typename Map::key_type& key,
111 auto pos = map.find(key);
112 return (pos != map.end() ? pos->second : dflt());
116 * Given a map and a key, return a pointer to the value corresponding to the
117 * key in the map, or nullptr if the key doesn't exist in the map.
120 const typename Map::mapped_type* get_ptr(
121 const Map& map, const typename Map::key_type& key) {
122 auto pos = map.find(key);
123 return (pos != map.end() ? &pos->second : nullptr);
127 * Non-const overload of the above.
130 typename Map::mapped_type* get_ptr(
131 Map& map, const typename Map::key_type& key) {
132 auto pos = map.find(key);
133 return (pos != map.end() ? &pos->second : nullptr);