internal/cexec: Refactor using new macro
Change-Id: Ia2ccb11980621fd9d474bea775bab13055e0e67e
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/meson.build b/src/meson.build
index fb47688..e8196cc 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -76,7 +76,6 @@
install_headers(
'sdeventplus/internal/sdevent.hpp',
- 'sdeventplus/internal/utils.hpp',
subdir: 'sdeventplus/internal')
install_headers(
diff --git a/src/sdeventplus/clock.cpp b/src/sdeventplus/clock.cpp
index dc9eafd..ff468ee 100644
--- a/src/sdeventplus/clock.cpp
+++ b/src/sdeventplus/clock.cpp
@@ -1,7 +1,7 @@
#include <sdeventplus/clock.hpp>
#include <sdeventplus/exception.hpp>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <utility>
namespace sdeventplus
@@ -21,9 +21,9 @@
typename Clock<Id>::time_point Clock<Id>::now() const
{
uint64_t now;
- internal::callCheck("sd_event_now", &internal::SdEvent::sd_event_now,
- event.getSdEvent(), event.get(),
- static_cast<clockid_t>(Id), &now);
+ SDEVENTPLUS_CHECK("sd_event_now",
+ event.getSdEvent()->sd_event_now(
+ event.get(), static_cast<clockid_t>(Id), &now));
return time_point(SdEventDuration(now));
}
diff --git a/src/sdeventplus/clock.hpp b/src/sdeventplus/clock.hpp
index 7851a98..a25b6ba 100644
--- a/src/sdeventplus/clock.hpp
+++ b/src/sdeventplus/clock.hpp
@@ -4,7 +4,6 @@
#include <cstdint>
#include <ctime>
#include <sdeventplus/event.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/types.hpp>
#include <type_traits>
diff --git a/src/sdeventplus/event.cpp b/src/sdeventplus/event.cpp
index 93f9afa..56891b5 100644
--- a/src/sdeventplus/event.cpp
+++ b/src/sdeventplus/event.cpp
@@ -1,7 +1,7 @@
#include <functional>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <systemd/sd-event.h>
#include <type_traits>
#include <utility>
@@ -29,16 +29,14 @@
Event Event::get_new(const internal::SdEvent* sdevent)
{
sd_event* event = nullptr;
- internal::callCheck("sd_event_new", &internal::SdEvent::sd_event_new,
- sdevent, &event);
+ SDEVENTPLUS_CHECK("sd_event_new", sdevent->sd_event_new(&event));
return Event(event, std::false_type(), sdevent);
}
Event Event::get_default(const internal::SdEvent* sdevent)
{
sd_event* event = nullptr;
- internal::callCheck("sd_event_default",
- &internal::SdEvent::sd_event_default, sdevent, &event);
+ SDEVENTPLUS_CHECK("sd_event_default", sdevent->sd_event_default(&event));
return Event(event, std::false_type(), sdevent);
}
@@ -54,68 +52,60 @@
int Event::prepare() const
{
- return internal::callCheck("sd_event_prepare",
- &internal::SdEvent::sd_event_prepare, sdevent,
- get());
+ return SDEVENTPLUS_CHECK("sd_event_prepare",
+ sdevent->sd_event_prepare(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;
- return internal::callCheck("sd_event_wait",
- &internal::SdEvent::sd_event_wait, sdevent,
- get(), timeout_usec);
+ return SDEVENTPLUS_CHECK("sd_event_wait",
+ sdevent->sd_event_wait(get(), timeout_usec));
}
int Event::dispatch() const
{
- return internal::callCheck("sd_event_dispatch",
- &internal::SdEvent::sd_event_dispatch, sdevent,
- get());
+ return SDEVENTPLUS_CHECK("sd_event_dispatch",
+ sdevent->sd_event_dispatch(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;
- return internal::callCheck("sd_event_run", &internal::SdEvent::sd_event_run,
- sdevent, get(), timeout_usec);
+ return SDEVENTPLUS_CHECK("sd_event_run",
+ sdevent->sd_event_run(get(), timeout_usec));
}
int Event::loop() const
{
- return internal::callCheck(
- "sd_event_loop", &internal::SdEvent::sd_event_loop, sdevent, get());
+ return SDEVENTPLUS_CHECK("sd_event_loop", sdevent->sd_event_loop(get()));
}
void Event::exit(int code) const
{
- internal::callCheck("sd_event_exit", &internal::SdEvent::sd_event_exit,
- sdevent, get(), code);
+ SDEVENTPLUS_CHECK("sd_event_exit", sdevent->sd_event_exit(get(), code));
}
int Event::get_exit_code() const
{
int code;
- internal::callCheck("sd_event_get_exit_code",
- &internal::SdEvent::sd_event_get_exit_code, sdevent,
- get(), &code);
+ SDEVENTPLUS_CHECK("sd_event_get_exit_code",
+ sdevent->sd_event_get_exit_code(get(), &code));
return code;
}
bool Event::get_watchdog() const
{
- return internal::callCheck("sd_event_get_watchdog",
- &internal::SdEvent::sd_event_get_watchdog,
- sdevent, get());
+ return SDEVENTPLUS_CHECK("sd_event_get_watchdog",
+ sdevent->sd_event_get_watchdog(get()));
}
bool Event::set_watchdog(bool b) const
{
- return internal::callCheck("sd_event_set_watchdog",
- &internal::SdEvent::sd_event_set_watchdog,
- sdevent, get(), b);
+ return SDEVENTPLUS_CHECK("sd_event_set_watchdog",
+ sdevent->sd_event_set_watchdog(get(), b));
}
sd_event* Event::ref(sd_event* const& event, const internal::SdEvent*& sdevent,
diff --git a/src/sdeventplus/event.hpp b/src/sdeventplus/event.hpp
index e17e743..02c1750 100644
--- a/src/sdeventplus/event.hpp
+++ b/src/sdeventplus/event.hpp
@@ -2,7 +2,6 @@
#include <optional>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/types.hpp>
#include <stdplus/handle/copyable.hpp>
#include <systemd/sd-event.h>
diff --git a/src/sdeventplus/internal/cexec.hpp b/src/sdeventplus/internal/cexec.hpp
new file mode 100644
index 0000000..6d835ab
--- /dev/null
+++ b/src/sdeventplus/internal/cexec.hpp
@@ -0,0 +1,6 @@
+#pragma once
+#include <sdeventplus/exception.hpp>
+#include <stdplus/util/cexec.hpp>
+
+#define SDEVENTPLUS_CHECK(msg, expr) \
+ CHECK_RET(expr, [&](int ret) { throw SdEventError(ret, (msg)); })
diff --git a/src/sdeventplus/internal/utils.hpp b/src/sdeventplus/internal/utils.hpp
deleted file mode 100644
index 9b18dee..0000000
--- a/src/sdeventplus/internal/utils.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#pragma once
-
-#include <sdeventplus/exception.hpp>
-#include <stdplus/util/cexec.hpp>
-#include <utility>
-
-namespace sdeventplus
-{
-namespace internal
-{
-
-/** @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 f4ed15b..70cb527 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -1,6 +1,6 @@
#include <functional>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/base.hpp>
#include <sdeventplus/types.hpp>
#include <utility>
@@ -23,27 +23,26 @@
const char* Base::get_description() const
{
const char* description;
- internal::callCheck("sd_event_source_get_description",
- &internal::SdEvent::sd_event_source_get_description,
- event.getSdEvent(), get(), &description);
+ SDEVENTPLUS_CHECK("sd_event_source_get_description",
+ event.getSdEvent()->sd_event_source_get_description(
+ get(), &description));
return description;
}
void Base::set_description(const char* description) const
{
- internal::callCheck("sd_event_source_set_description",
- &internal::SdEvent::sd_event_source_set_description,
- event.getSdEvent(), get(), description);
+ SDEVENTPLUS_CHECK("sd_event_source_set_description",
+ event.getSdEvent()->sd_event_source_set_description(
+ get(), description));
}
void Base::set_prepare(Callback&& callback)
{
try
{
- internal::callCheck("sd_event_source_set_prepare",
- &internal::SdEvent::sd_event_source_set_prepare,
- event.getSdEvent(), get(),
- callback ? prepareCallback : nullptr);
+ SDEVENTPLUS_CHECK("sd_event_source_set_prepare",
+ event.getSdEvent()->sd_event_source_set_prepare(
+ get(), callback ? prepareCallback : nullptr));
get_userdata().prepare = std::move(callback);
}
catch (...)
@@ -55,55 +54,55 @@
bool Base::get_pending() const
{
- return internal::callCheck("sd_event_source_get_pending",
- &internal::SdEvent::sd_event_source_get_pending,
- event.getSdEvent(), get());
+ return SDEVENTPLUS_CHECK(
+ "sd_event_source_get_pending",
+ event.getSdEvent()->sd_event_source_get_pending(get()));
}
int64_t Base::get_priority() const
{
int64_t priority;
- internal::callCheck("sd_event_source_get_priority",
- &internal::SdEvent::sd_event_source_get_priority,
- event.getSdEvent(), get(), &priority);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_priority",
+ event.getSdEvent()->sd_event_source_get_priority(get(), &priority));
return priority;
}
void Base::set_priority(int64_t priority) const
{
- internal::callCheck("sd_event_source_set_priority",
- &internal::SdEvent::sd_event_source_set_priority,
- event.getSdEvent(), get(), priority);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_set_priority",
+ event.getSdEvent()->sd_event_source_set_priority(get(), priority));
}
Enabled Base::get_enabled() const
{
int enabled;
- internal::callCheck("sd_event_source_get_enabled",
- &internal::SdEvent::sd_event_source_get_enabled,
- event.getSdEvent(), get(), &enabled);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_enabled",
+ event.getSdEvent()->sd_event_source_get_enabled(get(), &enabled));
return static_cast<Enabled>(enabled);
}
void Base::set_enabled(Enabled enabled) const
{
- internal::callCheck("sd_event_source_set_enabled",
- &internal::SdEvent::sd_event_source_set_enabled,
- event.getSdEvent(), get(), static_cast<int>(enabled));
+ SDEVENTPLUS_CHECK("sd_event_source_set_enabled",
+ event.getSdEvent()->sd_event_source_set_enabled(
+ get(), static_cast<int>(enabled)));
}
bool Base::get_floating() const
{
- return internal::callCheck("sd_event_source_get_floating",
- &internal::SdEvent::sd_event_source_get_floating,
- event.getSdEvent(), get());
+ return SDEVENTPLUS_CHECK(
+ "sd_event_source_get_floating",
+ event.getSdEvent()->sd_event_source_get_floating(get()));
}
void Base::set_floating(bool b) const
{
- internal::callCheck("sd_event_source_set_floating",
- &internal::SdEvent::sd_event_source_set_floating,
- event.getSdEvent(), get(), static_cast<int>(b));
+ SDEVENTPLUS_CHECK("sd_event_source_set_floating",
+ event.getSdEvent()->sd_event_source_set_floating(
+ get(), static_cast<int>(b)));
}
Base::Base(const Event& event, sd_event_source* source, std::false_type) :
@@ -119,10 +118,9 @@
void Base::set_userdata(std::unique_ptr<detail::BaseData> data) const
{
- internal::callCheck(
- "sd_event_source_set_destroy_callback",
- &internal::SdEvent::sd_event_source_set_destroy_callback,
- event.getSdEvent(), get(), &Base::destroy_userdata);
+ SDEVENTPLUS_CHECK("sd_event_source_set_destroy_callback",
+ event.getSdEvent()->sd_event_source_set_destroy_callback(
+ get(), &Base::destroy_userdata));
event.getSdEvent()->sd_event_source_set_userdata(get(), data.release());
}
diff --git a/src/sdeventplus/source/child.cpp b/src/sdeventplus/source/child.cpp
index 925a223..d89d062 100644
--- a/src/sdeventplus/source/child.cpp
+++ b/src/sdeventplus/source/child.cpp
@@ -1,6 +1,6 @@
#include <memory>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/child.hpp>
#include <sdeventplus/types.hpp>
#include <type_traits>
@@ -31,9 +31,9 @@
pid_t Child::get_pid() const
{
pid_t pid;
- internal::callCheck("sd_event_source_get_child_pid",
- &internal::SdEvent::sd_event_source_get_child_pid,
- event.getSdEvent(), get(), &pid);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_child_pid",
+ event.getSdEvent()->sd_event_source_get_child_pid(get(), &pid));
return pid;
}
@@ -51,10 +51,10 @@
int options)
{
sd_event_source* source;
- internal::callCheck("sd_event_add_child",
- &internal::SdEvent::sd_event_add_child,
- event.getSdEvent(), event.get(), &source, pid, options,
- childCallback, nullptr);
+ SDEVENTPLUS_CHECK(
+ "sd_event_add_child",
+ event.getSdEvent()->sd_event_add_child(
+ event.get(), &source, pid, options, childCallback, nullptr));
return source;
}
diff --git a/src/sdeventplus/source/event.cpp b/src/sdeventplus/source/event.cpp
index eced2ef..a7a49fc 100644
--- a/src/sdeventplus/source/event.cpp
+++ b/src/sdeventplus/source/event.cpp
@@ -1,6 +1,6 @@
#include <memory>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/event.hpp>
#include <sdeventplus/types.hpp>
#include <utility>
@@ -42,8 +42,8 @@
const Event& event)
{
sd_event_source* source;
- internal::callCheck(name, create, event.getSdEvent(), event.get(), &source,
- eventCallback, nullptr);
+ SDEVENTPLUS_CHECK(name, std::invoke(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 3bafd9a..2723ca1 100644
--- a/src/sdeventplus/source/io.cpp
+++ b/src/sdeventplus/source/io.cpp
@@ -1,5 +1,5 @@
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/io.hpp>
#include <sdeventplus/types.hpp>
#include <type_traits>
@@ -28,40 +28,39 @@
int IO::get_fd() const
{
- return internal::callCheck("sd_event_source_get_io_fd",
- &internal::SdEvent::sd_event_source_get_io_fd,
- event.getSdEvent(), get());
+ return SDEVENTPLUS_CHECK(
+ "sd_event_source_get_io_fd",
+ event.getSdEvent()->sd_event_source_get_io_fd(get()));
}
void IO::set_fd(int fd) const
{
- internal::callCheck("sd_event_source_set_io_fd",
- &internal::SdEvent::sd_event_source_set_io_fd,
- event.getSdEvent(), get(), fd);
+ SDEVENTPLUS_CHECK("sd_event_source_set_io_fd",
+ event.getSdEvent()->sd_event_source_set_io_fd(get(), fd));
}
uint32_t IO::get_events() const
{
uint32_t events;
- internal::callCheck("sd_event_source_get_io_events",
- &internal::SdEvent::sd_event_source_get_io_events,
- event.getSdEvent(), get(), &events);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_io_events",
+ event.getSdEvent()->sd_event_source_get_io_events(get(), &events));
return events;
}
void IO::set_events(uint32_t events) const
{
- internal::callCheck("sd_event_source_set_io_events",
- &internal::SdEvent::sd_event_source_set_io_events,
- event.getSdEvent(), get(), events);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_set_io_events",
+ event.getSdEvent()->sd_event_source_set_io_events(get(), events));
}
uint32_t IO::get_revents() const
{
uint32_t revents;
- internal::callCheck("sd_event_source_get_io_revents",
- &internal::SdEvent::sd_event_source_get_io_revents,
- event.getSdEvent(), get(), &revents);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_io_revents",
+ event.getSdEvent()->sd_event_source_get_io_revents(get(), &revents));
return revents;
}
@@ -78,9 +77,9 @@
sd_event_source* IO::create_source(const Event& event, int fd, uint32_t events)
{
sd_event_source* source;
- internal::callCheck("sd_event_add_io", &internal::SdEvent::sd_event_add_io,
- event.getSdEvent(), event.get(), &source, fd, events,
- ioCallback, nullptr);
+ SDEVENTPLUS_CHECK("sd_event_add_io", event.getSdEvent()->sd_event_add_io(
+ event.get(), &source, fd, events,
+ ioCallback, nullptr));
return source;
}
diff --git a/src/sdeventplus/source/signal.cpp b/src/sdeventplus/source/signal.cpp
index 97ae171..283b86f 100644
--- a/src/sdeventplus/source/signal.cpp
+++ b/src/sdeventplus/source/signal.cpp
@@ -1,6 +1,6 @@
#include <memory>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/signal.hpp>
#include <sdeventplus/types.hpp>
#include <type_traits>
@@ -30,9 +30,9 @@
int Signal::get_signal() const
{
- return internal::callCheck("sd_event_source_get_signal",
- &internal::SdEvent::sd_event_source_get_signal,
- event.getSdEvent(), get());
+ return SDEVENTPLUS_CHECK(
+ "sd_event_source_get_signal",
+ event.getSdEvent()->sd_event_source_get_signal(get()));
}
detail::SignalData& Signal::get_userdata() const
@@ -48,9 +48,9 @@
sd_event_source* Signal::create_source(const Event& event, int sig)
{
sd_event_source* source;
- internal::callCheck(
- "sd_event_add_signal", &internal::SdEvent::sd_event_add_signal,
- event.getSdEvent(), event.get(), &source, sig, signalCallback, nullptr);
+ SDEVENTPLUS_CHECK("sd_event_add_signal",
+ event.getSdEvent()->sd_event_add_signal(
+ event.get(), &source, sig, signalCallback, nullptr));
return source;
}
diff --git a/src/sdeventplus/source/time.cpp b/src/sdeventplus/source/time.cpp
index 03c4665..9eb7a7a 100644
--- a/src/sdeventplus/source/time.cpp
+++ b/src/sdeventplus/source/time.cpp
@@ -1,7 +1,7 @@
#include <memory>
#include <sdeventplus/clock.hpp>
+#include <sdeventplus/internal/cexec.hpp>
#include <sdeventplus/internal/sdevent.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/time.hpp>
#include <sdeventplus/types.hpp>
#include <type_traits>
@@ -37,38 +37,37 @@
typename Time<Id>::TimePoint Time<Id>::get_time() const
{
uint64_t usec;
- internal::callCheck("sd_event_source_get_time",
- &internal::SdEvent::sd_event_source_get_time,
- event.getSdEvent(), get(), &usec);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_time",
+ event.getSdEvent()->sd_event_source_get_time(get(), &usec));
return Time<Id>::TimePoint(SdEventDuration(usec));
}
template <ClockId Id>
void Time<Id>::set_time(TimePoint time) const
{
- internal::callCheck("sd_event_source_set_time",
- &internal::SdEvent::sd_event_source_set_time,
- event.getSdEvent(), get(),
- SdEventDuration(time.time_since_epoch()).count());
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_set_time",
+ event.getSdEvent()->sd_event_source_set_time(
+ get(), SdEventDuration(time.time_since_epoch()).count()));
}
template <ClockId Id>
typename Time<Id>::Accuracy Time<Id>::get_accuracy() const
{
uint64_t usec;
- internal::callCheck("sd_event_source_get_time_accuracy",
- &internal::SdEvent::sd_event_source_get_time_accuracy,
- event.getSdEvent(), get(), &usec);
+ SDEVENTPLUS_CHECK(
+ "sd_event_source_get_time_accuracy",
+ event.getSdEvent()->sd_event_source_get_time_accuracy(get(), &usec));
return SdEventDuration(usec);
}
template <ClockId Id>
void Time<Id>::set_accuracy(Accuracy accuracy) const
{
- internal::callCheck("sd_event_source_set_time_accuracy",
- &internal::SdEvent::sd_event_source_set_time_accuracy,
- event.getSdEvent(), get(),
- SdEventDuration(accuracy).count());
+ SDEVENTPLUS_CHECK("sd_event_source_set_time_accuracy",
+ event.getSdEvent()->sd_event_source_set_time_accuracy(
+ get(), SdEventDuration(accuracy).count()));
}
template <ClockId Id>
@@ -88,11 +87,12 @@
Accuracy accuracy)
{
sd_event_source* source;
- 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);
+ SDEVENTPLUS_CHECK("sd_event_add_time",
+ event.getSdEvent()->sd_event_add_time(
+ event.get(), &source, static_cast<clockid_t>(Id),
+ SdEventDuration(time.time_since_epoch()).count(),
+ SdEventDuration(accuracy).count(), timeCallback,
+ nullptr));
return source;
}
diff --git a/src/sdeventplus/source/time.hpp b/src/sdeventplus/source/time.hpp
index f7670b5..e606411 100644
--- a/src/sdeventplus/source/time.hpp
+++ b/src/sdeventplus/source/time.hpp
@@ -3,7 +3,6 @@
#include <cstdint>
#include <function2/function2.hpp>
#include <sdeventplus/clock.hpp>
-#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/base.hpp>
#include <sdeventplus/types.hpp>
#include <systemd/sd-event.h>