VSM C++ SDK
Vehicle Specific Modules SDK
io_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 
11 #ifndef IO_TASK_H_
12 #define IO_TASK_H_
13 
14 #include <ugcs/vsm/io_stream.h>
15 #include <ugcs/vsm/utils.h>
16 
17 namespace ugcs {
18 namespace vsm {
19 
21 class Io_request: public Request {
23 
24 public:
25 
36  Io_result& result_arg):
37  stream(stream), offset(offset), result_arg(result_arg)
38  {}
39 
42  Get_stream() const
43  {
44  return stream;
45  }
46 
50  {
51  return offset;
52  }
53 
60  void
62  Io_result result,
63  const Request::Locker& lock = Request::Locker())
64  {
65  Request::Locker local_lock;
66  if (lock) {
67  ASSERT(Lock(false).mutex() == lock.mutex());
68  } else {
69  local_lock = Lock();
70  }
72  result_arg = result;
73  }
74  last_result = result;
75  }
76 
81  Io_result
83  {
84  return last_result;
85  }
86 
87 private:
89  Io_stream::Ptr stream;
90 
92  Io_stream::Offset offset;
93 
95  Io_result &result_arg;
96 
98  Io_result last_result = Io_result::OTHER_FAILURE;
99 
100  virtual void
101  Destroy() override
102  {
103  stream = nullptr;
104  }
105 };
106 
108 class Write_request : public Io_request
109 {
110 public:
112  typedef std::shared_ptr<Write_request> Ptr;
113 
118  template <typename... Args>
119  Write_request(Io_buffer::Ptr buffer, Args &&... args):
120  Io_request(std::forward<Args>(args)...), buffer(buffer)
121  {}
122 
124  template <typename... Args>
125  static Ptr
126  Create(Args &&... args)
127  {
128  return std::make_shared<Write_request>(std::forward<Args>(args)...);
129  }
130 
134  {
135  return buffer;
136  }
137 
138 private:
140  Io_buffer::Ptr buffer;
141 };
142 
144 class Read_request : public Io_request
145 {
146 public:
148  typedef std::shared_ptr<Read_request> Ptr;
149 
158  template <typename... Args>
159  Read_request(Io_buffer::Ptr& buffer_arg, size_t max_to_read, size_t min_to_read,
160  Args &&... args):
161  Io_request(std::forward<Args>(args)...),
162  buffer_arg(buffer_arg), max_to_read(max_to_read), min_to_read(min_to_read)
163  {}
164 
166  template <typename... Args>
167  static Ptr
168  Create(Args &&... args)
169  {
170  return std::make_shared<Read_request>(std::forward<Args>(args)...);
171  }
172 
179  void
181  Io_buffer::Ptr buffer,
182  const Request::Locker& lock = Request::Locker())
183  {
184  Request::Locker local_lock;
185  if (lock) {
186  ASSERT(Lock(false).mutex() == lock.mutex());
187  } else {
188  local_lock = Lock();
189  }
191  buffer_arg = buffer;
192  }
193  last_read_buffer = buffer;
194  }
195 
202  {
203  return last_read_buffer;
204  }
205 
207  size_t
209  {
210  return max_to_read;
211  }
212 
214  size_t
216  {
217  return min_to_read;
218  }
219 
220 private:
222  Io_buffer::Ptr &buffer_arg;
223 
225  Io_buffer::Ptr last_read_buffer;
226 
228  size_t max_to_read,
230  min_to_read;
231 };
232 
233 } /* namespace vsm */
234 } /* namespace ugcs */
235 
236 #endif /* IO_TASK_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
void Set_buffer_arg(Io_buffer::Ptr buffer, const Request::Locker &lock=Request::Locker())
Sets the buffer argument.
Definition: io_request.h:180
Generic request for implementing inter-threads communications and asynchronous operations.
Definition: request_container.h:37
Io_stream::Ptr Get_stream() const
Locks and gets the associated stream.
Definition: io_request.h:42
static Ptr Create(Args &&...args)
Create an instance of the request.
Definition: io_request.h:126
int64_t Offset
Offset for read/write operations.
Definition: io_stream.h:62
Baser I/O write request.
Definition: io_request.h:108
Io_result Get_last_result()
Get the most recently set result value.
Definition: io_request.h:82
Abstract stream for I/O operations.
Write_request(Io_buffer::Ptr buffer, Args &&...args)
Construct write request.
Definition: io_request.h:119
size_t Get_max_to_read() const
Get maximal number of bytes to read.
Definition: io_request.h:208
STL namespace.
Basic I/O read request.
Definition: io_request.h:144
bool Is_completion_handler_present()
Checks the availability of the completion handler.
Io_result
Result of I/O operation.
Definition: io_stream.h:25
#define ASSERT(x)
No action in release.
Definition: debug.h:68
Io_buffer::Ptr & Data_buffer()
Access the buffer with data to write.
Definition: io_request.h:133
Various common utilities.
Abstract I/O stream interface.
Definition: io_stream.h:54
std::shared_ptr< Io_request > Ptr
Pointer type.
Definition: io_request.h:22
Some other system failure.
size_t Get_min_to_read() const
Get minimal number of bytes to read.
Definition: io_request.h:215
Locker Lock(bool acquire=true) const
Acquire lock for request internal state.
Read_request(Io_buffer::Ptr &buffer_arg, size_t max_to_read, size_t min_to_read, Args &&...args)
Constructor.
Definition: io_request.h:159
Io_stream::Offset & Offset()
Get I/O operation offset value.
Definition: io_request.h:49
Base request for I/O operations.
Definition: io_request.h:21
std::shared_ptr< Read_request > Ptr
Shared pointer to read request.
Definition: io_request.h:148
void Set_result_arg(Io_result result, const Request::Locker &lock=Request::Locker())
Sets the result argument.
Definition: io_request.h:61
std::unique_lock< std::mutex > Locker
Smart lock object for request external locking.
Definition: request_container.h:87
std::shared_ptr< Write_request > Ptr
Shared pointer to write request.
Definition: io_request.h:112
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Io_stream > Ptr
Pointer type.
Definition: io_stream.h:55
Io_buffer::Ptr Get_last_read_buffer()
Get the most recently set read buffer.
Definition: io_request.h:201
static Ptr Create(Args &&...args)
Create an instance of read request.
Definition: io_request.h:168