source/event: Implement event_handler style sources
diff --git a/.gitignore b/.gitignore
index 11b0674..7d7cad2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,4 +49,5 @@
/test/internal_sdref
/test/internal_utils
/test/source_base
+/test/source_event
/test/source_time
diff --git a/src/Makefile.am b/src/Makefile.am
index d4e7906..130c8e5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,6 +24,9 @@
nobase_include_HEADERS += sdeventplus/source/base.hpp
libsdeventplus_la_SOURCES += sdeventplus/source/base.cpp
+nobase_include_HEADERS += sdeventplus/source/event.hpp
+libsdeventplus_la_SOURCES += sdeventplus/source/event.cpp
+
nobase_include_HEADERS += sdeventplus/source/time.hpp
libsdeventplus_la_SOURCES += sdeventplus/source/time.cpp
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index 9a6ec07..a24c73c 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -22,6 +22,15 @@
uint64_t accuracy,
sd_event_time_handler_t callback,
void* userdata) const = 0;
+ virtual int sd_event_add_defer(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const = 0;
+ virtual int sd_event_add_post(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const = 0;
+ virtual int sd_event_add_exit(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const = 0;
virtual int sd_event_prepare(sd_event* event) const = 0;
virtual int sd_event_wait(sd_event* event, uint64_t usec) const = 0;
@@ -105,6 +114,27 @@
callback, userdata);
}
+ int sd_event_add_defer(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const override
+ {
+ return ::sd_event_add_defer(event, source, callback, userdata);
+ }
+
+ int sd_event_add_post(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const override
+ {
+ return ::sd_event_add_post(event, source, callback, userdata);
+ }
+
+ int sd_event_add_exit(sd_event* event, sd_event_source** source,
+ sd_event_handler_t callback,
+ void* userdata) const override
+ {
+ return ::sd_event_add_exit(event, source, callback, userdata);
+ }
+
int sd_event_prepare(sd_event* event) const override
{
return ::sd_event_prepare(event);
diff --git a/src/sdeventplus/source/event.cpp b/src/sdeventplus/source/event.cpp
new file mode 100644
index 0000000..694c50a
--- /dev/null
+++ b/src/sdeventplus/source/event.cpp
@@ -0,0 +1,60 @@
+#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/source/event.hpp>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+Event::Event(const char* name, CreateFunc create,
+ const sdeventplus::Event& event, Callback&& callback) :
+ Base(event, create_source(name, create, event), std::false_type()),
+ callback(std::move(callback))
+{
+}
+
+const Event::Callback& Event::get_callback() const
+{
+ return callback;
+}
+
+sd_event_source* Event::create_source(const char* name, CreateFunc create,
+ const sdeventplus::Event& event)
+{
+ sd_event_source* source;
+ int r = (event.getSdEvent()->*create)(event.get(), &source, eventCallback,
+ nullptr);
+ if (r < 0)
+ {
+ throw SdEventError(-r, name);
+ }
+ return source;
+}
+
+int Event::eventCallback(sd_event_source* source, void* userdata)
+{
+ return sourceCallback<Callback, Event, &Event::get_callback>(
+ "eventCallback", source, userdata);
+}
+
+Defer::Defer(const sdeventplus::Event& event, Callback&& callback) :
+ Event("sd_event_add_defer", &internal::SdEvent::sd_event_add_defer, event,
+ std::move(callback))
+{
+}
+
+Post::Post(const sdeventplus::Event& event, Callback&& callback) :
+ Event("sd_event_add_post", &internal::SdEvent::sd_event_add_post, event,
+ std::move(callback))
+{
+}
+
+Exit::Exit(const sdeventplus::Event& event, Callback&& callback) :
+ Event("sd_event_add_exit", &internal::SdEvent::sd_event_add_exit, event,
+ std::move(callback))
+{
+}
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/source/event.hpp b/src/sdeventplus/source/event.hpp
new file mode 100644
index 0000000..6375d7c
--- /dev/null
+++ b/src/sdeventplus/source/event.hpp
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <functional>
+#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/source/base.hpp>
+#include <systemd/sd-event.h>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+class Event : public Base
+{
+ public:
+ using Callback = std::function<void(Event& source)>;
+
+ protected:
+ using CreateFunc = decltype(&internal::SdEvent::sd_event_add_exit);
+
+ Event(const char* name, CreateFunc create, const sdeventplus::Event& event,
+ Callback&& callback);
+
+ private:
+ Callback callback;
+
+ const Callback& get_callback() const;
+
+ static sd_event_source* create_source(const char* name, CreateFunc create,
+ const sdeventplus::Event& event);
+
+ static int eventCallback(sd_event_source* source, void* userdata);
+};
+
+class Defer : public Event
+{
+ public:
+ Defer(const sdeventplus::Event& event, Callback&& Callback);
+};
+
+class Post : public Event
+{
+ public:
+ Post(const sdeventplus::Event& event, Callback&& callback);
+};
+
+class Exit : public Event
+{
+ public:
+ Exit(const sdeventplus::Event& event, Callback&& callback);
+};
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index 1e90bc7..ce386d6 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -20,6 +20,12 @@
MOCK_CONST_METHOD7(sd_event_add_time,
int(sd_event*, sd_event_source**, clockid_t, uint64_t,
uint64_t, sd_event_time_handler_t, void*));
+ MOCK_CONST_METHOD4(sd_event_add_defer, int(sd_event*, sd_event_source**,
+ sd_event_handler_t, void*));
+ MOCK_CONST_METHOD4(sd_event_add_post, int(sd_event*, sd_event_source**,
+ sd_event_handler_t, void*));
+ MOCK_CONST_METHOD4(sd_event_add_exit, int(sd_event*, sd_event_source**,
+ sd_event_handler_t, void*));
MOCK_CONST_METHOD1(sd_event_prepare, int(sd_event*));
MOCK_CONST_METHOD2(sd_event_wait, int(sd_event*, uint64_t));
diff --git a/test/Makefile.am b/test/Makefile.am
index 8fc2beb..49c7402 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -37,6 +37,11 @@
source_base_CPPFLAGS = $(gtest_cppflags)
source_base_LDADD = $(gtest_ldadd)
+check_PROGRAMS += source_event
+source_event_SOURCES = source/event.cpp
+source_event_CPPFLAGS = $(gtest_cppflags)
+source_event_LDADD = $(gtest_ldadd)
+
check_PROGRAMS += source_time
source_time_SOURCES = source/time.cpp
source_time_CPPFLAGS = $(gtest_cppflags)
diff --git a/test/source/event.cpp b/test/source/event.cpp
new file mode 100644
index 0000000..5f19e81
--- /dev/null
+++ b/test/source/event.cpp
@@ -0,0 +1,152 @@
+#include <cerrno>
+#include <functional>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <memory>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/exception.hpp>
+#include <sdeventplus/source/event.hpp>
+#include <sdeventplus/test/sdevent.hpp>
+#include <systemd/sd-event.h>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+namespace
+{
+
+using testing::DoAll;
+using testing::Return;
+using testing::SaveArg;
+using testing::SetArgPointee;
+
+using UniqueEvent = std::unique_ptr<sdeventplus::Event,
+ std::function<void(sdeventplus::Event*)>>;
+
+class EventTest : public testing::Test
+{
+ protected:
+ testing::StrictMock<test::SdEventMock> mock;
+ sd_event_source* const expected_source =
+ reinterpret_cast<sd_event_source*>(1234);
+ sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
+ UniqueEvent event = make_event(expected_event);
+
+ UniqueEvent make_event(sd_event* event)
+ {
+ auto deleter = [this, event](sdeventplus::Event* e) {
+ EXPECT_CALL(this->mock, sd_event_unref(event))
+ .WillOnce(Return(nullptr));
+ delete e;
+ };
+ return UniqueEvent(
+ new sdeventplus::Event(event, std::false_type(), &mock), deleter);
+ }
+
+ void expect_destruct()
+ {
+ {
+ testing::InSequence sequence;
+ EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
+ SD_EVENT_OFF))
+ .WillOnce(Return(0));
+ 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(EventTest, DeferConstruct)
+{
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ void* userdata;
+ EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
+ .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+ sd_event_handler_t handler;
+ EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
+ nullptr))
+ .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
+ Return(0)));
+ int completions = 0;
+ Event::Callback callback = [&completions](Event&) { completions++; };
+ Defer defer(*event, std::move(callback));
+ EXPECT_EQ(&defer, userdata);
+ EXPECT_FALSE(callback);
+ EXPECT_EQ(0, completions);
+
+ EXPECT_EQ(0, handler(nullptr, &defer));
+ EXPECT_EQ(1, completions);
+
+ expect_destruct();
+}
+
+TEST_F(EventTest, PostConstruct)
+{
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ void* userdata;
+ EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
+ .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+ sd_event_handler_t handler;
+ EXPECT_CALL(mock, sd_event_add_post(expected_event, testing::_, testing::_,
+ nullptr))
+ .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
+ Return(0)));
+ int completions = 0;
+ Event::Callback callback = [&completions](Event&) { completions++; };
+ Post post(*event, std::move(callback));
+ EXPECT_EQ(&post, userdata);
+ EXPECT_FALSE(callback);
+ EXPECT_EQ(0, completions);
+
+ EXPECT_EQ(0, handler(nullptr, &post));
+ EXPECT_EQ(1, completions);
+
+ expect_destruct();
+}
+
+TEST_F(EventTest, ExitConstruct)
+{
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ void* userdata;
+ EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
+ .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+ sd_event_handler_t handler;
+ EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_,
+ nullptr))
+ .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
+ Return(0)));
+ int completions = 0;
+ Event::Callback callback = [&completions](Event&) { completions++; };
+ Exit exit(*event, std::move(callback));
+ EXPECT_EQ(&exit, userdata);
+ EXPECT_FALSE(callback);
+ EXPECT_EQ(0, completions);
+
+ EXPECT_EQ(0, handler(nullptr, &exit));
+ EXPECT_EQ(1, completions);
+
+ expect_destruct();
+}
+
+TEST_F(EventTest, ConstructFailure)
+{
+ EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
+ nullptr))
+ .WillOnce(Return(-EINVAL));
+ int completions = 0;
+ Event::Callback callback = [&completions](Event&) { completions++; };
+ EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError);
+ EXPECT_TRUE(callback);
+ EXPECT_EQ(0, completions);
+}
+
+} // namespace
+} // namespace source
+} // namespace sdeventplus