pragma once include plugin plugin void _esthread_helper std function v

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
#include "../plugin/plugin.h"
void _esthread_helper(std::function<void()> f) {
try {
f();
} catch (std::exception &e) {
//report_exception(e);
} catch (...) {
//std::exception_ptr pe = std::current_exception();
//std::string type_name = typeName(pe.__cxa_exception_type()); // NOTE: GNU only
//writeLog(MessageType::Error, "got non-STL exception of type %s", type_name);
throw; // that should be cancellation request, it will kill the current thread only.
// Otherwise that's something that should've never left thread body.
}
}
template <typename _Callable, typename... _Args>
std::thread make_esthread(_Callable&& __f, _Args&&... __args) {
void _esthread_helper(std::function<void()> f);
return std::thread(_esthread_helper,
std::bind(std::forward<_Callable>(__f),
std::forward<_Args>(__args)...));
}
using Message = message::WrapperObject;
enum class Destination {
Transport = 0,
Coordinator = 1,
Siblings = 2,
};
class UserPlugin : private Plugin {
public:
using Plugin::Plugin;
using Plugin::writeLog;
virtual ~UserPlugin() = default;
virtual void start() {}
virtual bool isEnabled() { return true; }
virtual void configure(PluginConfiguration const &configuration) = 0;
private:
void stop() noexcept final {
Plugin::stop();
for (auto &&t : threads) {
ping_thread(t);
}
wait();
}
void wait() noexcept {
while (threads.size()) {
threads.back().join();
threads.pop_back();
}
}
void exec() final {
start();
while (!stop_requested) {
sleep(1);
}
}
void parseConfiguration(PluginConfiguration const &configuration) final {
configure(configuration);
}
protected:
using Plugin::uid;
bool sendTo(Destination to, Message &&message) {
sendMessage(std::make_shared<Message>(std::move(message)));
return true;
}
bool running() const noexcept {
return !stop_requested;
}
template <class _Object, typename ...Args>
void start_thread(_Object &obj, void (_Object::*fn)(Args ...), Args &&...args) {
threads.emplace_back(make_esthread(fn, &obj, std::forward<Args>(args)...));
}
template <class _Plugin, typename ...Args>
void start_thread(void (_Plugin::*fn)(Args ...), Args &&...args) {
static_assert(std::is_base_of<Plugin, _Plugin>::value, "start_thread runs member functions only");
assert(dynamic_cast<_Plugin *>(this)); // this *must* be an instance of _Plugin
threads.emplace_back(make_esthread(fn, static_cast<_Plugin *>(this), std::forward<Args>(args)...));
}
private:
std::list<std::thread> threads;
};