SPH
MainLoop.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
10 #include <wx/event.h>
11 
12 namespace Sph {
13  class MainLoopEvent;
14 }
15 wxDECLARE_EVENT(MAIN_LOOP_TYPE, Sph::MainLoopEvent);
16 
17 typedef void (wxEvtHandler::*MainLoopEventFunction)(Sph::MainLoopEvent&);
18 #define MainLoopEventHandler(func) wxEVENT_HANDLER_CAST(MainLoopEventFunction, func)
19 
21 
26 class MainLoopEvent : public wxCommandEvent {
27 private:
28  Function<void()> callback;
29 
30 public:
31  MainLoopEvent(const Function<void()>& callback)
32  : wxCommandEvent(MAIN_LOOP_TYPE, 0)
33  , callback(callback) {}
34 
36  : wxCommandEvent(evt)
37  , callback(evt.callback) {}
38 
39  wxEvent* Clone() const {
40  return new MainLoopEvent(*this);
41  }
42 
43  void execute() {
44  callback();
45  }
46 };
47 
53 void executeOnMainThread(const Function<void()>& function);
54 
59 template <typename Type, typename TFunctor>
60 void executeOnMainThread(const SharedPtr<Type>& ptr, TFunctor functor) {
61  executeOnMainThread([weakPtr = WeakPtr<Type>(ptr), f = std::move(functor)] {
62  if (auto ptr = weakPtr.lock()) {
63  f(ptr);
64  }
65  });
66 }
67 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Generic wrappers of lambdas, functors and other callables.
void executeOnMainThread(const Function< void()> &function)
Posts a callback to be executed on main thread.
Definition: MainLoop.cpp:8
wxDECLARE_EVENT(MAIN_LOOP_TYPE, Sph::MainLoopEvent)
void(wxEvtHandler::* MainLoopEventFunction)(Sph::MainLoopEvent &)
Definition: MainLoop.h:17
#define NAMESPACE_SPH_END
Definition: Object.h:12
Custom event holding a callback.
Definition: MainLoop.h:26
MainLoopEvent(const Function< void()> &callback)
Definition: MainLoop.h:31
MainLoopEvent(const MainLoopEvent &evt)
Definition: MainLoop.h:35
wxEvent * Clone() const
Definition: MainLoop.h:39
void execute()
Definition: MainLoop.h:43
Definition: MemoryPool.h:5