source/base: Take an event directly instead of sdevent
Also expose a mechanism for retrieving our internal event.
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index 78ae406..cab2c7a 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -42,11 +42,16 @@
}
}
+const Event& Base::get_event() const
+{
+ return event;
+}
+
const char* Base::get_description() const
{
const char* description;
- int r =
- sdevent->sd_event_source_get_description(source.get(), &description);
+ int r = event.getSdEvent()->sd_event_source_get_description(source.get(),
+ &description);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_get_description");
@@ -56,7 +61,8 @@
void Base::set_description(const char* description) const
{
- int r = sdevent->sd_event_source_set_description(source.get(), description);
+ int r = event.getSdEvent()->sd_event_source_set_description(source.get(),
+ description);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_set_description");
@@ -75,7 +81,7 @@
void Base::set_prepare(Callback&& callback)
{
- int r = sdevent->sd_event_source_set_prepare(
+ int r = event.getSdEvent()->sd_event_source_set_prepare(
source.get(), callback ? prepare_callback : nullptr);
if (r < 0)
{
@@ -87,7 +93,7 @@
int Base::get_pending() const
{
- int r = sdevent->sd_event_source_get_pending(source.get());
+ int r = event.getSdEvent()->sd_event_source_get_pending(source.get());
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_get_pending");
@@ -98,7 +104,8 @@
int64_t Base::get_priority() const
{
int64_t priority;
- int r = sdevent->sd_event_source_get_priority(source.get(), &priority);
+ int r = event.getSdEvent()->sd_event_source_get_priority(source.get(),
+ &priority);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_get_priority");
@@ -108,7 +115,8 @@
void Base::set_priority(int64_t priority) const
{
- int r = sdevent->sd_event_source_set_priority(source.get(), priority);
+ int r = event.getSdEvent()->sd_event_source_set_priority(source.get(),
+ priority);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_set_priority");
@@ -118,7 +126,8 @@
int Base::get_enabled() const
{
int enabled;
- int r = sdevent->sd_event_source_get_enabled(source.get(), &enabled);
+ int r =
+ event.getSdEvent()->sd_event_source_get_enabled(source.get(), &enabled);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_get_enabled");
@@ -128,25 +137,25 @@
void Base::set_enabled(int enabled) const
{
- int r = sdevent->sd_event_source_set_enabled(source.get(), enabled);
+ int r =
+ event.getSdEvent()->sd_event_source_set_enabled(source.get(), enabled);
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_set_enabled");
}
}
-Base::Base(sd_event_source* source, internal::SdEvent* sdevent) :
- sdevent(sdevent), source(source, &internal::SdEvent::sd_event_source_ref,
- &internal::SdEvent::sd_event_source_unref, sdevent)
+Base::Base(const Event& event, sd_event_source* source) :
+ event(event),
+ source(source, &internal::SdEvent::sd_event_source_ref,
+ &internal::SdEvent::sd_event_source_unref, event.getSdEvent())
{
}
-Base::Base(sd_event_source* source, std::false_type,
- internal::SdEvent* sdevent) :
- sdevent(sdevent),
- source(source, &internal::SdEvent::sd_event_source_ref,
- &internal::SdEvent::sd_event_source_unref, std::false_type(),
- sdevent)
+Base::Base(const Event& event, sd_event_source* source, std::false_type) :
+ event(event), source(source, &internal::SdEvent::sd_event_source_ref,
+ &internal::SdEvent::sd_event_source_unref,
+ std::false_type(), event.getSdEvent())
{
}
diff --git a/src/sdeventplus/source/base.hpp b/src/sdeventplus/source/base.hpp
index 3b237db..f8db620 100644
--- a/src/sdeventplus/source/base.hpp
+++ b/src/sdeventplus/source/base.hpp
@@ -2,7 +2,7 @@
#include <cstdint>
#include <functional>
-#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/event.hpp>
#include <sdeventplus/internal/sdref.hpp>
#include <systemd/sd-bus.h>
#include <type_traits>
@@ -27,6 +27,8 @@
int prepareCallback();
+ const Event& get_event() const;
+
const char* get_description() const;
void set_description(const char* description) const;
void set_prepare(Callback&& callback);
@@ -37,14 +39,12 @@
void set_enabled(int enabled) const;
protected:
- const internal::SdEvent* const sdevent;
+ const Event event;
const internal::SdRef<sd_event_source> source;
// Base sources cannot be directly constructed.
- Base(sd_event_source* source,
- internal::SdEvent* sdevent = &internal::sdevent_impl);
- Base(sd_event_source* source, std::false_type,
- internal::SdEvent* sdevent = &internal::sdevent_impl);
+ Base(const Event& event, sd_event_source* source);
+ Base(const Event& event, sd_event_source* source, std::false_type);
private:
Callback prepare;
diff --git a/test/source/base.cpp b/test/source/base.cpp
index 13c534a..3b6a63f 100644
--- a/test/source/base.cpp
+++ b/test/source/base.cpp
@@ -1,12 +1,15 @@
#include <cerrno>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <memory>
+#include <sdeventplus/event.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/internal/sdevent.hpp>
#include <sdeventplus/source/base.hpp>
#include <sdeventplus/test/sdevent.hpp>
#include <string>
#include <system_error>
+#include <systemd/sd-event.h>
#include <type_traits>
namespace sdeventplus
@@ -24,13 +27,11 @@
class BaseImpl : public Base
{
public:
- BaseImpl(sd_event_source* source, internal::SdEvent* sdevent) :
- Base(source, sdevent)
+ BaseImpl(const Event& event, sd_event_source* source) : Base(event, source)
{
}
- BaseImpl(sd_event_source* source, std::false_type,
- internal::SdEvent* sdevent) :
- Base(source, std::false_type(), sdevent)
+ BaseImpl(const Event& event, sd_event_source* source, std::false_type) :
+ Base(event, source, std::false_type())
{
}
};
@@ -39,15 +40,30 @@
{
protected:
testing::StrictMock<test::SdEventMock> mock;
- sd_event_source* const expected_source =
- reinterpret_cast<sd_event_source*>(1234);
+ sd_event_source* expected_source = reinterpret_cast<sd_event_source*>(1234);
+ sd_event* expected_event = reinterpret_cast<sd_event*>(2345);
+ std::unique_ptr<Event> event;
+
+ virtual void SetUp()
+ {
+ event =
+ std::make_unique<Event>(expected_event, std::false_type(), &mock);
+ }
+ virtual void TearDown()
+ {
+ EXPECT_CALL(mock, sd_event_unref(expected_event))
+ .WillOnce(Return(nullptr));
+ event.reset();
+ }
};
TEST_F(BaseTest, NewBaseRef)
{
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
EXPECT_CALL(mock, sd_event_source_ref(expected_source))
.WillOnce(Return(expected_source));
- BaseImpl source(expected_source, &mock);
+ BaseImpl source(*event, expected_source);
{
testing::InSequence seq;
@@ -57,11 +73,14 @@
EXPECT_CALL(mock, sd_event_source_unref(expected_source))
.WillOnce(Return(nullptr));
}
+ EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
}
TEST_F(BaseTest, NewBaseNoRef)
{
- BaseImpl source(expected_source, std::false_type(), &mock);
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ BaseImpl source(*event, expected_source, std::false_type());
{
testing::InSequence seq;
@@ -71,6 +90,7 @@
EXPECT_CALL(mock, sd_event_source_unref(expected_source))
.WillOnce(Return(nullptr));
}
+ EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
}
class BaseMethodTest : public BaseTest
@@ -78,13 +98,16 @@
protected:
std::unique_ptr<BaseImpl> base;
- void SetUp()
+ void SetUp() override
{
- base = std::make_unique<BaseImpl>(expected_source, std::false_type(),
- &mock);
+ BaseTest::SetUp();
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ base = std::make_unique<BaseImpl>(*event, expected_source,
+ std::false_type());
}
- void TearDown()
+ void TearDown() override
{
{
testing::InSequence seq;
@@ -94,9 +117,19 @@
EXPECT_CALL(mock, sd_event_source_unref(expected_source))
.WillOnce(Return(nullptr));
}
+ EXPECT_CALL(mock, sd_event_unref(expected_event))
+ .WillOnce(Return(nullptr));
+ base.reset();
+ BaseTest::TearDown();
}
};
+TEST_F(BaseMethodTest, GetEvent)
+{
+ EXPECT_NE(event.get(), &base->get_event());
+ EXPECT_EQ(event->get(), base->get_event().get());
+}
+
TEST_F(BaseMethodTest, GetDescriptionSuccess)
{
const char* expected = "test_desc";