tree-wide: Use cexec for checking calls
This makes error handling guaranteed and slightly more compact.
Tested:
Ran through unit test suite.
Change-Id: If12cf25302390e5e5238f80b4fdb7ea577b5b5c8
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/sdeventplus/clock.cpp b/src/sdeventplus/clock.cpp
index 37395c4..dc9eafd 100644
--- a/src/sdeventplus/clock.cpp
+++ b/src/sdeventplus/clock.cpp
@@ -1,5 +1,6 @@
#include <sdeventplus/clock.hpp>
#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/sdevent.hpp>
#include <sdeventplus/internal/utils.hpp>
#include <utility>
@@ -20,12 +21,9 @@
typename Clock<Id>::time_point Clock<Id>::now() const
{
uint64_t now;
- int r = event.getSdEvent()->sd_event_now(event.get(),
- static_cast<clockid_t>(Id), &now);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_now");
- }
+ internal::callCheck("sd_event_now", &internal::SdEvent::sd_event_now,
+ event.getSdEvent(), event.get(),
+ static_cast<clockid_t>(Id), &now);
return time_point(SdEventDuration(now));
}
diff --git a/src/sdeventplus/event.cpp b/src/sdeventplus/event.cpp
index 132dc7f..601c0df 100644
--- a/src/sdeventplus/event.cpp
+++ b/src/sdeventplus/event.cpp
@@ -1,7 +1,7 @@
#include <functional>
#include <sdeventplus/event.hpp>
-#include <sdeventplus/exception.hpp>
#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <systemd/sd-event.h>
#include <type_traits>
@@ -23,22 +23,16 @@
Event Event::get_new(const internal::SdEvent* sdevent)
{
sd_event* event = nullptr;
- int r = sdevent->sd_event_new(&event);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_new");
- }
+ internal::callCheck("sd_event_new", &internal::SdEvent::sd_event_new,
+ sdevent, &event);
return Event(event, std::false_type(), sdevent);
}
Event Event::get_default(const internal::SdEvent* sdevent)
{
sd_event* event = nullptr;
- int r = sdevent->sd_event_default(&event);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_default");
- }
+ internal::callCheck("sd_event_default",
+ &internal::SdEvent::sd_event_default, sdevent, &event);
return Event(event, std::false_type(), sdevent);
}
@@ -54,96 +48,68 @@
int Event::prepare() const
{
- int r = sdevent->sd_event_prepare(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_prepare");
- }
- return r;
+ return internal::callCheck("sd_event_prepare",
+ &internal::SdEvent::sd_event_prepare, sdevent,
+ get());
}
int Event::wait(MaybeTimeout timeout) const
{
// An unsigned -1 timeout value means infinity in sd_event
uint64_t timeout_usec = timeout ? timeout->count() : -1;
- int r = sdevent->sd_event_wait(get(), timeout_usec);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_wait");
- }
- return r;
+ return internal::callCheck("sd_event_wait",
+ &internal::SdEvent::sd_event_wait, sdevent,
+ get(), timeout_usec);
}
int Event::dispatch() const
{
- int r = sdevent->sd_event_dispatch(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_dispatch");
- }
- return r;
+ return internal::callCheck("sd_event_dispatch",
+ &internal::SdEvent::sd_event_dispatch, sdevent,
+ get());
}
int Event::run(MaybeTimeout timeout) const
{
// An unsigned -1 timeout value means infinity in sd_event
uint64_t timeout_usec = timeout ? timeout->count() : -1;
- int r = sdevent->sd_event_run(get(), timeout_usec);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_run");
- }
- return r;
+ return internal::callCheck("sd_event_run", &internal::SdEvent::sd_event_run,
+ sdevent, get(), timeout_usec);
}
int Event::loop() const
{
- int r = sdevent->sd_event_loop(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_loop");
- }
- return r;
+ return internal::callCheck(
+ "sd_event_loop", &internal::SdEvent::sd_event_loop, sdevent, get());
}
void Event::exit(int code) const
{
- int r = sdevent->sd_event_exit(get(), code);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_exit");
- }
+ internal::callCheck("sd_event_exit", &internal::SdEvent::sd_event_exit,
+ sdevent, get(), code);
}
int Event::get_exit_code() const
{
int code;
- int r = sdevent->sd_event_get_exit_code(get(), &code);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_get_exit_code");
- }
+ internal::callCheck("sd_event_get_exit_code",
+ &internal::SdEvent::sd_event_get_exit_code, sdevent,
+ get(), &code);
return code;
}
bool Event::get_watchdog() const
{
- int r = sdevent->sd_event_get_watchdog(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_get_watchdog");
- }
- return r;
+ return internal::callCheck("sd_event_get_watchdog",
+ &internal::SdEvent::sd_event_get_watchdog,
+ sdevent, get());
}
bool Event::set_watchdog(bool b) const
{
- int r = sdevent->sd_event_set_watchdog(get(), b);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_set_watchdog");
- }
- return r;
+ return internal::callCheck("sd_event_set_watchdog",
+ &internal::SdEvent::sd_event_set_watchdog,
+ sdevent, get(), b);
}
sd_event* Event::ref(sd_event* const& event, const internal::SdEvent*& sdevent)
diff --git a/src/sdeventplus/internal/utils.hpp b/src/sdeventplus/internal/utils.hpp
index ce9342d..193a52c 100644
--- a/src/sdeventplus/internal/utils.hpp
+++ b/src/sdeventplus/internal/utils.hpp
@@ -6,6 +6,8 @@
#include <exception>
#include <sdeventplus/exception.hpp>
#include <stdexcept>
+#include <stdplus/util/cexec.hpp>
+#include <utility>
namespace sdeventplus
{
@@ -46,5 +48,19 @@
}
}
+/** @brief Constructs an SdEventError for stdplus cexec
+ */
+inline SdEventError makeError(int error, const char* msg)
+{
+ return SdEventError(error, msg);
+}
+
+template <typename... Args>
+inline auto callCheck(const char* msg, Args&&... args)
+{
+ return stdplus::util::callCheckRet<makeError, Args...>(
+ msg, std::forward<Args>(args)...);
+}
+
} // namespace internal
} // namespace sdeventplus
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index 09566f8..450d9a1 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -1,6 +1,6 @@
#include <functional>
-#include <sdeventplus/exception.hpp>
#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/base.hpp>
#include <type_traits>
#include <utility>
@@ -31,35 +31,34 @@
const char* Base::get_description() const
{
const char* description;
- int r = event.getSdEvent()->sd_event_source_get_description(get(),
- &description);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_description");
- }
+ internal::callCheck("sd_event_source_get_description",
+ &internal::SdEvent::sd_event_source_get_description,
+ event.getSdEvent(), get(), &description);
return description;
}
void Base::set_description(const char* description) const
{
- int r =
- event.getSdEvent()->sd_event_source_set_description(get(), description);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_description");
- }
+ internal::callCheck("sd_event_source_set_description",
+ &internal::SdEvent::sd_event_source_set_description,
+ event.getSdEvent(), get(), description);
}
void Base::set_prepare(Callback&& callback)
{
- int r = event.getSdEvent()->sd_event_source_set_prepare(
- get(), callback ? prepareCallback : nullptr);
- if (r < 0)
+ try
+ {
+ internal::callCheck("sd_event_source_set_prepare",
+ &internal::SdEvent::sd_event_source_set_prepare,
+ event.getSdEvent(), get(),
+ callback ? prepareCallback : nullptr);
+ prepare = std::move(callback);
+ }
+ catch (...)
{
prepare = nullptr;
- throw SdEventError(-r, "sd_event_source_set_prepare");
+ throw;
}
- prepare = std::move(callback);
}
const Base::Callback& Base::get_prepare() const
@@ -69,53 +68,41 @@
bool Base::get_pending() const
{
- int r = event.getSdEvent()->sd_event_source_get_pending(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_pending");
- }
- return r;
+ return internal::callCheck("sd_event_source_get_pending",
+ &internal::SdEvent::sd_event_source_get_pending,
+ event.getSdEvent(), get());
}
int64_t Base::get_priority() const
{
int64_t priority;
- int r = event.getSdEvent()->sd_event_source_get_priority(get(), &priority);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_priority");
- }
+ internal::callCheck("sd_event_source_get_priority",
+ &internal::SdEvent::sd_event_source_get_priority,
+ event.getSdEvent(), get(), &priority);
return priority;
}
void Base::set_priority(int64_t priority) const
{
- int r = event.getSdEvent()->sd_event_source_set_priority(get(), priority);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_priority");
- }
+ internal::callCheck("sd_event_source_set_priority",
+ &internal::SdEvent::sd_event_source_set_priority,
+ event.getSdEvent(), get(), priority);
}
Enabled Base::get_enabled() const
{
int enabled;
- int r = event.getSdEvent()->sd_event_source_get_enabled(get(), &enabled);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_enabled");
- }
+ internal::callCheck("sd_event_source_get_enabled",
+ &internal::SdEvent::sd_event_source_get_enabled,
+ event.getSdEvent(), get(), &enabled);
return static_cast<Enabled>(enabled);
}
void Base::set_enabled(Enabled enabled) const
{
- int r = event.getSdEvent()->sd_event_source_set_enabled(
- get(), static_cast<int>(enabled));
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_enabled");
- }
+ internal::callCheck("sd_event_source_set_enabled",
+ &internal::SdEvent::sd_event_source_set_enabled,
+ event.getSdEvent(), get(), static_cast<int>(enabled));
}
Base::Base(const Event& event, sd_event_source* source, std::false_type) :
diff --git a/src/sdeventplus/source/child.cpp b/src/sdeventplus/source/child.cpp
index c7b02d9..e1fe167 100644
--- a/src/sdeventplus/source/child.cpp
+++ b/src/sdeventplus/source/child.cpp
@@ -1,4 +1,5 @@
-#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/child.hpp>
#include <type_traits>
#include <utility>
@@ -22,11 +23,9 @@
pid_t Child::get_pid() const
{
pid_t pid;
- int r = event.getSdEvent()->sd_event_source_get_child_pid(get(), &pid);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_child_pid");
- }
+ internal::callCheck("sd_event_source_get_child_pid",
+ &internal::SdEvent::sd_event_source_get_child_pid,
+ event.getSdEvent(), get(), &pid);
return pid;
}
@@ -39,12 +38,10 @@
int options)
{
sd_event_source* source;
- int r = event.getSdEvent()->sd_event_add_child(
- event.get(), &source, pid, options, childCallback, nullptr);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_add_child");
- }
+ internal::callCheck("sd_event_add_child",
+ &internal::SdEvent::sd_event_add_child,
+ event.getSdEvent(), event.get(), &source, pid, options,
+ childCallback, nullptr);
return source;
}
diff --git a/src/sdeventplus/source/event.cpp b/src/sdeventplus/source/event.cpp
index c73f16c..b64ef64 100644
--- a/src/sdeventplus/source/event.cpp
+++ b/src/sdeventplus/source/event.cpp
@@ -1,4 +1,5 @@
#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/event.hpp>
#include <utility>
@@ -28,12 +29,8 @@
const Event& event)
{
sd_event_source* source;
- int r = (event.getSdEvent()->*create)(event.get(), &source, eventCallback,
- nullptr);
- if (r < 0)
- {
- throw SdEventError(-r, name);
- }
+ internal::callCheck(name, create, event.getSdEvent(), event.get(), &source,
+ eventCallback, nullptr);
return source;
}
diff --git a/src/sdeventplus/source/io.cpp b/src/sdeventplus/source/io.cpp
index d3aa83b..50e0bbf 100644
--- a/src/sdeventplus/source/io.cpp
+++ b/src/sdeventplus/source/io.cpp
@@ -1,3 +1,5 @@
+#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/io.hpp>
#include <type_traits>
#include <utility>
@@ -20,51 +22,40 @@
int IO::get_fd() const
{
- int r = event.getSdEvent()->sd_event_source_get_io_fd(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_io_fd");
- }
- return r;
+ return internal::callCheck("sd_event_source_get_io_fd",
+ &internal::SdEvent::sd_event_source_get_io_fd,
+ event.getSdEvent(), get());
}
void IO::set_fd(int fd) const
{
- int r = event.getSdEvent()->sd_event_source_set_io_fd(get(), fd);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_io_fd");
- }
+ internal::callCheck("sd_event_source_set_io_fd",
+ &internal::SdEvent::sd_event_source_set_io_fd,
+ event.getSdEvent(), get(), fd);
}
uint32_t IO::get_events() const
{
uint32_t events;
- int r = event.getSdEvent()->sd_event_source_get_io_events(get(), &events);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_io_events");
- }
+ internal::callCheck("sd_event_source_get_io_events",
+ &internal::SdEvent::sd_event_source_get_io_events,
+ event.getSdEvent(), get(), &events);
return events;
}
void IO::set_events(uint32_t events) const
{
- int r = event.getSdEvent()->sd_event_source_set_io_events(get(), events);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_io_events");
- }
+ internal::callCheck("sd_event_source_set_io_events",
+ &internal::SdEvent::sd_event_source_set_io_events,
+ event.getSdEvent(), get(), events);
}
uint32_t IO::get_revents() const
{
uint32_t revents;
- int r = event.getSdEvent()->sd_event_source_get_io_revents(get(), &revents);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_io_revents");
- }
+ internal::callCheck("sd_event_source_get_io_revents",
+ &internal::SdEvent::sd_event_source_get_io_revents,
+ event.getSdEvent(), get(), &revents);
return revents;
}
@@ -76,12 +67,9 @@
sd_event_source* IO::create_source(const Event& event, int fd, uint32_t events)
{
sd_event_source* source;
- int r = event.getSdEvent()->sd_event_add_io(event.get(), &source, fd,
- events, ioCallback, nullptr);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_add_io");
- }
+ internal::callCheck("sd_event_add_io", &internal::SdEvent::sd_event_add_io,
+ event.getSdEvent(), event.get(), &source, fd, events,
+ ioCallback, nullptr);
return source;
}
diff --git a/src/sdeventplus/source/signal.cpp b/src/sdeventplus/source/signal.cpp
index b47d78d..dc39023 100644
--- a/src/sdeventplus/source/signal.cpp
+++ b/src/sdeventplus/source/signal.cpp
@@ -1,4 +1,5 @@
-#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/signal.hpp>
#include <type_traits>
#include <utility>
@@ -21,12 +22,9 @@
int Signal::get_signal() const
{
- int r = event.getSdEvent()->sd_event_source_get_signal(get());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_signal");
- }
- return r;
+ return internal::callCheck("sd_event_source_get_signal",
+ &internal::SdEvent::sd_event_source_get_signal,
+ event.getSdEvent(), get());
}
const Signal::Callback& Signal::get_callback() const
@@ -37,12 +35,9 @@
sd_event_source* Signal::create_source(const Event& event, int sig)
{
sd_event_source* source;
- int r = event.getSdEvent()->sd_event_add_signal(event.get(), &source, sig,
- signalCallback, nullptr);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_add_signal");
- }
+ internal::callCheck(
+ "sd_event_add_signal", &internal::SdEvent::sd_event_add_signal,
+ event.getSdEvent(), event.get(), &source, sig, signalCallback, nullptr);
return source;
}
diff --git a/src/sdeventplus/source/time.cpp b/src/sdeventplus/source/time.cpp
index 9d4f9ca..856e65f 100644
--- a/src/sdeventplus/source/time.cpp
+++ b/src/sdeventplus/source/time.cpp
@@ -1,6 +1,6 @@
#include <cstdio>
#include <sdeventplus/clock.hpp>
-#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/sdevent.hpp>
#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/time.hpp>
#include <type_traits>
@@ -29,46 +29,38 @@
typename Time<Id>::TimePoint Time<Id>::get_time() const
{
uint64_t usec;
- int r = event.getSdEvent()->sd_event_source_get_time(get(), &usec);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_time");
- }
+ internal::callCheck("sd_event_source_get_time",
+ &internal::SdEvent::sd_event_source_get_time,
+ event.getSdEvent(), get(), &usec);
return Time<Id>::TimePoint(SdEventDuration(usec));
}
template <ClockId Id>
void Time<Id>::set_time(TimePoint time) const
{
- int r = event.getSdEvent()->sd_event_source_set_time(
- get(), SdEventDuration(time.time_since_epoch()).count());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_time");
- }
+ internal::callCheck("sd_event_source_set_time",
+ &internal::SdEvent::sd_event_source_set_time,
+ event.getSdEvent(), get(),
+ SdEventDuration(time.time_since_epoch()).count());
}
template <ClockId Id>
typename Time<Id>::Accuracy Time<Id>::get_accuracy() const
{
uint64_t usec;
- int r = event.getSdEvent()->sd_event_source_get_time_accuracy(get(), &usec);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_get_time_accuracy");
- }
+ internal::callCheck("sd_event_source_get_time_accuracy",
+ &internal::SdEvent::sd_event_source_get_time_accuracy,
+ event.getSdEvent(), get(), &usec);
return SdEventDuration(usec);
}
template <ClockId Id>
void Time<Id>::set_accuracy(Accuracy accuracy) const
{
- int r = event.getSdEvent()->sd_event_source_set_time_accuracy(
- get(), SdEventDuration(accuracy).count());
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_source_set_time_accuracy");
- }
+ internal::callCheck("sd_event_source_set_time_accuracy",
+ &internal::SdEvent::sd_event_source_set_time_accuracy,
+ event.getSdEvent(), get(),
+ SdEventDuration(accuracy).count());
}
template <ClockId Id>
@@ -82,14 +74,11 @@
Accuracy accuracy)
{
sd_event_source* source;
- int r = event.getSdEvent()->sd_event_add_time(
- event.get(), &source, static_cast<clockid_t>(Id),
+ internal::callCheck(
+ "sd_event_add_time", &internal::SdEvent::sd_event_add_time,
+ event.getSdEvent(), event.get(), &source, static_cast<clockid_t>(Id),
SdEventDuration(time.time_since_epoch()).count(),
SdEventDuration(accuracy).count(), timeCallback, nullptr);
- if (r < 0)
- {
- throw SdEventError(-r, "sd_event_add_time");
- }
return source;
}