cheshirekow  v0.1.0
select_set.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (josh.bialkowski@gmail.com)
3  *
4  * This file is part of cpp-nix.
5  *
6  * cpp-nix is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * cpp-nix is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cpp-nix. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPP_NIX_SELECT_SET_H_
28 #define CPP_NIX_SELECT_SET_H_
29 
30 #include <sys/select.h>
31 #include <map>
32 
33 
34 namespace nix {
35 
36 enum {
37  SELECT_READ = 0x01,
38  SELECT_WRITE = 0x02,
40 };
41 
42 
43 typedef std::map<int, int> SelectMap;
44 
46  private:
48  int m_fd;
49 
50  public:
51  SelectSurrogate(SelectMap* select_map, int fd)
52  : m_select_map(select_map),
53  m_fd(fd) {
54  }
55 
56  operator int() {
57  return (*m_select_map)[m_fd];
58  }
59 
60  const SelectSurrogate& operator=(int spec) const {
61  if ((spec & (SELECT_READ | SELECT_WRITE | SELECT_EXCEPT)) == 0) {
62  SelectMap::iterator fd_iter = m_select_map->find(m_fd);
63  if (fd_iter != m_select_map->end()) {
64  m_select_map->erase(fd_iter);
65  }
66  } else {
67  (*m_select_map)[m_fd] = spec;
68  }
69  return *this;
70  }
71 };
72 
73 class SelectSet {
74  public:
76  return SelectSurrogate(&m_map, fd);
77  }
78 
79  int BuildSets(fd_set* read_set, fd_set* write_set, fd_set* except_set) const;
80 
81  int Select(fd_set* read_set, fd_set* write_set, fd_set* except_set,
82  timeval* timeout) const;
83 
84  int Pselect(fd_set* read_set, fd_set* write_set, fd_set* except_set,
85  timespec* timeout, sigset_t* sigmask) const;
86 
87  private:
89 };
90 
91 } // namespace nix
92 
93 #endif
int BuildSets(fd_set *read_set, fd_set *write_set, fd_set *except_set) const
std::map< int, int > SelectMap
Definition: select_set.h:43
SelectMap * m_select_map
Definition: select_set.h:47
int Pselect(fd_set *read_set, fd_set *write_set, fd_set *except_set, timespec *timeout, sigset_t *sigmask) const
int Select(fd_set *read_set, fd_set *write_set, fd_set *except_set, timeval *timeout) const
SelectSurrogate operator[](int fd)
Definition: select_set.h:75
SelectMap m_map
Definition: select_set.h:88
SelectSurrogate(SelectMap *select_map, int fd)
Definition: select_set.h:51
const SelectSurrogate & operator=(int spec) const
Definition: select_set.h:60