sdref: Moves should actually invalidate the reference
This also makes it possible to use in boolean expressions to check
validity.
diff --git a/src/sdeventplus/internal/sdref.cpp b/src/sdeventplus/internal/sdref.cpp
index 47faa87..add6b90 100644
--- a/src/sdeventplus/internal/sdref.cpp
+++ b/src/sdeventplus/internal/sdref.cpp
@@ -49,6 +49,14 @@
}
template <typename T>
+SdRef<T>::SdRef(SdRef&& other) :
+ sdevent(std::move(other.sdevent)), take_ref(std::move(other.take_ref)),
+ release_ref(std::move(other.release_ref)), ref(std::move(other.ref))
+{
+ other.ref = nullptr;
+}
+
+template <typename T>
SdRef<T>& SdRef<T>::operator=(SdRef&& other)
{
if (this != &other)
@@ -63,6 +71,7 @@
take_ref = std::move(other.take_ref);
release_ref = std::move(other.release_ref);
ref = std::move(other.ref);
+ other.ref = nullptr;
}
return *this;
}
@@ -76,6 +85,12 @@
}
template <typename T>
+SdRef<T>::operator bool() const
+{
+ return ref != nullptr;
+}
+
+template <typename T>
T* SdRef<T>::get() const
{
return ref;
diff --git a/src/sdeventplus/internal/sdref.hpp b/src/sdeventplus/internal/sdref.hpp
index c0c77fc..12e395f 100644
--- a/src/sdeventplus/internal/sdref.hpp
+++ b/src/sdeventplus/internal/sdref.hpp
@@ -23,9 +23,10 @@
SdRef(const SdRef& other);
SdRef& operator=(const SdRef& other);
- SdRef(SdRef&& other) = default;
+ SdRef(SdRef&& other);
SdRef& operator=(SdRef&& other);
+ explicit operator bool() const;
T* get() const;
private:
diff --git a/test/internal/sdref.cpp b/test/internal/sdref.cpp
index 190d388..3b261d5 100644
--- a/test/internal/sdref.cpp
+++ b/test/internal/sdref.cpp
@@ -28,6 +28,7 @@
.WillOnce(testing::Return(expected_event));
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -38,6 +39,7 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -48,11 +50,15 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(testing::Return(expected_event));
SdRef<sd_event> event2(event);
+ EXPECT_TRUE(event);
+ EXPECT_EQ(expected_event, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -64,9 +70,13 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
SdRef<sd_event> event2(std::move(event));
+ EXPECT_FALSE(event);
+ EXPECT_EQ(nullptr, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -77,9 +87,11 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock2);
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event2, event2.get());
EXPECT_CALL(mock2, sd_event_unref(expected_event2))
@@ -87,6 +99,9 @@
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(testing::Return(expected_event));
event2 = event;
+ EXPECT_TRUE(event);
+ EXPECT_EQ(expected_event, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -98,12 +113,17 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock2);
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event2, event2.get());
{
SdRef<sd_event> event_mover(std::move(event2));
+ EXPECT_FALSE(event2);
+ EXPECT_EQ(nullptr, event2.get());
+ EXPECT_TRUE(event_mover);
EXPECT_EQ(expected_event2, event_mover.get());
EXPECT_CALL(mock2, sd_event_unref(expected_event2))
@@ -113,6 +133,9 @@
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(testing::Return(expected_event));
event2 = event;
+ EXPECT_TRUE(event);
+ EXPECT_EQ(expected_event, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -124,9 +147,12 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
event = event;
+ EXPECT_TRUE(event);
+ EXPECT_EQ(expected_event, event.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
.WillOnce(testing::Return(nullptr));
}
@@ -135,14 +161,19 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
SdRef<sd_event> event2(expected_event2, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock2);
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event2, event2.get());
EXPECT_CALL(mock2, sd_event_unref(expected_event2))
.WillOnce(testing::Return(nullptr));
event2 = std::move(event);
+ EXPECT_FALSE(event);
+ EXPECT_EQ(nullptr, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -159,6 +190,9 @@
EXPECT_EQ(expected_event2, event2.get());
{
SdRef<sd_event> event_mover(std::move(event2));
+ EXPECT_FALSE(event2);
+ EXPECT_EQ(nullptr, event2.get());
+ EXPECT_TRUE(event_mover);
EXPECT_EQ(expected_event2, event_mover.get());
EXPECT_CALL(mock2, sd_event_unref(expected_event2))
@@ -166,6 +200,9 @@
}
event2 = std::move(event);
+ EXPECT_FALSE(event);
+ EXPECT_EQ(nullptr, event.get());
+ EXPECT_TRUE(event2);
EXPECT_EQ(expected_event, event2.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
@@ -176,8 +213,12 @@
{
SdRef<sd_event> event(expected_event, &SdEvent::sd_event_ref,
&SdEvent::sd_event_unref, std::false_type(), &mock);
+ EXPECT_TRUE(event);
EXPECT_EQ(expected_event, event.get());
+
event = std::move(event);
+ EXPECT_TRUE(event);
+ EXPECT_EQ(expected_event, event.get());
EXPECT_CALL(mock, sd_event_unref(expected_event))
.WillOnce(testing::Return(nullptr));
}