+ return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{});
+}
+template <
+ class Map,
+ typename Key = typename Map::key_type,
+ typename Value = typename Map::mapped_type,
+ typename std::enable_if<!is_invocable<Value>::value>::type* = nullptr>
+typename Map::mapped_type
+get_default(const Map& map, const Key& key, Value&& dflt) {
+ using M = typename Map::mapped_type;
+ auto pos = map.find(key);
+ return (pos != map.end()) ? (pos->second) : M(std::forward<Value>(dflt));
+}
+
+/**
+ * Give a map and a key, return the value corresponding to the key in the map,
+ * or a given default value if the key doesn't exist in the map.
+ */
+template <
+ class Map,
+ typename Key = typename Map::key_type,
+ typename Func,
+ typename = typename std::enable_if<std::is_convertible<
+ typename std::result_of<Func()>::type,
+ typename Map::mapped_type>::value>::type>
+typename Map::mapped_type
+get_default(const Map& map, const Key& key, Func&& dflt) {
+ auto pos = map.find(key);
+ return pos != map.end() ? pos->second : dflt();
+}
+
+/**
+ * Given a map and a key, return the value corresponding to the key in the map,
+ * or throw an exception of the specified type.
+ */
+template <
+ class E = std::out_of_range,
+ class Map,
+ typename Key = typename Map::key_type>
+const typename Map::mapped_type& get_or_throw(
+ const Map& map,
+ const Key& key,
+ const std::string& exceptionStrPrefix = std::string()) {
+ auto pos = map.find(key);
+ if (pos != map.end()) {
+ return pos->second;
+ }
+ throw E(folly::to<std::string>(exceptionStrPrefix, key));
+}
+
+template <
+ class E = std::out_of_range,
+ class Map,
+ typename Key = typename Map::key_type>
+typename Map::mapped_type& get_or_throw(
+ Map& map,
+ const Key& key,
+ const std::string& exceptionStrPrefix = std::string()) {
+ auto pos = map.find(key);
+ if (pos != map.end()) {
+ return pos->second;
+ }
+ throw E(folly::to<std::string>(exceptionStrPrefix, key));
+}
+
+/**
+ * Given a map and a key, return a Optional<V> if the key exists and None if the
+ * key does not exist in the map.
+ */
+template <class Map, typename Key = typename Map::key_type>
+folly::Optional<typename Map::mapped_type> get_optional(
+ const Map& map,
+ const Key& key) {
+ auto pos = map.find(key);
+ if (pos != map.end()) {
+ return folly::Optional<typename Map::mapped_type>(pos->second);
+ } else {
+ return folly::none;
+ }