Disallow temporary default values in get_ref_default()
[folly.git] / folly / MapUtil.h
index 25863370da41ca5557bbe57086a92e116f94df3c..77e092f6ab5b766aedf2d8e1374f2c2d4c44b3b1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#ifndef FOLLY_MAPUTIL_H_
-#define FOLLY_MAPUTIL_H_
+#pragma once
+
+#include <folly/Conv.h>
+#include <folly/Optional.h>
 
 namespace folly {
 
@@ -32,6 +34,65 @@ typename Map::mapped_type get_default(
   return (pos != map.end() ? pos->second : 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 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 typename Map::key_type& 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>
+const typename Map::mapped_type& get_or_throw(
+    const Map& map,
+    const typename Map::key_type& 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 Map::mapped_type& get_or_throw(
+    Map& map,
+    const typename Map::key_type& 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>
+folly::Optional<typename Map::mapped_type> get_optional(
+    const Map& map, const typename Map::key_type& key) {
+  auto pos = map.find(key);
+  if (pos != map.end()) {
+    return folly::Optional<typename Map::mapped_type>(pos->second);
+  } else {
+    return folly::none;
+  }
+}
+
 /**
  * Given a map and a key, return a reference to the value corresponding to the
  * key in the map, or the given default reference if the key doesn't exist in
@@ -45,6 +106,45 @@ const typename Map::mapped_type& get_ref_default(
   return (pos != map.end() ? pos->second : dflt);
 }
 
+/**
+ * Passing a temporary default value returns a dangling reference when it is
+ * returned. Lifetime extension is broken by the indirection.
+ * The caller must ensure that the default value outlives the reference returned
+ * by get_ref_default().
+ */
+template <class Map>
+const typename Map::mapped_type& get_ref_default(
+    const Map& map,
+    const typename Map::key_type& key,
+    typename Map::mapped_type&& dflt) = delete;
+
+template <class Map>
+const typename Map::mapped_type& get_ref_default(
+    const Map& map,
+    const typename Map::key_type& key,
+    const typename Map::mapped_type&& dflt) = delete;
+
+/**
+ * Given a map and a key, return a reference to the value corresponding to the
+ * key in the map, or the given default reference if the key doesn't exist in
+ * the map.
+ */
+template <
+    class Map,
+    typename Func,
+    typename = typename std::enable_if<std::is_convertible<
+        typename std::result_of<Func()>::type,
+        const typename Map::mapped_type&>::value>::type,
+    typename = typename std::enable_if<
+        std::is_reference<typename std::result_of<Func()>::type>::value>::type>
+const typename Map::mapped_type& get_ref_default(
+    const Map& map,
+    const typename Map::key_type& key,
+    Func&& dflt) {
+  auto pos = map.find(key);
+  return (pos != map.end() ? pos->second : dflt());
+}
+
 /**
  * Given a map and a key, return a pointer to the value corresponding to the
  * key in the map, or nullptr if the key doesn't exist in the map.
@@ -66,6 +166,41 @@ typename Map::mapped_type* get_ptr(
   return (pos != map.end() ? &pos->second : nullptr);
 }
 
-}  // namespace folly
+namespace detail {
+template <
+    class T,
+    size_t pathLength,
+    class = typename std::enable_if<(pathLength > 0)>::type>
+struct NestedMapType {
+  using type = typename NestedMapType<T, pathLength - 1>::type::mapped_type;
+};
+
+template <class T>
+struct NestedMapType<T, 1> {
+  using type = typename T::mapped_type;
+};
+}
+
+/**
+ * Given a map of maps and a path of keys, return a pointer to the nested value,
+ * or nullptr if the key doesn't exist in the map.
+ */
+template <class Map, class Key1, class Key2, class... Keys>
+auto get_ptr(
+    const Map& map,
+    const Key1& key1,
+    const Key2& key2,
+    const Keys&... keys) ->
+    typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type const* {
+  auto pos = map.find(key1);
+  return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
+}
 
-#endif /* FOLLY_MAPUTIL_H_ */
+template <class Map, class Key1, class Key2, class... Keys>
+auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
+    -> typename detail::NestedMapType<Map, 2 + sizeof...(Keys)>::type* {
+  auto pos = map.find(key1);
+  return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
+}
+
+}  // namespace folly