Realistic 3D camera system
3D camera system components
win_object_handle_service.hpp
Go to the documentation of this file.
1 //
2 // detail/win_object_handle_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 
12 #ifndef ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
13 #define ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
14 
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 
19 #include "asio/detail/config.hpp"
20 
21 #if defined(ASIO_HAS_WINDOWS_OBJECT_HANDLE)
22 
26 #include "asio/error.hpp"
27 #include "asio/io_service.hpp"
28 
30 
31 namespace asio {
32 namespace detail {
33 
34 class win_object_handle_service
35 {
36 public:
37  // The native type of an object handle.
38  typedef HANDLE native_handle_type;
39 
40  // The implementation type of the object handle.
41  class implementation_type
42  {
43  public:
44  // Default constructor.
45  implementation_type()
46  : handle_(INVALID_HANDLE_VALUE),
47  wait_handle_(INVALID_HANDLE_VALUE),
48  owner_(0),
49  next_(0),
50  prev_(0)
51  {
52  }
53 
54  private:
55  // Only this service will have access to the internal values.
56  friend class win_object_handle_service;
57 
58  // The native object handle representation. May be accessed or modified
59  // without locking the mutex.
60  native_handle_type handle_;
61 
62  // The handle used to unregister the wait operation. The mutex must be
63  // locked when accessing or modifying this member.
64  HANDLE wait_handle_;
65 
66  // The operations waiting on the object handle. If there is a registered
67  // wait then the mutex must be locked when accessing or modifying this
68  // member
69  op_queue<wait_op> op_queue_;
70 
71  // The service instance that owns the object handle implementation.
72  win_object_handle_service* owner_;
73 
74  // Pointers to adjacent handle implementations in linked list. The mutex
75  // must be locked when accessing or modifying these members.
76  implementation_type* next_;
77  implementation_type* prev_;
78  };
79 
80  // Constructor.
81  ASIO_DECL win_object_handle_service(
82  asio::io_service& io_service);
83 
84  // Destroy all user-defined handler objects owned by the service.
85  ASIO_DECL void shutdown_service();
86 
87  // Construct a new handle implementation.
88  ASIO_DECL void construct(implementation_type& impl);
89 
90  // Move-construct a new handle implementation.
91  ASIO_DECL void move_construct(implementation_type& impl,
92  implementation_type& other_impl);
93 
94  // Move-assign from another handle implementation.
95  ASIO_DECL void move_assign(implementation_type& impl,
96  win_object_handle_service& other_service,
97  implementation_type& other_impl);
98 
99  // Destroy a handle implementation.
100  ASIO_DECL void destroy(implementation_type& impl);
101 
102  // Assign a native handle to a handle implementation.
103  ASIO_DECL asio::error_code assign(implementation_type& impl,
104  const native_handle_type& handle, asio::error_code& ec);
105 
106  // Determine whether the handle is open.
107  bool is_open(const implementation_type& impl) const
108  {
109  return impl.handle_ != INVALID_HANDLE_VALUE && impl.handle_ != 0;
110  }
111 
112  // Destroy a handle implementation.
113  ASIO_DECL asio::error_code close(implementation_type& impl,
114  asio::error_code& ec);
115 
116  // Get the native handle representation.
117  native_handle_type native_handle(const implementation_type& impl) const
118  {
119  return impl.handle_;
120  }
121 
122  // Cancel all operations associated with the handle.
123  ASIO_DECL asio::error_code cancel(implementation_type& impl,
124  asio::error_code& ec);
125 
126  // Perform a synchronous wait for the object to enter a signalled state.
127  ASIO_DECL void wait(implementation_type& impl,
128  asio::error_code& ec);
129 
131  template <typename Handler>
132  void async_wait(implementation_type& impl, Handler& handler)
133  {
134  // Allocate and construct an operation to wrap the handler.
135  typedef wait_handler<Handler> op;
136  typename op::ptr p = { asio::detail::addressof(handler),
138  sizeof(op), handler), 0 };
139  p.p = new (p.v) op(handler);
140 
141  ASIO_HANDLER_CREATION((p.p, "object_handle", &impl, "async_wait"));
142 
143  start_wait_op(impl, p.p);
144  p.v = p.p = 0;
145  }
146 
147 private:
148  // Helper function to start an asynchronous wait operation.
149  ASIO_DECL void start_wait_op(implementation_type& impl, wait_op* op);
150 
151  // Helper function to register a wait operation.
152  ASIO_DECL void register_wait_callback(
153  implementation_type& impl, mutex::scoped_lock& lock);
154 
155  // Callback function invoked when the registered wait completes.
156  static ASIO_DECL VOID CALLBACK wait_callback(
157  PVOID param, BOOLEAN timeout);
158 
159  // The io_service implementation used to post completions.
160  io_service_impl& io_service_;
161 
162  // Mutex to protect access to internal state.
163  mutex mutex_;
164 
165  // The head of a linked list of all implementations.
166  implementation_type* impl_list_;
167 
168  // Flag to indicate that the dispatcher has been shut down.
169  bool shutdown_;
170 };
171 
172 } // namespace detail
173 } // namespace asio
174 
176 
177 #if defined(ASIO_HEADER_ONLY)
179 #endif // defined(ASIO_HEADER_ONLY)
180 
181 #endif // defined(ASIO_HAS_WINDOWS_OBJECT_HANDLE)
182 
183 #endif // ASIO_DETAIL_WIN_OBJECT_HANDLE_SERVICE_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
null_mutex mutex
Definition: mutex.hpp:36
class task_io_service io_service_impl
Definition: io_service.hpp:48
asio::detail::scoped_lock< null_mutex > scoped_lock
Definition: null_mutex.hpp:34
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
ASIO_DECL int close(int d, state_type &state, asio::error_code &ec)
void * allocate(std::size_t s, Handler &h)
#define ASIO_HANDLER_CREATION(args)