VSM C++ SDK
Vehicle Specific Modules SDK
vehicle_request.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 
9 #ifndef _VEHICLE_REQUEST_H_
10 #define _VEHICLE_REQUEST_H_
11 
14 #include <ugcs/vsm/debug.h>
15 
16 namespace ugcs {
17 namespace vsm {
18 
21 class Vehicle_request: public std::enable_shared_from_this<Vehicle_request> {
23 public:
24 
26  enum class Result {
28  OK,
30  NOK
31  };
32 
38  class Handle {
39  public:
40 
42  Handle();
43 
47 
53  explicit operator bool() const;
54 
55  void
56  Fail(const char *format = nullptr, ...) __FORMAT(printf, 2, 3);
57 
58  void
59  Fail_v(const char *format, va_list fmt_args) __FORMAT(printf, 2, 0);
60 
61  void
62  Fail(const std::string& reason);
63 
64  void
65  Succeed();
66 
67  protected:
68 
70  void
71  Assign_result(Result result, const std::string& status_text = std::string());
72 
75  };
76 
79 
81  Vehicle_request(Completion_handler completion_handler,
82  Request_completion_context::Ptr completion_ctx);
83 
85  virtual
87 
92  void
93  Set_completion_result(Result result, const std::string&);
94 
97  void
98  Complete();
99 
103  void
104  Abort();
105 
107  Result
109 
111  bool
112  Is_completed() const;
113 
115  void
116  Add_ref();
117 
121  void
122  Release_ref();
123 
124 private:
125 
127  Request::Ptr request;
128 
130  Completion_handler completion_handler;
131 
133  std::atomic_int ref_count = { 0 };
134 
135  friend class Vehicle;
136 };
137 
139 template <class Payload>
142 
143 public:
144 
149  template<typename... Args>
151  Request_completion_context::Ptr completion_ctx,
152  Args &&... args):
153  Vehicle_request(completion_handler, completion_ctx),
154  payload(std::forward<Args>(args)...)
155  {}
156 
161  public:
162 
164 
166  Payload*
167  operator ->()
168  {
169  return &static_cast<Vehicle_request_spec&>(*vehicle_request).payload;
170  }
171 
173  const Payload*
174  operator ->() const
175  {
176  return &static_cast<Vehicle_request_spec&>(*vehicle_request).payload;
177  }
178 
180  Payload&
181  operator *()
182  {
183  return static_cast<Vehicle_request_spec&>(*vehicle_request).payload;
184  }
185 
186  const Payload&
187  operator *() const
188  {
189  return static_cast<Vehicle_request_spec&>(*vehicle_request).payload;
190  }
191  };
192 
194  Payload payload;
195 };
196 
198 template<>
201 public:
203  template<typename... Args>
204  Vehicle_request_spec(Args &&... args):
205  Vehicle_request(std::forward<Args>(args)...)
206  {}
207 };
208 
209 } /* namespace vsm */
210 } /* namespace ugcs */
211 
212 #endif /* _VEHICLE_REQUEST_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Payload payload
Specific payload of the request.
Definition: vehicle_request.h:194
void Assign_result(Result result, const std::string &status_text=std::string())
Assignment operator implementation.
Result Get_completion_result()
Get current completion result.
Debugging and troubleshooting helpers.
void Add_ref()
Add reference from user handle.
Vehicle_request(Completion_handler completion_handler, Request_completion_context::Ptr completion_ctx)
Construct base request instance.
Reference_guard< Vehicle_request::Ptr > vehicle_request
Managed vehicle request.
Definition: vehicle_request.h:74
STL namespace.
Reference guard class definition.
std::shared_ptr< Vehicle_request > Ptr
Pointer type.
Definition: vehicle_request.h:22
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Operation completed successfully.
Request execution context.
Request execution context.
Definition: request_context.h:24
#define __FORMAT(type, fmt_idx, arg_idx)
Specify that a function has format arguments (like printf or scanf).
Definition: defs.h:24
Result
Request completion result.
Definition: vehicle_request.h:26
virtual ~Vehicle_request()
Make sure class is polymorphic.
Handle of a specific vehicle request.
Definition: vehicle_request.h:160
void Complete()
Should be called when vehicle request is completed by the user.
bool Is_completed() const
Check if the request was completed.
void Set_completion_result(Result result, const std::string &)
Set completion result to be used when Complete method is called.
Base class of a generic request for a vehicle.
Definition: vehicle_request.h:21
void Abort()
Abort the request.
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
Callback_proxy< void, Result, std::string > Completion_handler
Completion handler type of the request.
Definition: vehicle_request.h:78
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
Vehicle request with specific payload.
Definition: vehicle_request.h:140
Handle()
Construct initially invalid handle.
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Handle of the request passed to the SDK user.
Definition: vehicle_request.h:38
void Release_ref()
Release reference from user handle.
Base class for user-defined vehicles.
Definition: vehicle.h:35