zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
poller.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_POLLER_HPP_
18 #define ZMQPP_POLLER_HPP_
19 
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "compatibility.hpp"
24 
25 namespace zmqpp
26 {
27 
28 class socket;
29 typedef socket socket_t;
30 
37 {
38 public:
39  enum {
40  wait_forever = -1
41  };
42 
43  enum : short {
44  poll_none = 0,
45  poll_in = ZMQ_POLLIN,
46  poll_out = ZMQ_POLLOUT,
47  poll_error = ZMQ_POLLERR,
48 #if ((ZMQ_VERSION_MAJOR == 4 && ZMQ_VERSION_MINOR >= 2) || ZMQ_VERSION_MAJOR > 4)
49  poll_pri = ZMQ_POLLPRI
50 #endif
51  };
52 
56  poller();
57 
63  ~poller();
64 
71  void add(socket_t& socket, short const event = poll_in);
72 
79  void add(raw_socket_t const descriptor, short const event = poll_in | poll_error);
80 
88  void add(zmq_pollitem_t const& item);
89 
96  bool has(socket_t const& socket);
97 
104  bool has(raw_socket_t const descriptor);
105 
114  bool has(zmq_pollitem_t const& item);
115 
121  void remove(socket_t const& socket);
122 
128  void remove(raw_socket_t const descriptor);
129 
135  void remove(zmq_pollitem_t const& item);
136 
143  void check_for(socket_t const& socket, short const event);
144 
151  void check_for(raw_socket_t const descriptor, short const event);
152 
159  void check_for(zmq_pollitem_t const& item, short const event);
160 
172  bool poll(long timeout = wait_forever);
173 
180  short events(socket_t const& socket) const;
181 
188  short events(raw_socket_t const descriptor) const;
189 
196  short events(zmq_pollitem_t const& item) const;
197 
206  template<typename Watched>
207  bool has_input(Watched const& watchable) const { return (events(watchable) & poll_in) != 0; }
208 
217  template<typename Watched>
218  bool has_output(Watched const& watchable) const { return (events(watchable) & poll_out) != 0; }
219 
231  template<typename Watched>
232  bool has_error(Watched const& watchable) const { return (events(watchable) & poll_error) != 0; }
233 
234 private:
235  std::vector<zmq_pollitem_t> _items;
236  std::unordered_map<void *, size_t> _index;
237  std::unordered_map<raw_socket_t, size_t> _fdindex;
238 
239  void reindex(size_t const index);
240 };
241 
242 }
243 
244 #endif /* ZMQPP_POLLER_HPP_ */
C++ wrapper around zmq.
Definition: actor.cpp:29
The socket class represents the zmq sockets.
Definition: socket.hpp:75
socket socket_t
socket type
Definition: loop.hpp:25
bool has_error(Watched const &watchable) const
Check a standard socket (file descriptor or SOCKET).
Definition: poller.hpp:232
int raw_socket_t
Definition: compatibility.hpp:134
bool has_output(Watched const &watchable) const
Check either a standard socket or zmq socket for output events.
Definition: poller.hpp:218
std::vector< zmq_pollitem_t > _items
Definition: poller.hpp:235
#define ZMQPP_EXPORT
Definition: compatibility.hpp:39
std::unordered_map< void *, size_t > _index
Definition: poller.hpp:236
Polling wrapper.
Definition: poller.hpp:36
bool has_input(Watched const &watchable) const
Check either a standard socket or zmq socket for input events.
Definition: poller.hpp:207
std::unordered_map< raw_socket_t, size_t > _fdindex
Definition: poller.hpp:237