-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncCommandClass.h
More file actions
42 lines (38 loc) · 1.55 KB
/
AsyncCommandClass.h
File metadata and controls
42 lines (38 loc) · 1.55 KB
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
#pragma once
#include<future>
#include<memory>
#include<functional>
/*!
This templated class takes 2 template parameters. One is the command type and other one is Response type
Both these types can be a user defined types or can be inbult types. The thread which is sending the command
should first get the future by calling getFuture() method. The receiver thread can access the command
by accessing the Command Variable. The receiver thread can return the result of command execution by calling
the operator(). The sending thread can then get this result from the future which it acquired earlier
*/
template<typename CommandType,typename ResponseType>
class AsyncCommandClass{
public:
/*!
The constructor takes command and intializes its internal command object.
sender thread creates this object by calling this constructor with command
it wants to send to the receiver thread.
*/
explicit AsyncCommandClass(CommandType&& cmd) :
ComputeResult_{ [](ResponseType&& response) {return response;} }, Command{ cmd } {
}
/*!
command sender thread should call this before sending the command
*/
std::future<ResponseType> getFuture() {
return ComputeResult_.get_future();
}
/*!
command receiver thread should call this to return the result of command execution back to command sender
*/
void sendResponse(ResponseType&& Response) {
ComputeResult_(std::move(Response));
}
CommandType Command; //! this represents actual command
private:
std::packaged_task<ResponseType(ResponseType&&)> ComputeResult_;
};