1 From 5ee51d38575f289c2bf37ed817ef680ed47bb320 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Jos=C3=A9=20Bollo?= <jose.bollo@iot.bzh>
3 Date: Fri, 1 Feb 2019 15:37:44 +0100
4 Subject: [PATCH 12/14] Avoid casting from "const T&" to "void*"
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Latest version of g++ refuse the cast
11 reinterpret_cast<void (Service::*)(void*)>(serviceFunction)
13 I made no investigation to know if the problem
14 is coming from the const or not.
16 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
18 src/server/main/include/service-thread.h | 42 ++++++++++--------------
19 1 file changed, 18 insertions(+), 24 deletions(-)
21 diff --git a/src/server/main/include/service-thread.h b/src/server/main/include/service-thread.h
22 index 964d168..61fdda8 100644
23 --- a/src/server/main/include/service-thread.h
24 +++ b/src/server/main/include/service-thread.h
25 @@ -94,7 +94,7 @@ public:
27 while (!m_eventQueue.empty()){
28 auto front = m_eventQueue.front();
29 - delete front.eventPtr;
34 @@ -104,34 +104,28 @@ public:
36 void (Service::*serviceFunction)(const T &))
38 - EventDescription description;
39 - description.serviceFunctionPtr =
40 - reinterpret_cast<void (Service::*)(void*)>(serviceFunction);
41 - description.servicePtr = servicePtr;
42 - description.eventFunctionPtr = &ServiceThread::EventCall<T>;
43 - description.eventPtr = new T(event);
44 + EventCallerBase *ec = new EventCaller<T>(event, servicePtr, serviceFunction);
46 std::lock_guard<std::mutex> lock(m_eventQueueMutex);
47 - m_eventQueue.push(description);
48 + m_eventQueue.push(ec);
50 m_waitCondition.notify_one();
55 - struct EventDescription {
56 - void (Service::*serviceFunctionPtr)(void *);
57 - Service *servicePtr;
58 - void (ServiceThread::*eventFunctionPtr)(const EventDescription &event);
59 - GenericEvent* eventPtr;
60 + struct EventCallerBase {
61 + virtual void fire() = 0;
62 + virtual ~EventCallerBase() {}
66 - void EventCall(const EventDescription &desc) {
67 - auto fun = reinterpret_cast<void (Service::*)(const T&)>(desc.serviceFunctionPtr);
68 - const T& eventLocale = *(static_cast<T*>(desc.eventPtr));
69 - (desc.servicePtr->*fun)(eventLocale);
71 + struct EventCaller : public EventCallerBase {
72 + T *event; Service *target; void (Service::*function)(const T&);
73 + EventCaller(const T &e, Service *c, void (Service::*f)(const T&)) : event(new T(e)), target(c), function(f) {}
74 + ~EventCaller() { delete event; }
75 + void fire() { (target->*function)(*event); }
78 static void ThreadLoopStatic(ServiceThread *ptr) {
80 @@ -139,33 +133,33 @@ protected:
84 - EventDescription description = {NULL, NULL, NULL, NULL};
85 + EventCallerBase *ec = NULL;
87 std::unique_lock<std::mutex> ulock(m_eventQueueMutex);
90 if (!m_eventQueue.empty()) {
91 - description = m_eventQueue.front();
92 + ec = m_eventQueue.front();
95 m_waitCondition.wait(ulock);
99 - if (description.eventPtr != NULL) {
101 UNHANDLED_EXCEPTION_HANDLER_BEGIN
103 - (this->*description.eventFunctionPtr)(description);
104 - delete description.eventPtr;
107 UNHANDLED_EXCEPTION_HANDLER_END
113 std::thread m_thread;
114 std::mutex m_eventQueueMutex;
115 - std::queue<EventDescription> m_eventQueue;
116 + std::queue<EventCallerBase*> m_eventQueue;
117 std::condition_variable m_waitCondition;