utility/timer: Implement callback setting
All of the other classes that take callbacks support having the callback
updated.
Tested:
Builds and passed through the unit test suite.
Change-Id: I0efcd074523fa6ec0cc84c7a930346dd9340f7ec
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/sdeventplus/utility/timer.cpp b/src/sdeventplus/utility/timer.cpp
index 2f28257..242f5d6 100644
--- a/src/sdeventplus/utility/timer.cpp
+++ b/src/sdeventplus/utility/timer.cpp
@@ -55,6 +55,12 @@
}
template <ClockId Id>
+void Timer<Id>::set_callback(Callback&& callback)
+{
+ this->callback = std::move(callback);
+}
+
+template <ClockId Id>
const Event& Timer<Id>::get_event() const
{
return timeSource.get_event();
diff --git a/src/sdeventplus/utility/timer.hpp b/src/sdeventplus/utility/timer.hpp
index e45cf46..5d36458 100644
--- a/src/sdeventplus/utility/timer.hpp
+++ b/src/sdeventplus/utility/timer.hpp
@@ -61,6 +61,12 @@
typename source::Time<Id>::Accuracy accuracy =
std::chrono::milliseconds{1});
+ /** @brief Sets the callback
+ *
+ * @param[in] callback - The function executed on timer elapse
+ */
+ void set_callback(Callback&& callback);
+
/** @brief Gets the associated Event object
*
* @return The Event
diff --git a/test/utility/timer.cpp b/test/utility/timer.cpp
index 6c04e87..f35a0c4 100644
--- a/test/utility/timer.cpp
+++ b/test/utility/timer.cpp
@@ -379,6 +379,13 @@
expectSetTime(new_time + interval);
EXPECT_EQ(0, handler(nullptr, 0, handler_userdata));
EXPECT_EQ(1, called);
+
+ // update the callback and make sure it still works
+ timer->set_callback(std::bind([]() {}));
+ expectNow(new_time);
+ expectSetTime(new_time + interval);
+ EXPECT_EQ(0, handler(nullptr, 0, handler_userdata));
+ EXPECT_EQ(1, called);
}
TEST_F(TimerTest, SetValuesExpiredTimer)