source/*: Make updating callbacks possible
Sometimes callers want to be able to update the callback used when the
source is acted upon. This is needed for updating references stored in
the callback.
Tested:
Run through the unit test suite.
Change-Id: I78bda32569287964bfc9d49501869d3a2b497f3d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/sdeventplus/source/child.cpp b/src/sdeventplus/source/child.cpp
index cda067b..b549473 100644
--- a/src/sdeventplus/source/child.cpp
+++ b/src/sdeventplus/source/child.cpp
@@ -14,6 +14,11 @@
{
}
+void Child::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
pid_t Child::get_pid() const
{
pid_t pid;
diff --git a/src/sdeventplus/source/child.hpp b/src/sdeventplus/source/child.hpp
index 3a9857f..ed147d6 100644
--- a/src/sdeventplus/source/child.hpp
+++ b/src/sdeventplus/source/child.hpp
@@ -31,6 +31,12 @@
*/
Child(const Event& event, pid_t pid, int options, Callback&& callback);
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on event dispatch
+ */
+ void set_callback(Callback&& callback);
+
/** @brief Gets the pid of the child process being watched
*
* @return The child pid
diff --git a/src/sdeventplus/source/event.cpp b/src/sdeventplus/source/event.cpp
index 12d4d2b..c73f16c 100644
--- a/src/sdeventplus/source/event.cpp
+++ b/src/sdeventplus/source/event.cpp
@@ -7,6 +7,11 @@
namespace source
{
+void EventBase::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
EventBase::EventBase(const char* name, CreateFunc create, const Event& event,
Callback&& callback) :
Base(event, create_source(name, create, event), std::false_type()),
diff --git a/src/sdeventplus/source/event.hpp b/src/sdeventplus/source/event.hpp
index cc23209..c384364 100644
--- a/src/sdeventplus/source/event.hpp
+++ b/src/sdeventplus/source/event.hpp
@@ -21,6 +21,12 @@
public:
using Callback = std::function<void(EventBase& source)>;
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on event dispatch
+ */
+ void set_callback(Callback&& callback);
+
protected:
using CreateFunc = decltype(&internal::SdEvent::sd_event_add_exit);
diff --git a/src/sdeventplus/source/io.cpp b/src/sdeventplus/source/io.cpp
index a325f91..49583d0 100644
--- a/src/sdeventplus/source/io.cpp
+++ b/src/sdeventplus/source/io.cpp
@@ -13,6 +13,11 @@
{
}
+void IO::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
int IO::get_fd() const
{
int r = event.getSdEvent()->sd_event_source_get_io_fd(source.get());
diff --git a/src/sdeventplus/source/io.hpp b/src/sdeventplus/source/io.hpp
index 61e9681..b35500e 100644
--- a/src/sdeventplus/source/io.hpp
+++ b/src/sdeventplus/source/io.hpp
@@ -32,6 +32,12 @@
*/
IO(const Event& event, int fd, uint32_t events, Callback&& callback);
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on event dispatch
+ */
+ void set_callback(Callback&& callback);
+
/** @brief Gets the file descriptor tied to the source
*
* @throws SdEventError for underlying sd_event errors
diff --git a/src/sdeventplus/source/signal.cpp b/src/sdeventplus/source/signal.cpp
index 1df3a29..98b1a05 100644
--- a/src/sdeventplus/source/signal.cpp
+++ b/src/sdeventplus/source/signal.cpp
@@ -14,6 +14,11 @@
{
}
+void Signal::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
int Signal::get_signal() const
{
int r = event.getSdEvent()->sd_event_source_get_signal(source.get());
diff --git a/src/sdeventplus/source/signal.hpp b/src/sdeventplus/source/signal.hpp
index d5d9b5a..f62db44 100644
--- a/src/sdeventplus/source/signal.hpp
+++ b/src/sdeventplus/source/signal.hpp
@@ -33,6 +33,12 @@
*/
Signal(const Event& event, int sig, Callback&& callback);
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on event dispatch
+ */
+ void set_callback(Callback&& callback);
+
/** @brief Gets the signum watched by the source
*
* @throws SdEventError for underlying sd_event errors
diff --git a/src/sdeventplus/source/time.cpp b/src/sdeventplus/source/time.cpp
index 1a24604..bbc3295 100644
--- a/src/sdeventplus/source/time.cpp
+++ b/src/sdeventplus/source/time.cpp
@@ -20,6 +20,12 @@
}
template <ClockId Id>
+void Time<Id>::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
+template <ClockId Id>
typename Time<Id>::TimePoint Time<Id>::get_time() const
{
uint64_t usec;
diff --git a/src/sdeventplus/source/time.hpp b/src/sdeventplus/source/time.hpp
index c43de7a..ef79803 100644
--- a/src/sdeventplus/source/time.hpp
+++ b/src/sdeventplus/source/time.hpp
@@ -40,6 +40,12 @@
Time(const Event& event, TimePoint time, Accuracy accuracy,
Callback&& callback);
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on event dispatch
+ */
+ void set_callback(Callback&& callback);
+
/** @brief Gets the absolute time when the time source expires
*
* @throws SdEventError for underlying sd_event errors
diff --git a/test/source/child.cpp b/test/source/child.cpp
index e1e1322..ab173b1 100644
--- a/test/source/child.cpp
+++ b/test/source/child.cpp
@@ -91,6 +91,10 @@
EXPECT_EQ(1, completions);
EXPECT_EQ(expected_si, return_si);
+ child.set_callback(std::bind([]() {}));
+ EXPECT_EQ(0, handler(nullptr, expected_si, &child));
+ EXPECT_EQ(1, completions);
+
expect_destruct();
}
diff --git a/test/source/event.cpp b/test/source/event.cpp
index 90edaec..e4d7b2a 100644
--- a/test/source/event.cpp
+++ b/test/source/event.cpp
@@ -83,6 +83,10 @@
EXPECT_EQ(0, handler(nullptr, &defer));
EXPECT_EQ(1, completions);
+ defer.set_callback(std::bind([]() {}));
+ EXPECT_EQ(0, handler(nullptr, &defer));
+ EXPECT_EQ(1, completions);
+
expect_destruct();
}
diff --git a/test/source/io.cpp b/test/source/io.cpp
index b680be7..89506b1 100644
--- a/test/source/io.cpp
+++ b/test/source/io.cpp
@@ -92,6 +92,10 @@
EXPECT_EQ(5, return_fd);
EXPECT_EQ(EPOLLIN, return_revents);
+ io.set_callback(std::bind([]() {}));
+ EXPECT_EQ(0, handler(nullptr, 5, EPOLLIN, &io));
+ EXPECT_EQ(1, completions);
+
expect_destruct();
}
diff --git a/test/source/signal.cpp b/test/source/signal.cpp
index c99aa6b..d3b53a7 100644
--- a/test/source/signal.cpp
+++ b/test/source/signal.cpp
@@ -92,6 +92,10 @@
EXPECT_EQ(1, completions);
EXPECT_EQ(expected_si, return_si);
+ signal.set_callback(std::bind([]() {}));
+ EXPECT_EQ(0, handler(nullptr, expected_si, &signal));
+ EXPECT_EQ(1, completions);
+
expect_destruct();
}
diff --git a/test/source/time.cpp b/test/source/time.cpp
index aa25da0..96a920e 100644
--- a/test/source/time.cpp
+++ b/test/source/time.cpp
@@ -90,6 +90,11 @@
EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
saved_time);
+ time.set_callback(std::bind([]() {}));
+ EXPECT_EQ(0, handler(nullptr, 0, userdata));
+ EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
+ saved_time);
+
expect_time_destroy(expected_event, expected_source);
}