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/copyable.cpp b/test/handle/copyable.cpp
index 6ce210f..b55b216 100644
--- a/test/handle/copyable.cpp
+++ b/test/handle/copyable.cpp
@@ -15,34 +15,58 @@
static std::vector<int> dropped;
static int stored_drop = 0;
+struct SimpleRef
+{
+ int operator()(const int& i) noexcept
+ {
+ reffed.push_back(i);
+ return i + 1;
+ }
+};
+
int ref(const int& i)
{
- reffed.push_back(i);
- return i + 1;
+ return SimpleRef()(i);
}
+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));
}
-int ref(const int& i, std::string&, int& si)
+struct StoreRef
{
- reffed.push_back(i);
- // Make sure we can update the stored data
- stored_ref = si++;
- return i + 1;
-}
+ int operator()(const int& i, std::string&, int& si)
+ {
+ reffed.push_back(i);
+ // Make sure we can update the stored data
+ stored_ref = si++;
+ return i + 1;
+ }
+};
-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_drop = si++;
-}
+ void operator()(int&& i, std::string&, int& si)
+ {
+ dropped.push_back(std::move(i));
+ // Make sure we can update the stored data
+ stored_drop = si++;
+ }
+};
-using SimpleHandle = Copyable<int>::Handle<drop, ref>;
-using StoreHandle = Copyable<int, std::string, int>::Handle<drop, ref>;
+using SimpleHandleOld = Copyable<int>::Handle<drop, ref>;
+using SimpleHandle = Copyable<int>::HandleF<SimpleDrop, SimpleRef>;
+using StoreHandle =
+ Copyable<int, std::string, int>::HandleF<StoreDrop, StoreRef>;
class CopyableHandleTest : public ::testing::Test
{
@@ -62,7 +86,7 @@
TEST_F(CopyableHandleTest, EmptyNoStorage)
{
- SimpleHandle h(std::nullopt);
+ SimpleHandleOld h(std::nullopt);
EXPECT_FALSE(h);
EXPECT_THROW(h.value(), std::bad_optional_access);
h.reset();
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);