Update github.com git:// SRC_URIs
[AGL/meta-agl.git] / meta-app-framework / recipes-security / security-manager / security-manager / 0012-Avoid-casting-from-const-T-to-void.patch
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*"
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Latest version of g++ refuse the cast
10
11      reinterpret_cast<void (Service::*)(void*)>(serviceFunction)
12
13 I made no investigation to know if the problem
14 is coming from the const or not.
15
16 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
17 ---
18  src/server/main/include/service-thread.h | 42 ++++++++++--------------
19  1 file changed, 18 insertions(+), 24 deletions(-)
20
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:
26              Join();
27          while (!m_eventQueue.empty()){
28              auto front = m_eventQueue.front();
29 -            delete front.eventPtr;
30 +            delete front;
31              m_eventQueue.pop();
32          }
33      }
34 @@ -104,34 +104,28 @@ public:
35                 Service *servicePtr,
36                 void (Service::*serviceFunction)(const T &))
37      {
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);
45          {
46              std::lock_guard<std::mutex> lock(m_eventQueueMutex);
47 -            m_eventQueue.push(description);
48 +            m_eventQueue.push(ec);
49          }
50          m_waitCondition.notify_one();
51      }
52  
53  protected:
54  
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() {}
63      };
64  
65      template <class T>
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);
70 -    }
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); }
76 +    };
77  
78      static void ThreadLoopStatic(ServiceThread *ptr) {
79          ptr->ThreadLoop();
80 @@ -139,33 +133,33 @@ protected:
81  
82      void ThreadLoop(){
83          for (;;) {
84 -            EventDescription description = {NULL, NULL, NULL, NULL};
85 +            EventCallerBase *ec = NULL;
86              {
87                  std::unique_lock<std::mutex> ulock(m_eventQueueMutex);
88                  if (m_quit)
89                      return;
90                  if (!m_eventQueue.empty()) {
91 -                    description = m_eventQueue.front();
92 +                    ec = m_eventQueue.front();
93                      m_eventQueue.pop();
94                  } else {
95                      m_waitCondition.wait(ulock);
96                  }
97              }
98  
99 -            if (description.eventPtr != NULL) {
100 +            if (ec != NULL) {
101                  UNHANDLED_EXCEPTION_HANDLER_BEGIN
102                  {
103 -                    (this->*description.eventFunctionPtr)(description);
104 -                    delete description.eventPtr;
105 +                    ec->fire();
106                  }
107                  UNHANDLED_EXCEPTION_HANDLER_END
108 +                delete ec;
109              }
110          }
111      }
112  
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;
118  
119      State m_state;
120 -- 
121 2.21.0
122