Realistic 3D camera system
3D camera system components
reactive_descriptor_service.hpp
Go to the documentation of this file.
1 //
2 // detail/reactive_descriptor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
12 #define ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if !defined(ASIO_WINDOWS) \
21  && !defined(ASIO_WINDOWS_RUNTIME) \
22  && !defined(__CYGWIN__)
23 
24 #include "asio/buffer.hpp"
25 #include "asio/io_service.hpp"
35 #include "asio/detail/reactor.hpp"
36 
38 
39 namespace asio {
40 namespace detail {
41 
43 {
44 public:
45  // The native type of a descriptor.
46  typedef int native_handle_type;
47 
48  // The implementation type of the descriptor.
51  {
52  public:
53  // Default constructor.
55  : descriptor_(-1),
56  state_(0)
57  {
58  }
59 
60  private:
61  // Only this service will have access to the internal values.
63 
64  // The native descriptor representation.
65  int descriptor_;
66 
67  // The current state of the descriptor.
69 
70  // Per-descriptor data used by the reactor.
71  reactor::per_descriptor_data reactor_data_;
72  };
73 
74  // Constructor.
77 
78  // Destroy all user-defined handler objects owned by the service.
80 
81  // Construct a new descriptor implementation.
83 
84  // Move-construct a new descriptor implementation.
86  implementation_type& other_impl);
87 
88  // Move-assign from another descriptor implementation.
90  reactive_descriptor_service& other_service,
91  implementation_type& other_impl);
92 
93  // Destroy a descriptor implementation.
95 
96  // Assign a native descriptor to a descriptor implementation.
98  const native_handle_type& native_descriptor,
99  asio::error_code& ec);
100 
101  // Determine whether the descriptor is open.
102  bool is_open(const implementation_type& impl) const
103  {
104  return impl.descriptor_ != -1;
105  }
106 
107  // Destroy a descriptor implementation.
109  asio::error_code& ec);
110 
111  // Get the native descriptor representation.
112  native_handle_type native_handle(const implementation_type& impl) const
113  {
114  return impl.descriptor_;
115  }
116 
117  // Release ownership of the native descriptor representation.
118  ASIO_DECL native_handle_type release(implementation_type& impl);
119 
120  // Cancel all operations associated with the descriptor.
122  asio::error_code& ec);
123 
124  // Perform an IO control command on the descriptor.
125  template <typename IO_Control_Command>
127  IO_Control_Command& command, asio::error_code& ec)
128  {
129  descriptor_ops::ioctl(impl.descriptor_, impl.state_,
130  command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
131  return ec;
132  }
133 
134  // Gets the non-blocking mode of the descriptor.
135  bool non_blocking(const implementation_type& impl) const
136  {
137  return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
138  }
139 
140  // Sets the non-blocking mode of the descriptor.
142  bool mode, asio::error_code& ec)
143  {
145  impl.descriptor_, impl.state_, mode, ec);
146  return ec;
147  }
148 
149  // Gets the non-blocking mode of the native descriptor implementation.
150  bool native_non_blocking(const implementation_type& impl) const
151  {
152  return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
153  }
154 
155  // Sets the non-blocking mode of the native descriptor implementation.
157  bool mode, asio::error_code& ec)
158  {
160  impl.descriptor_, impl.state_, mode, ec);
161  return ec;
162  }
163 
164  // Write some data to the descriptor.
165  template <typename ConstBufferSequence>
167  const ConstBufferSequence& buffers, asio::error_code& ec)
168  {
170  ConstBufferSequence> bufs(buffers);
171 
172  return descriptor_ops::sync_write(impl.descriptor_, impl.state_,
173  bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
174  }
175 
176  // Wait until data can be written without blocking.
178  const null_buffers&, asio::error_code& ec)
179  {
180  // Wait for descriptor to become ready.
181  descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
182 
183  return 0;
184  }
185 
186  // Start an asynchronous write. The data being sent must be valid for the
187  // lifetime of the asynchronous operation.
188  template <typename ConstBufferSequence, typename Handler>
190  const ConstBufferSequence& buffers, Handler& handler)
191  {
192  bool is_continuation =
194 
195  // Allocate and construct an operation to wrap the handler.
197  typename op::ptr p = { asio::detail::addressof(handler),
199  sizeof(op), handler), 0 };
200  p.p = new (p.v) op(impl.descriptor_, buffers, handler);
201 
202  ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_write_some"));
203 
204  start_op(impl, reactor::write_op, p.p, is_continuation, true,
206  ConstBufferSequence>::all_empty(buffers));
207  p.v = p.p = 0;
208  }
209 
210  // Start an asynchronous wait until data can be written without blocking.
211  template <typename Handler>
213  const null_buffers&, Handler& handler)
214  {
215  bool is_continuation =
217 
218  // Allocate and construct an operation to wrap the handler.
220  typename op::ptr p = { asio::detail::addressof(handler),
222  sizeof(op), handler), 0 };
223  p.p = new (p.v) op(handler);
224 
225  ASIO_HANDLER_CREATION((p.p, "descriptor",
226  &impl, "async_write_some(null_buffers)"));
227 
228  start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
229  p.v = p.p = 0;
230  }
231 
232  // Read some data from the stream. Returns the number of bytes read.
233  template <typename MutableBufferSequence>
235  const MutableBufferSequence& buffers, asio::error_code& ec)
236  {
238  MutableBufferSequence> bufs(buffers);
239 
240  return descriptor_ops::sync_read(impl.descriptor_, impl.state_,
241  bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
242  }
243 
244  // Wait until data can be read without blocking.
246  const null_buffers&, asio::error_code& ec)
247  {
248  // Wait for descriptor to become ready.
249  descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
250 
251  return 0;
252  }
253 
254  // Start an asynchronous read. The buffer for the data being read must be
255  // valid for the lifetime of the asynchronous operation.
256  template <typename MutableBufferSequence, typename Handler>
258  const MutableBufferSequence& buffers, Handler& handler)
259  {
260  bool is_continuation =
262 
263  // Allocate and construct an operation to wrap the handler.
265  typename op::ptr p = { asio::detail::addressof(handler),
267  sizeof(op), handler), 0 };
268  p.p = new (p.v) op(impl.descriptor_, buffers, handler);
269 
270  ASIO_HANDLER_CREATION((p.p, "descriptor", &impl, "async_read_some"));
271 
272  start_op(impl, reactor::read_op, p.p, is_continuation, true,
274  MutableBufferSequence>::all_empty(buffers));
275  p.v = p.p = 0;
276  }
277 
278  // Wait until data can be read without blocking.
279  template <typename Handler>
281  const null_buffers&, Handler& handler)
282  {
283  bool is_continuation =
285 
286  // Allocate and construct an operation to wrap the handler.
288  typename op::ptr p = { asio::detail::addressof(handler),
290  sizeof(op), handler), 0 };
291  p.p = new (p.v) op(handler);
292 
293  ASIO_HANDLER_CREATION((p.p, "descriptor",
294  &impl, "async_read_some(null_buffers)"));
295 
296  start_op(impl, reactor::read_op, p.p, is_continuation, false, false);
297  p.v = p.p = 0;
298  }
299 
300 private:
301  // Start the asynchronous operation.
302  ASIO_DECL void start_op(implementation_type& impl, int op_type,
303  reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
304 
305  // The selector that performs event demultiplexing for the service.
306  reactor& reactor_;
307 };
308 
309 } // namespace detail
310 } // namespace asio
311 
313 
314 #if defined(ASIO_HEADER_ONLY)
316 #endif // defined(ASIO_HEADER_ONLY)
317 
318 #endif // !defined(ASIO_WINDOWS)
319  // && !defined(ASIO_WINDOWS_RUNTIME)
320  // && !defined(__CYGWIN__)
321 
322 #endif // ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
ASIO_DECL bool set_internal_non_blocking(int d, state_type &state, bool value, asio::error_code &ec)
native_handle_type native_handle(const implementation_type &impl) const
void async_write_some(implementation_type &impl, const null_buffers &, Handler &handler)
void async_write_some(implementation_type &impl, const ConstBufferSequence &buffers, Handler &handler)
asio::error_code native_non_blocking(implementation_type &impl, bool mode, asio::error_code &ec)
Provides core I/O functionality.
Definition: io_service.hpp:184
Holds a buffer that cannot be modified.
Definition: buffer.hpp:211
asio::error_code non_blocking(implementation_type &impl, bool mode, asio::error_code &ec)
ASIO_DECL std::size_t sync_write(int d, state_type state, const buf *bufs, std::size_t count, bool all_empty, asio::error_code &ec)
size_t read_some(implementation_type &impl, const null_buffers &, asio::error_code &ec)
size_t read_some(implementation_type &impl, const MutableBufferSequence &buffers, asio::error_code &ec)
ASIO_DECL asio::error_code close(implementation_type &impl, asio::error_code &ec)
size_t write_some(implementation_type &impl, const ConstBufferSequence &buffers, asio::error_code &ec)
size_t write_some(implementation_type &impl, const null_buffers &, asio::error_code &ec)
ASIO_DECL void move_construct(implementation_type &impl, implementation_type &other_impl)
ASIO_DECL asio::error_code cancel(implementation_type &impl, asio::error_code &ec)
const MutableBufferSequence & buffers
Definition: read.hpp:521
ASIO_DECL void move_assign(implementation_type &impl, reactive_descriptor_service &other_service, implementation_type &other_impl)
Holds a buffer that can be modified.
Definition: buffer.hpp:91
class select_reactor reactor
Definition: reactor_fwd.hpp:34
void async_read_some(implementation_type &impl, const null_buffers &, Handler &handler)
ASIO_DECL std::size_t sync_read(int d, state_type state, buf *bufs, std::size_t count, bool all_empty, asio::error_code &ec)
ASIO_DECL bool set_user_non_blocking(int d, state_type &state, bool value, asio::error_code &ec)
ASIO_DECL int poll_write(int d, state_type state, asio::error_code &ec)
ASIO_DECL int poll_read(int d, state_type state, asio::error_code &ec)
bool is_continuation(Context &context)
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
bool native_non_blocking(const implementation_type &impl) const
asio::error_code io_control(implementation_type &impl, IO_Control_Command &command, asio::error_code &ec)
bool non_blocking(const implementation_type &impl) const
ASIO_DECL asio::error_code assign(implementation_type &impl, const native_handle_type &native_descriptor, asio::error_code &ec)
void async_read_some(implementation_type &impl, const MutableBufferSequence &buffers, Handler &handler)
void * allocate(std::size_t s, Handler &h)
ASIO_DECL void construct(implementation_type &impl)
ASIO_DECL void destroy(implementation_type &impl)
#define ASIO_HANDLER_CREATION(args)
ASIO_DECL native_handle_type release(implementation_type &impl)
ASIO_DECL int ioctl(int d, state_type &state, long cmd, ioctl_arg_type *arg, asio::error_code &ec)
bool is_open(const implementation_type &impl) const