Realistic 3D camera system
3D camera system components
reactor_op_queue.hpp
Go to the documentation of this file.
1 //
2 // detail/reactor_op_queue.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_REACTOR_OP_QUEUE_HPP
12 #define ASIO_DETAIL_REACTOR_OP_QUEUE_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 #include "asio/detail/hash_map.hpp"
21 #include "asio/detail/op_queue.hpp"
23 #include "asio/error.hpp"
24 
26 
27 namespace asio {
28 namespace detail {
29 
30 template <typename Descriptor>
32  : private noncopyable
33 {
34 public:
35  typedef Descriptor key_type;
36 
37  struct mapped_type : op_queue<reactor_op>
38  {
41  void operator=(const mapped_type&) {}
42  };
43 
46 
47  // Constructor.
49  : operations_()
50  {
51  }
52 
53  // Obtain iterators to all registered descriptors.
54  iterator begin() { return operations_.begin(); }
55  iterator end() { return operations_.end(); }
56 
57  // Add a new operation to the queue. Returns true if this is the only
58  // operation for the given descriptor, in which case the reactor's event
59  // demultiplexing function call may need to be interrupted and restarted.
60  bool enqueue_operation(Descriptor descriptor, reactor_op* op)
61  {
62  std::pair<iterator, bool> entry =
63  operations_.insert(value_type(descriptor, mapped_type()));
64  entry.first->second.push(op);
65  return entry.second;
66  }
67 
68  // Cancel all operations associated with the descriptor identified by the
69  // supplied iterator. Any operations pending for the descriptor will be
70  // cancelled. Returns true if any operations were cancelled, in which case
71  // the reactor's event demultiplexing function may need to be interrupted and
72  // restarted.
73  bool cancel_operations(iterator i, op_queue<operation>& ops,
74  const asio::error_code& ec =
76  {
77  if (i != operations_.end())
78  {
79  while (reactor_op* op = i->second.front())
80  {
81  op->ec_ = ec;
82  i->second.pop();
83  ops.push(op);
84  }
85  operations_.erase(i);
86  return true;
87  }
88 
89  return false;
90  }
91 
92  // Cancel all operations associated with the descriptor. Any operations
93  // pending for the descriptor will be cancelled. Returns true if any
94  // operations were cancelled, in which case the reactor's event
95  // demultiplexing function may need to be interrupted and restarted.
96  bool cancel_operations(Descriptor descriptor, op_queue<operation>& ops,
97  const asio::error_code& ec =
99  {
100  return this->cancel_operations(operations_.find(descriptor), ops, ec);
101  }
102 
103  // Whether there are no operations in the queue.
104  bool empty() const
105  {
106  return operations_.empty();
107  }
108 
109  // Determine whether there are any operations associated with the descriptor.
110  bool has_operation(Descriptor descriptor) const
111  {
112  return operations_.find(descriptor) != operations_.end();
113  }
114 
115  // Perform the operations corresponding to the descriptor identified by the
116  // supplied iterator. Returns true if there are still unfinished operations
117  // queued for the descriptor.
119  {
120  if (i != operations_.end())
121  {
122  while (reactor_op* op = i->second.front())
123  {
124  if (op->perform())
125  {
126  i->second.pop();
127  ops.push(op);
128  }
129  else
130  {
131  return true;
132  }
133  }
134  operations_.erase(i);
135  }
136  return false;
137  }
138 
139  // Perform the operations corresponding to the descriptor. Returns true if
140  // there are still unfinished operations queued for the descriptor.
141  bool perform_operations(Descriptor descriptor, op_queue<operation>& ops)
142  {
143  return this->perform_operations(operations_.find(descriptor), ops);
144  }
145 
146  // Get all operations owned by the queue.
148  {
149  iterator i = operations_.begin();
150  while (i != operations_.end())
151  {
152  iterator op_iter = i++;
153  ops.push(op_iter->second);
154  operations_.erase(op_iter);
155  }
156  }
157 
158 private:
159  // The operations that are currently executing asynchronously.
161 };
162 
163 } // namespace detail
164 } // namespace asio
165 
167 
168 #endif // ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
Operation cancelled.
Definition: error.hpp:161
bool perform_operations(Descriptor descriptor, op_queue< operation > &ops)
bool has_operation(Descriptor descriptor) const
bool enqueue_operation(Descriptor descriptor, reactor_op *op)
void get_all_operations(op_queue< operation > &ops)
bool perform_operations(iterator i, op_queue< operation > &ops)
bool cancel_operations(Descriptor descriptor, op_queue< operation > &ops, const asio::error_code &ec=asio::error::operation_aborted)
bool cancel_operations(iterator i, op_queue< operation > &ops, const asio::error_code &ec=asio::error::operation_aborted)
void push(Operation *h)
Definition: op_queue.hpp:104
hash_map< key_type, mapped_type >::value_type value_type
Class to represent an error code value.
Definition: error_code.hpp:80
std::pair< K, V > value_type
Definition: hash_map.hpp:58
hash_map< key_type, mapped_type >::iterator iterator
std::list< value_type >::iterator iterator
Definition: hash_map.hpp:61