Realistic 3D camera system
3D camera system components
win_iocp_overlapped_ptr.hpp
Go to the documentation of this file.
1 //
2 // detail/win_iocp_overlapped_ptr.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_WIN_IOCP_OVERLAPPED_PTR_HPP
12 #define ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_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_HAS_IOCP)
21 
22 #include "asio/io_service.hpp"
28 
30 
31 namespace asio {
32 namespace detail {
33 
34 // Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
35 class win_iocp_overlapped_ptr
36  : private noncopyable
37 {
38 public:
39  // Construct an empty win_iocp_overlapped_ptr.
40  win_iocp_overlapped_ptr()
41  : ptr_(0),
42  iocp_service_(0)
43  {
44  }
45 
46  // Construct an win_iocp_overlapped_ptr to contain the specified handler.
47  template <typename Handler>
48  explicit win_iocp_overlapped_ptr(
49  asio::io_service& io_service, ASIO_MOVE_ARG(Handler) handler)
50  : ptr_(0),
51  iocp_service_(0)
52  {
53  this->reset(io_service, ASIO_MOVE_CAST(Handler)(handler));
54  }
55 
56  // Destructor automatically frees the OVERLAPPED object unless released.
57  ~win_iocp_overlapped_ptr()
58  {
59  reset();
60  }
61 
62  // Reset to empty.
63  void reset()
64  {
65  if (ptr_)
66  {
67  ptr_->destroy();
68  ptr_ = 0;
69  iocp_service_->work_finished();
70  iocp_service_ = 0;
71  }
72  }
73 
74  // Reset to contain the specified handler, freeing any current OVERLAPPED
75  // object.
76  template <typename Handler>
77  void reset(asio::io_service& io_service, Handler handler)
78  {
79  typedef win_iocp_overlapped_op<Handler> op;
80  typename op::ptr p = { asio::detail::addressof(handler),
82  sizeof(op), handler), 0 };
83  p.p = new (p.v) op(handler);
84 
85  ASIO_HANDLER_CREATION((p.p, "io_service",
86  &io_service.impl_, "overlapped"));
87 
88  io_service.impl_.work_started();
89  reset();
90  ptr_ = p.p;
91  p.v = p.p = 0;
92  iocp_service_ = &io_service.impl_;
93  }
94 
95  // Get the contained OVERLAPPED object.
96  OVERLAPPED* get()
97  {
98  return ptr_;
99  }
100 
101  // Get the contained OVERLAPPED object.
102  const OVERLAPPED* get() const
103  {
104  return ptr_;
105  }
106 
107  // Release ownership of the OVERLAPPED object.
108  OVERLAPPED* release()
109  {
110  if (ptr_)
111  iocp_service_->on_pending(ptr_);
112 
113  OVERLAPPED* tmp = ptr_;
114  ptr_ = 0;
115  iocp_service_ = 0;
116  return tmp;
117  }
118 
119  // Post completion notification for overlapped operation. Releases ownership.
120  void complete(const asio::error_code& ec,
121  std::size_t bytes_transferred)
122  {
123  if (ptr_)
124  {
125  iocp_service_->on_completion(ptr_, ec,
126  static_cast<DWORD>(bytes_transferred));
127  ptr_ = 0;
128  iocp_service_ = 0;
129  }
130  }
131 
132 private:
133  win_iocp_operation* ptr_;
134  win_iocp_io_service* iocp_service_;
135 };
136 
137 } // namespace detail
138 } // namespace asio
139 
141 
142 #endif // defined(ASIO_HAS_IOCP)
143 
144 #endif // ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
asio::basic_streambuf< Allocator > CompletionCondition ASIO_MOVE_ARG(ReadHandler) handler)
Definition: read.hpp:704
Class to represent an error code value.
Definition: error_code.hpp:80
void * allocate(std::size_t s, Handler &h)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
#define ASIO_HANDLER_CREATION(args)