Problem: poller item lookup can be simplified

Solution: Extract generic find function
parent 7b1fef28
...@@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_) ...@@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_)
return socket_.is_thread_safe (); return socket_.is_thread_safe ();
} }
// compare elements to value
template <class It, class T, class Pred>
static It find_if2 (It b_, It e_, const T &value, Pred pred)
{
for (; b_ != e_; ++b_) {
if (pred (*b_, value)) {
break;
}
}
return b_;
}
zmq::socket_poller_t::socket_poller_t () : zmq::socket_poller_t::socket_poller_t () :
_tag (0xCAFEBABE), _tag (0xCAFEBABE),
_signaler (NULL) _signaler (NULL)
...@@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_, ...@@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
void *user_data_, void *user_data_,
short events_) short events_)
{ {
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end; if (find_if2 (_items.begin (), _items.end (), socket_, &is_socket)
++it) { != _items.end ()) {
if (it->socket == socket_) { errno = EINVAL;
errno = EINVAL; return -1;
return -1;
}
} }
if (is_thread_safe (*socket_)) { if (is_thread_safe (*socket_)) {
...@@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_, ...@@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_,
int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_) int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
{ {
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end; if (find_if2 (_items.begin (), _items.end (), fd_, &is_fd)
++it) { != _items.end ()) {
if (!it->socket && it->fd == fd_) { errno = EINVAL;
errno = EINVAL; return -1;
return -1;
}
} }
const item_t item = { const item_t item = {
...@@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_) ...@@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_) int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
{ {
const items_t::iterator end = _items.end (); const items_t::iterator it =
items_t::iterator it; find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
for (it = _items.begin (); it != end; ++it) {
if (it->socket == socket_)
break;
}
if (it == end) { if (it == _items.end ()) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
...@@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_) ...@@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_) int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
{ {
const items_t::iterator end = _items.end (); const items_t::iterator it =
items_t::iterator it; find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
for (it = _items.begin (); it != end; ++it) { if (it == _items.end ()) {
if (!it->socket && it->fd == fd_)
break;
}
if (it == end) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
...@@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_) ...@@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
int zmq::socket_poller_t::remove (socket_base_t *socket_) int zmq::socket_poller_t::remove (socket_base_t *socket_)
{ {
const items_t::iterator end = _items.end (); const items_t::iterator it =
items_t::iterator it; find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
for (it = _items.begin (); it != end; ++it) {
if (it->socket == socket_)
break;
}
if (it == end) { if (it == _items.end ()) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
...@@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_) ...@@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_)
int zmq::socket_poller_t::remove_fd (fd_t fd_) int zmq::socket_poller_t::remove_fd (fd_t fd_)
{ {
const items_t::iterator end = _items.end (); const items_t::iterator it =
items_t::iterator it; find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
for (it = _items.begin (); it != end; ++it) {
if (!it->socket && it->fd == fd_)
break;
}
if (it == end) { if (it == _items.end ()) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
......
...@@ -86,6 +86,17 @@ class socket_poller_t ...@@ -86,6 +86,17 @@ class socket_poller_t
bool check_tag () const; bool check_tag () const;
private: private:
typedef struct item_t
{
socket_base_t *socket;
fd_t fd;
void *user_data;
short events;
#if defined ZMQ_POLL_BASED_ON_POLL
int pollfd_index;
#endif
} item_t;
static void zero_trail_events (zmq::socket_poller_t::event_t *events_, static void zero_trail_events (zmq::socket_poller_t::event_t *events_,
int n_events_, int n_events_,
int found_); int found_);
...@@ -103,6 +114,15 @@ class socket_poller_t ...@@ -103,6 +114,15 @@ class socket_poller_t
uint64_t &now_, uint64_t &now_,
uint64_t &end_, uint64_t &end_,
bool &first_pass_); bool &first_pass_);
static bool is_socket (const item_t &item, const socket_base_t *socket_)
{
return item.socket == socket_;
}
static bool is_fd (const item_t &item, fd_t fd_)
{
return !item.socket && item.fd == fd_;
}
int rebuild (); int rebuild ();
// Used to check whether the object is a socket_poller. // Used to check whether the object is a socket_poller.
...@@ -111,17 +131,6 @@ class socket_poller_t ...@@ -111,17 +131,6 @@ class socket_poller_t
// Signaler used for thread safe sockets polling // Signaler used for thread safe sockets polling
signaler_t *_signaler; signaler_t *_signaler;
typedef struct item_t
{
socket_base_t *socket;
fd_t fd;
void *user_data;
short events;
#if defined ZMQ_POLL_BASED_ON_POLL
int pollfd_index;
#endif
} item_t;
// List of sockets // List of sockets
typedef std::vector<item_t> items_t; typedef std::vector<item_t> items_t;
items_t _items; items_t _items;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment