Fix comments in UnboundedQueue and DynamicBoundedQueue
[folly.git] / folly / MapUtil.h
index 77e092f6ab5b766aedf2d8e1374f2c2d4c44b3b1..433489273785a6f2677c6f36563ea2a292d8fb58 100644 (file)
@@ -18,6 +18,8 @@
 
 #include <folly/Conv.h>
 #include <folly/Optional.h>
+#include <folly/functional/Invoke.h>
+#include <tuple>
 
 namespace folly {
 
@@ -25,13 +27,21 @@ namespace folly {
  * Given 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 Map::mapped_type get_default(
-    const Map& map, const typename Map::key_type& key,
-    const typename Map::mapped_type& dflt =
-    typename Map::mapped_type()) {
+template <typename Map, typename Key>
+typename Map::mapped_type get_default(const Map& map, const Key& key) {
   auto pos = map.find(key);
-  return (pos != map.end() ? pos->second : dflt);
+  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));
 }
 
 /**
@@ -40,12 +50,13 @@ typename Map::mapped_type get_default(
  */
 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 typename Map::key_type& key, Func&& dflt) {
+get_default(const Map& map, const Key& key, Func&& dflt) {
   auto pos = map.find(key);
   return pos != map.end() ? pos->second : dflt();
 }
@@ -54,10 +65,13 @@ get_default(const Map& map, const typename Map::key_type& key, Func&& 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>
+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 typename Map::key_type& key,
+    const Key& key,
     const std::string& exceptionStrPrefix = std::string()) {
   auto pos = map.find(key);
   if (pos != map.end()) {
@@ -66,10 +80,13 @@ const typename Map::mapped_type& get_or_throw(
   throw E(folly::to<std::string>(exceptionStrPrefix, key));
 }
 
-template <class E = std::out_of_range, class Map>
+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 typename Map::key_type& key,
+    const Key& key,
     const std::string& exceptionStrPrefix = std::string()) {
   auto pos = map.find(key);
   if (pos != map.end()) {
@@ -82,9 +99,10 @@ typename Map::mapped_type& get_or_throw(
  * 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>
+template <class Map, typename Key = typename Map::key_type>
 folly::Optional<typename Map::mapped_type> get_optional(
-    const Map& map, const typename Map::key_type& key) {
+    const Map& map,
+    const Key& key) {
   auto pos = map.find(key);
   if (pos != map.end()) {
     return folly::Optional<typename Map::mapped_type>(pos->second);
@@ -98,9 +116,10 @@ folly::Optional<typename Map::mapped_type> get_optional(
  * key in the map, or the given default reference if the key doesn't exist in
  * the map.
  */
-template <class Map>
+template <class Map, typename Key = typename Map::key_type>
 const typename Map::mapped_type& get_ref_default(
-    const Map& map, const typename Map::key_type& key,
+    const Map& map,
+    const Key& key,
     const typename Map::mapped_type& dflt) {
   auto pos = map.find(key);
   return (pos != map.end() ? pos->second : dflt);
@@ -112,16 +131,16 @@ const typename Map::mapped_type& get_ref_default(
  * The caller must ensure that the default value outlives the reference returned
  * by get_ref_default().
  */
-template <class Map>
+template <class Map, typename Key = typename Map::key_type>
 const typename Map::mapped_type& get_ref_default(
     const Map& map,
-    const typename Map::key_type& key,
+    const Key& key,
     typename Map::mapped_type&& dflt) = delete;
 
-template <class Map>
+template <class Map, typename Key = typename Map::key_type>
 const typename Map::mapped_type& get_ref_default(
     const Map& map,
-    const typename Map::key_type& key,
+    const Key& key,
     const typename Map::mapped_type&& dflt) = delete;
 
 /**
@@ -131,16 +150,15 @@ const typename Map::mapped_type& get_ref_default(
  */
 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,
         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) {
+const typename Map::mapped_type&
+get_ref_default(const Map& map, const Key& key, Func&& dflt) {
   auto pos = map.find(key);
   return (pos != map.end() ? pos->second : dflt());
 }
@@ -149,9 +167,8 @@ const typename Map::mapped_type& get_ref_default(
  * 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.
  */
-template <class Map>
-const typename Map::mapped_type* get_ptr(
-    const Map& map, const typename Map::key_type& key) {
+template <class Map, typename Key = typename Map::key_type>
+const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) {
   auto pos = map.find(key);
   return (pos != map.end() ? &pos->second : nullptr);
 }
@@ -159,13 +176,14 @@ const typename Map::mapped_type* get_ptr(
 /**
  * Non-const overload of the above.
  */
-template <class Map>
-typename Map::mapped_type* get_ptr(
-    Map& map, const typename Map::key_type& key) {
+template <class Map, typename Key = typename Map::key_type>
+typename Map::mapped_type* get_ptr(Map& map, const Key& key) {
   auto pos = map.find(key);
   return (pos != map.end() ? &pos->second : nullptr);
 }
 
+// TODO: Remove the return type computations when clang 3.5 and gcc 5.1 are
+// the minimum supported versions.
 namespace detail {
 template <
     class T,
@@ -179,7 +197,26 @@ template <class T>
 struct NestedMapType<T, 1> {
   using type = typename T::mapped_type;
 };
+
+template <typename... KeysDefault>
+struct DefaultType;
+
+template <typename Default>
+struct DefaultType<Default> {
+  using type = Default;
+};
+
+template <typename Key, typename... KeysDefault>
+struct DefaultType<Key, KeysDefault...> {
+  using type = typename DefaultType<KeysDefault...>::type;
+};
+
+template <class... KeysDefault>
+auto extract_default(const KeysDefault&... keysDefault) ->
+    typename DefaultType<KeysDefault...>::type const& {
+  return std::get<sizeof...(KeysDefault)-1>(std::tie(keysDefault...));
 }
+} // namespace detail
 
 /**
  * Given a map of maps and a path of keys, return a pointer to the nested value,
@@ -203,4 +240,53 @@ auto get_ptr(Map& map, const Key1& key1, const Key2& key2, const Keys&... keys)
   return pos != map.end() ? get_ptr(pos->second, key2, keys...) : nullptr;
 }
 
-}  // namespace folly
+/**
+ * Given a map and a path of keys, return the value corresponding to the nested
+ * value, or a given default value if the path doesn't exist in the map.
+ * The default value is the last parameter, and is copied when returned.
+ */
+template <
+    class Map,
+    class Key1,
+    class Key2,
+    class... KeysDefault,
+    typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type>
+auto get_default(
+    const Map& map,
+    const Key1& key1,
+    const Key2& key2,
+    const KeysDefault&... keysDefault) ->
+    typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type {
+  if (const auto* ptr = get_ptr(map, key1)) {
+    return get_default(*ptr, key2, keysDefault...);
+  }
+  return detail::extract_default(keysDefault...);
+}
+
+/**
+ * Given a map and a path of keys, return a reference to the value corresponding
+ * to the nested value, or the given default reference if the path doesn't exist
+ * in the map.
+ * The default value is the last parameter, and must be a lvalue reference.
+ */
+template <
+    class Map,
+    class Key1,
+    class Key2,
+    class... KeysDefault,
+    typename = typename std::enable_if<sizeof...(KeysDefault) != 0>::type,
+    typename = typename std::enable_if<std::is_lvalue_reference<
+        typename detail::DefaultType<KeysDefault...>::type>::value>::type>
+auto get_ref_default(
+    const Map& map,
+    const Key1& key1,
+    const Key2& key2,
+    KeysDefault&&... keysDefault) ->
+    typename detail::NestedMapType<Map, 1 + sizeof...(KeysDefault)>::type
+    const& {
+  if (const auto* ptr = get_ptr(map, key1)) {
+    return get_ref_default(*ptr, key2, keysDefault...);
+  }
+  return detail::extract_default(keysDefault...);
+}
+} // namespace folly