summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2018-07-17 14:40:14 -0700
committerWilliam A. Kennington III <wak@google.com>2018-07-17 14:40:14 -0700
commitfeef68f751e09d8d5a4f2bf2f2f4cab27de1b73e (patch)
treeb1610bb958a43d3958b630525120501f768bc9fe /src
parent2d943ead4b1c160a163f284fb5b639f1f076ce49 (diff)
downloadsdeventplus-feef68f751e09d8d5a4f2bf2f2f4cab27de1b73e.tar.gz
sdeventplus-feef68f751e09d8d5a4f2bf2f2f4cab27de1b73e.zip
source/base: Refactor out callback routine
The callback mechanism will be used by other callback methods in the same fashion, so we can template and re-use this functionality
Diffstat (limited to 'src')
-rw-r--r--src/sdeventplus/internal/utils.hpp35
-rw-r--r--src/sdeventplus/source/base.cpp31
-rw-r--r--src/sdeventplus/source/base.hpp2
3 files changed, 39 insertions, 29 deletions
diff --git a/src/sdeventplus/internal/utils.hpp b/src/sdeventplus/internal/utils.hpp
index 253e10d..c0b0e68 100644
--- a/src/sdeventplus/internal/utils.hpp
+++ b/src/sdeventplus/internal/utils.hpp
@@ -1,6 +1,11 @@
#pragma once
+#include <cerrno>
#include <chrono>
+#include <cstdio>
+#include <exception>
+#include <sdeventplus/exception.hpp>
+#include <stdexcept>
namespace sdeventplus
{
@@ -9,4 +14,34 @@ namespace sdeventplus
using SdEventDuration =
std::chrono::duration<uint64_t, std::chrono::microseconds::period>;
+namespace internal
+{
+
+// Helpers for sd_event callbacks to handle exceptions gracefully
+template <typename Func, typename... Args>
+static int performCallback(Func func, Args... args)
+{
+ try
+ {
+ func(args...);
+ return 0;
+ }
+ catch (const std::system_error& e)
+ {
+ fprintf(stderr, "sdeventplus: callback: %s\n", e.what());
+ return -e.code().value();
+ }
+ catch (const std::exception& e)
+ {
+ fprintf(stderr, "sdeventplus: callback: %s\n", e.what());
+ return -ENOSYS;
+ }
+ catch (...)
+ {
+ fprintf(stderr, "sdeventplus: callback: Unknown error\n");
+ return -ENOSYS;
+ }
+}
+
+} // namespace internal
} // namespace sdeventplus
diff --git a/src/sdeventplus/source/base.cpp b/src/sdeventplus/source/base.cpp
index 83908d1..5f202db 100644
--- a/src/sdeventplus/source/base.cpp
+++ b/src/sdeventplus/source/base.cpp
@@ -1,10 +1,10 @@
#include <cerrno>
#include <cstdio>
-#include <exception>
+#include <functional>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/internal/sdevent.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <sdeventplus/source/base.hpp>
-#include <stdexcept>
#include <type_traits>
#include <utility>
@@ -21,30 +21,6 @@ Base::~Base()
}
}
-int Base::prepareCallback()
-{
- try
- {
- prepare(*this);
- return 0;
- }
- catch (const std::system_error& e)
- {
- fprintf(stderr, "sdeventplus: prepareCallback: %s\n", e.what());
- return -e.code().value();
- }
- catch (const std::exception& e)
- {
- fprintf(stderr, "sdeventplus: prepareCallback: %s\n", e.what());
- return -ENOSYS;
- }
- catch (...)
- {
- fprintf(stderr, "sdeventplus: prepareCallback: Unknown error\n");
- return -ENOSYS;
- }
-}
-
sd_event_source* Base::get() const
{
return source.get();
@@ -84,7 +60,8 @@ static int prepare_callback(sd_event_source*, void* userdata)
fprintf(stderr, "sdeventplus: prepare_callback: Missing userdata\n");
return -EINVAL;
}
- return reinterpret_cast<Base*>(userdata)->prepareCallback();
+ Base* base = reinterpret_cast<Base*>(userdata);
+ return internal::performCallback(base->get_prepare(), std::ref(*base));
}
void Base::set_prepare(Callback&& callback)
diff --git a/src/sdeventplus/source/base.hpp b/src/sdeventplus/source/base.hpp
index ed30016..9253b71 100644
--- a/src/sdeventplus/source/base.hpp
+++ b/src/sdeventplus/source/base.hpp
@@ -19,8 +19,6 @@ class Base
virtual ~Base();
- int prepareCallback();
-
sd_event_source* get() const;
const Event& get_event() const;
OpenPOWER on IntegriCloud