VSM C++ SDK
Vehicle Specific Modules SDK
operation_waiter.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef OPERATION_WAITER_H_
12 #define OPERATION_WAITER_H_
13 
16 
17 namespace ugcs {
18 namespace vsm {
19 
25 public:
27  typedef std::unique_ptr<Operation_waiter> Ptr;
28 
31 
38  Operation_waiter(Request::Ptr request = nullptr):
39  request(request)
40  {}
41 
43  template <class Request_type>
44  Operation_waiter(std::shared_ptr<Request_type> request):
45  Operation_waiter(Request::Ptr(request))
46  {}
47 
50 
52  Operation_waiter(Operation_waiter &&) = default;
53 
57  Operation_waiter(const Operation_waiter&) = delete;
58 
63 
68  operator=(const Operation_waiter&) = delete;
69 
71  operator bool() const
72  {
73  return request != nullptr;
74  }
75 
87  bool
88  Wait(bool process_ctx = true,
89  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero());
90 
104  void
105  Timeout(std::chrono::milliseconds timeout,
106  Timeout_handler timeout_handler = Timeout_handler(),
107  bool cancel_operation = true,
108  Request_completion_context::Ptr ctx = nullptr);
109 
113  void
114  Cancel();
115 
119  void
120  Abort();
121 
125  bool
126  Is_done() const
127  {
128  return request ? request->Is_done() : true;
129  }
130 
131 private:
133  Request::Ptr request;
134 
138  void
139  Destroy();
140 
147  static bool
148  Timeout_cbk(Request::Ptr request, Timeout_handler handler, bool cancel_operation);
149 
151  static void
152  Request_done_cbk(Timer_processor::Timer::Ptr timer);
153 };
154 
159 DEFINE_CALLBACK_BUILDER(Make_timeout_callback, (Operation_waiter::Ptr), (nullptr))
160 
161 } /* namespace vsm */
162 } /* namespace ugcs */
163 
164 #endif /* OPERATION_WAITER_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Generic request for implementing inter-threads communications and asynchronous operations.
Definition: request_container.h:37
bool Wait(bool process_ctx=true, std::chrono::milliseconds timeout=std::chrono::milliseconds::zero())
Wait for request is fully processed, i.e.
#define DEFINE_CALLBACK_BUILDER(__name, __types, __values)
Define callback builder function.
Definition: callback.h:42
void Timeout(std::chrono::milliseconds timeout, Timeout_handler timeout_handler=Timeout_handler(), bool cancel_operation=true, Request_completion_context::Ptr ctx=nullptr)
Schedule timeout for the operation.
~Operation_waiter()
Destructor which might wait for the request to be done.
void Cancel()
Cancel the operation.
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
bool Is_done() const
Check if request is fully processed, i.e.
Definition: operation_waiter.h:126
Request container declaration.
Operation_waiter(Request::Ptr request=nullptr)
Construct operation waiter.
Definition: operation_waiter.h:38
Helper class for proxying callback invocation.
Definition: callback.h:691
Timer processor.
Operation_waiter & operator=(Operation_waiter &&)
Move assignment operator.
void Abort()
Abort the operation.
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
Callback_proxy< void, Ptr > Timeout_handler
Timeout handler prototype.
Definition: operation_waiter.h:30
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
std::unique_ptr< Operation_waiter > Ptr
Pointer type when used as argument in callbacks.
Definition: operation_waiter.h:27
Operation_waiter(std::shared_ptr< Request_type > request)
Construct operation waiter from any type of request pointer.
Definition: operation_waiter.h:44
Class for synchronizing with request execution.
Definition: operation_waiter.h:24