COVISE Core
asyncWait.h
Go to the documentation of this file.
1#ifndef COVISE_UTIL_ASYNC_WAIT
2#define COVISE_UTIL_ASYNC_WAIT
3#include <future>
4#include <functional>
5#include <vector>
6#include <cassert>
7#include <algorithm>
8#include <util/coExport.h>
9namespace covise
10{
11
12 //An AsyncWait executes the Condition in a separate thread.
13 //The main loop has to check for met conditions via handleAsyncWaits.
14 //If the Condition of and AsyncWait is met the Response is executed in the main thread.
15 //AsyncWaits can be chained together via the >> operator eg. waitForA >> waitForB.
16 //In this example waitForB's condition is only checked after waitForB's Response is executed.
18{
19public:
20 virtual ~AsyncWaitInterface() = default;
21 virtual bool operator()() = 0;
22 virtual void wait() = 0;
23 virtual void remove() = 0;
24};
25
26typedef std::vector<std::unique_ptr<AsyncWaitInterface>> AsyncWaits;
28
29//has to be called in main loop to execute the Responses of AsyncWaits that have their condition met
31
32template <typename Param>
34{
35public:
36 typedef std::function<Param(void)> Condition;
37 typedef std::function<bool(const Param &)> Response;
39 : m_response(response), m_condition(condition){}
40 ~AsyncWaitClass() = default;
41 AsyncWaitClass(AsyncWaitClass &&other) = delete;
42 AsyncWaitClass(const AsyncWaitClass &) = delete;
45
46 //wait for the condition to accure
47 void wait()
48 {
49#ifdef _WIN32
50 m_future = std::async(std::launch::async, [this]() {
51 return std::unique_ptr<Param>{new Param{ m_condition() }};
52 });
53#else
54 m_future = std::async(std::launch::async, m_condition);
55#endif
56 }
57
58 bool operator()() override
59 {
60 if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
61 {
62#ifdef _WIN32
63 m_response(*m_future.get());
64#else
65 m_response(m_future.get());
66#endif
67 return true;
68 }
69 return false;
70 }
71
72 template <typename OtherParam>
74 {
75
76 next = &c;
77 Response oldRes = m_response;
78 m_response = [this, oldRes](const Param &r)
79 {
80 if (oldRes(r))
81 {
82 assert(next);
83 next->wait();
84 next = nullptr;
85 return true;
86 }
87 remove();
88 return false;
89 };
90 return *dynamic_cast<AsyncWaitClass<OtherParam> *>(next);
91 }
92
93 void remove() override
94 {
95 if (next)
96 {
97 next->remove();
98 asyncWaits.erase(std::remove_if(asyncWaits.begin(), asyncWaits.end(), [this](const std::unique_ptr<AsyncWaitInterface> &ptr)
99 { return ptr.get() == next; }));
100 next = nullptr;
101 }
102 }
103
104private:
107#ifdef _WIN32
108 std::future<std::unique_ptr<Param>> m_future;
109#else
110 std::future<Param> m_future;
111#endif
113};
114
115template <typename Param>
116AsyncWaitClass<Param> &AsyncWait(std::function<Param(void)> condition, std::function<bool(const Param&)> response)
117{
118 auto r = new AsyncWaitClass<Param>{condition, response};
119 asyncWaits.emplace(asyncWaits.end(), std::unique_ptr<AsyncWaitClass<Param>>{r});
120 return *r;
121}
122} //covise
123
124#endif // COVISE_UTIL_ASYNC_WAIT
#define UTILEXPORT
Definition: coExport.h:206
const GLubyte * c
Definition: khronos-glext.h:9864
GLenum condition
Definition: khronos-glext.h:10218
GLdouble GLdouble GLdouble r
Definition: khronos-glext.h:6457
list of all chemical elements
Definition: coConfig.h:27
UTILEXPORT AsyncWaits asyncWaits
Definition: asyncWait.cpp:7
AsyncWaitClass< Param > & AsyncWait(std::function< Param(void)> condition, std::function< bool(const Param &)> response)
Definition: asyncWait.h:116
void UTILEXPORT handleAsyncWaits()
Definition: asyncWait.cpp:9
std::vector< std::unique_ptr< AsyncWaitInterface > > AsyncWaits
Definition: asyncWait.h:26
Definition: asyncWait.h:18
virtual bool operator()()=0
virtual void remove()=0
virtual ~AsyncWaitInterface()=default
Definition: asyncWait.h:34
AsyncWaitClass(Condition condition, Response response)
Definition: asyncWait.h:38
std::future< Param > m_future
Definition: asyncWait.h:110
AsyncWaitClass & operator=(AsyncWaitClass &&)=delete
AsyncWaitClass(const AsyncWaitClass &)=delete
void remove() override
Definition: asyncWait.h:93
void wait()
Definition: asyncWait.h:47
AsyncWaitClass(AsyncWaitClass &&other)=delete
std::function< Param(void)> Condition
Definition: asyncWait.h:36
Response m_response
Definition: asyncWait.h:105
AsyncWaitClass & operator=(const AsyncWaitClass &)=delete
std::function< bool(const Param &)> Response
Definition: asyncWait.h:37
AsyncWaitClass< OtherParam > & operator>>(AsyncWaitClass< OtherParam > &c)
Definition: asyncWait.h:73
Condition m_condition
Definition: asyncWait.h:106
bool operator()() override
Definition: asyncWait.h:58
AsyncWaitInterface * next
Definition: asyncWait.h:112
Definition: covise_msg.h:137