get_or_throw and get_optional
authorBlake Matheny <bmatheny@fb.com>
Wed, 29 Apr 2015 15:35:30 +0000 (08:35 -0700)
committerAndrii Grynenko <andrii@fb.com>
Wed, 29 Apr 2015 22:57:36 +0000 (15:57 -0700)
Summary:
Adds get_or_throw map helper (get a value, or throw with the specified
exception type) and get_optional (get an Optional<Value>). This is a folly
backport of some util helpers in experimental.

Test Plan: Unit tests

Reviewed By: wez@fb.com

Subscribers: folly-diffs@, yfeldblum, chalfant

FB internal diff: D2029802

Tasks: 4332480

Signature: t1:2029802:1430280249:3efbb2fb9394e31b3fbe6d5bd209d12ebb7ed587

folly/MapUtil.h
folly/test/MapUtilTest.cpp

index 25863370da41ca5557bbe57086a92e116f94df3c..b00bccff4d99b0122340397fcd7554fca89d2290 100644 (file)
@@ -17,6 +17,9 @@
 #ifndef FOLLY_MAPUTIL_H_
 #define FOLLY_MAPUTIL_H_
 
+#include <folly/Conv.h>
+#include <folly/Optional.h>
+
 namespace folly {
 
 /**
@@ -32,6 +35,36 @@ typename Map::mapped_type get_default(
   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 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));
+}
+
+/**
+ * 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
index 4bb3974c1955fe2ea8f942035215d2a62b8fae29..f6d6902f7a6a1a160b3724f8f83fbfaddbae760d 100644 (file)
@@ -29,6 +29,28 @@ TEST(MapUtil, get_default) {
   EXPECT_EQ(0, get_default(m, 3));
 }
 
+TEST(MapUtil, get_or_throw) {
+  std::map<int, int> m;
+  m[1] = 2;
+  EXPECT_EQ(2, get_or_throw(m, 1));
+  EXPECT_THROW(get_or_throw(m, 2), std::out_of_range);
+}
+
+TEST(MapUtil, get_or_throw_specified) {
+  std::map<int, int> m;
+  m[1] = 2;
+  EXPECT_EQ(2, get_or_throw<std::runtime_error>(m, 1));
+  EXPECT_THROW(get_or_throw<std::runtime_error>(m, 2), std::runtime_error);
+}
+
+TEST(MapUtil, get_optional) {
+  std::map<int, int> m;
+  m[1] = 2;
+  EXPECT_TRUE(get_optional(m, 1));
+  EXPECT_EQ(2, get_optional(m, 1).value());
+  EXPECT_FALSE(get_optional(m, 2));
+}
+
 TEST(MapUtil, get_ref_default) {
   std::map<int, int> m;
   m[1] = 2;