if (!hasException()) {
return false;
}
- return e_.with_exception(std::move(func));
+ return e_.with_exception<Ex>(std::move(func));
}
template <class Ex, class F>
+ bool withException(F func) const {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception<Ex>(std::move(func));
+ }
+
+ /*
+ * If the Try contains an exception and it is of type compatible with Ex as
+ * deduced from the first parameter of func, execute func(Ex)
+ *
+ * @param func a function that takes a single parameter of type const Ex&
+ *
+ * @returns True if the Try held an Ex and func was executed, false otherwise
+ */
+ template <class F>
+ bool withException(F func) {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception(std::move(func));
+ }
+ template <class F>
bool withException(F func) const {
if (!hasException()) {
return false;
if (!hasException()) {
return false;
}
- return e_.with_exception(std::move(func));
+ return e_.with_exception<Ex>(std::move(func));
}
template <class Ex, class F>
+ bool withException(F func) const {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception<Ex>(std::move(func));
+ }
+
+ /*
+ * If the Try contains an exception and it is of type compatible with Ex as
+ * deduced from the first parameter of func, execute func(Ex)
+ *
+ * @param func a function that takes a single parameter of type const Ex&
+ *
+ * @returns True if the Try held an Ex and func was executed, false otherwise
+ */
+ template <class F>
+ bool withException(F func) {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception(std::move(func));
+ }
+ template <class F>
bool withException(F func) const {
if (!hasException()) {
return false;
EXPECT_EQ(num, t.tryGetExceptionObject<int>());
}
}
+
+TEST(Try, withException) {
+ auto ew = make_exception_wrapper<std::range_error>("oops");
+
+ {
+ auto t = Try<bool>(true);
+ EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error&) {}));
+ }
+
+ {
+ auto t = Try<bool>(ew);
+ EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error&) {}));
+ }
+
+ {
+ auto t = Try<void>();
+ EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException([](std::runtime_error&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error&) {}));
+ }
+
+ {
+ auto t = Try<void>(ew);
+ EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_TRUE(t.withException([](std::runtime_error&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error&) {}));
+ }
+
+ {
+ auto const t = Try<bool>(true);
+ EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
+ }
+
+ {
+ auto const t = Try<bool>(ew);
+ EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
+ }
+
+ {
+ auto const t = Try<void>();
+ EXPECT_FALSE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException([](std::runtime_error const&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
+ }
+
+ {
+ auto const t = Try<void>(ew);
+ EXPECT_TRUE(t.withException<std::runtime_error>([](auto&) {}));
+ EXPECT_FALSE(t.withException<std::logic_error>([](auto&) {}));
+ EXPECT_TRUE(t.withException([](std::runtime_error const&) {}));
+ EXPECT_FALSE(t.withException([](std::logic_error const&) {}));
+ }
+}