source/base: Improve some method types
diff --git a/example/heartbeat.cpp b/example/heartbeat.cpp
index e45ae89..1840f60 100644
--- a/example/heartbeat.cpp
+++ b/example/heartbeat.cpp
@@ -30,6 +30,6 @@
};
Time<ClockId::RealTime> time(event, Clock<ClockId::RealTime>(event).now(),
std::chrono::seconds{1}, std::move(hbFunc));
- time.set_enabled(SD_EVENT_ON);
+ time.set_enabled(sdeventplus::source::Enabled::On);
return event.loop();
}
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index d63a03e..9915132 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -14,7 +14,7 @@
{
if (source)
{
- set_enabled(SD_EVENT_OFF);
+ set_enabled(Enabled::Off);
}
}
@@ -67,7 +67,7 @@
return prepare;
}
-int Base::get_pending() const
+bool Base::get_pending() const
{
int r = event.getSdEvent()->sd_event_source_get_pending(source.get());
if (r < 0)
@@ -99,7 +99,7 @@
}
}
-int Base::get_enabled() const
+Enabled Base::get_enabled() const
{
int enabled;
int r =
@@ -108,13 +108,13 @@
{
throw SdEventError(-r, "sd_event_source_get_enabled");
}
- return enabled;
+ return static_cast<Enabled>(enabled);
}
-void Base::set_enabled(int enabled) const
+void Base::set_enabled(Enabled enabled) const
{
- int r =
- event.getSdEvent()->sd_event_source_set_enabled(source.get(), enabled);
+ int r = event.getSdEvent()->sd_event_source_set_enabled(
+ source.get(), static_cast<int>(enabled));
if (r < 0)
{
throw SdEventError(-r, "sd_event_source_set_enabled");
@@ -152,7 +152,7 @@
// after it gets deleted in the move
if (source)
{
- set_enabled(SD_EVENT_OFF);
+ set_enabled(Enabled::Off);
}
event = std::move(other.event);
diff --git a/src/sdeventplus/source/base.hpp b/src/sdeventplus/source/base.hpp
index 5a5d12b..dd2a715 100644
--- a/src/sdeventplus/source/base.hpp
+++ b/src/sdeventplus/source/base.hpp
@@ -15,6 +15,17 @@
namespace source
{
+/** @class Enabled
+ * @brief Mapping of sdeventplus source enable values to the sd-event
+ * equivalent
+ */
+enum class Enabled
+{
+ Off = SD_EVENT_OFF,
+ On = SD_EVENT_ON,
+ OneShot = SD_EVENT_ONESHOT,
+};
+
/** @class Base
* @brief The base class for all sources implementing common source methods
* Not instantiated directly by end users
@@ -67,7 +78,7 @@
* @return 'true' if the source has pending events
* 'false' otherwise
*/
- int get_pending() const;
+ bool get_pending() const;
/** @brief Gets the priority of the source relative to other sources
* The lower the priority the more important the source
@@ -90,14 +101,14 @@
* @throws SdEventError for underlying sd_event errors
* @return The enabled status of the source
*/
- int get_enabled() const;
+ Enabled get_enabled() const;
/** @brief Sets the enablement value of the source
*
* @param[in] enabled - The new state of the source
* @throws SdEventError for underlying sd_event errors
*/
- void set_enabled(int enabled) const;
+ void set_enabled(Enabled enabled) const;
protected:
Event event;
diff --git a/test/source/base.cpp b/test/source/base.cpp
index bc0eec0..df855c1 100644
--- a/test/source/base.cpp
+++ b/test/source/base.cpp
@@ -355,10 +355,10 @@
{
EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
.WillOnce(Return(0));
- EXPECT_EQ(0, base->get_pending());
+ EXPECT_FALSE(base->get_pending());
EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
.WillOnce(Return(4));
- EXPECT_EQ(4, base->get_pending());
+ EXPECT_TRUE(base->get_pending());
}
TEST_F(BaseMethodTest, GetPendingError)
@@ -400,7 +400,7 @@
{
EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
.WillOnce(DoAll(SetArgPointee<1>(SD_EVENT_ON), Return(0)));
- EXPECT_EQ(SD_EVENT_ON, base->get_enabled());
+ EXPECT_EQ(Enabled::On, base->get_enabled());
}
TEST_F(BaseMethodTest, GetEnabledError)
@@ -414,14 +414,15 @@
{
EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
.WillOnce(Return(0));
- base->set_enabled(SD_EVENT_ON);
+ base->set_enabled(Enabled::On);
}
TEST_F(BaseMethodTest, SetEnabledError)
{
- EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
+ EXPECT_CALL(mock,
+ sd_event_source_set_enabled(expected_source, SD_EVENT_ONESHOT))
.WillOnce(Return(-EINVAL));
- EXPECT_THROW(base->set_enabled(SD_EVENT_ON), SdEventError);
+ EXPECT_THROW(base->set_enabled(Enabled::OneShot), SdEventError);
}
} // namespace