handle: Support functors alongside function pointers
This allows for more flexibility in the type signatures of the
provided Drop and Ref functionality. Backwards compatibility is
maintained.
Change-Id: I9b8d66fc72d27d5feb4e01f2266dcd317733c150
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/handle/managed.cpp b/test/handle/managed.cpp
index 155ba59..fffffde 100644
--- a/test/handle/managed.cpp
+++ b/test/handle/managed.cpp
@@ -14,20 +14,32 @@
static std::vector<int> dropped;
static int stored = 0;
+struct SimpleDrop
+{
+ void operator()(int&& i) noexcept
+ {
+ dropped.push_back(std::move(i));
+ }
+};
+
void drop(int&& i)
{
- dropped.push_back(std::move(i));
+ SimpleDrop()(std::move(i));
}
-void drop(int&& i, std::string&, int& si)
+struct StoreDrop
{
- dropped.push_back(std::move(i));
- // Make sure we can update the stored data
- stored = si++;
-}
+ void operator()(int&& i, std::string&, int& si)
+ {
+ dropped.push_back(std::move(i));
+ // Make sure we can update the stored data
+ stored = si++;
+ }
+};
-using SimpleHandle = Managed<int>::Handle<drop>;
-using StoreHandle = Managed<int, std::string, int>::Handle<drop>;
+using SimpleHandleOld = Managed<int>::Handle<drop>;
+using SimpleHandle = Managed<int>::HandleF<SimpleDrop>;
+using StoreHandle = Managed<int, std::string, int>::HandleF<StoreDrop>;
class ManagedHandleTest : public ::testing::Test
{
@@ -45,7 +57,7 @@
TEST_F(ManagedHandleTest, EmptyNoStorage)
{
- SimpleHandle h(std::nullopt);
+ SimpleHandleOld h(std::nullopt);
EXPECT_FALSE(h);
EXPECT_THROW(h.value(), std::bad_optional_access);
EXPECT_THROW((void)h.release(), std::bad_optional_access);