Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFileDescriptorMask.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JFILEDESCRIPTORMASK__
2 #define __JLANG__JFILEDESCRIPTORMASK__
3 
4 #include <sys/select.h>
5 
6 #include "JLang/JAbstractFile.hh"
7 #include "JLang/JTimeval.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19 
20  /**
21  * Auxiliary class for method select.
22  * This class encapsulates the <tt>fd_set</tT> data structure.
23  */
25  protected fd_set
26  {
27  public:
28 
29 
30  static const int MAXIMUM_FILE_DESCRIPTOR = FD_SETSIZE;
31 
32 
33  /**
34  * Default constructor.
35  */
39  {
40  FD_ZERO(get());
41  }
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param file file descriptor
48  */
52  {
53  FD_ZERO(get());
54 
55  set(file);
56  }
57 
58 
59  /**
60  * Constructor.
61  *
62  * \param file_descriptor file descriptor
63  */
64  JFileDescriptorMask(const int file_descriptor) :
67  {
68  FD_ZERO(get());
69 
70  set(file_descriptor);
71  }
72 
73 
74  /**
75  * Get pointer to mask.
76  *
77  * \return pointer to mask
78  */
79  const fd_set* get() const
80  {
81  return static_cast<const fd_set*>(this);
82  }
83 
84 
85  /**
86  * Get pointer to mask.
87  *
88  * \return pointer to mask
89  */
90  fd_set* get()
91  {
92  return static_cast<fd_set*>(this);
93  }
94 
95 
96  /**
97  * Address of operator.
98  *
99  * \return pointer to mask
100  */
101  const fd_set* operator &() const
102  {
103  return get();
104  }
105 
106 
107  /**
108  * Address of operator.
109  *
110  * \return pointer to mask
111  */
112  fd_set* operator &()
113  {
114  return get();
115  }
116 
117 
118  /**
119  * Reset mask.
120  * A hard reset causes the reset of the complete mask whereas
121  * a soft reset causes the reset of the internal parameters only.
122  *
123  * \param option true hard reset; else soft reset
124  */
125  void reset(const bool option = true)
126  {
127  if (option) {
128 
129  switch (number_of_file_descriptors) {
130 
131  case 0:
132  break;
133 
134  case 1:
135  FD_CLR(maximum_file_descriptor, get());
136  break;
137 
138  default:
139  FD_ZERO(get());
140  break;
141  }
142  }
143 
146  }
147 
148 
149  /**
150  * Set file descriptor.
151  *
152  * \param file_descriptor file descriptor
153  */
154  void set(const int file_descriptor)
155  {
156  if (!has(file_descriptor)) {
157 
158  FD_SET(file_descriptor, get());
159 
160  if (file_descriptor > maximum_file_descriptor) {
161  maximum_file_descriptor = file_descriptor;
162  }
163 
165  }
166  }
167 
168 
169  /**
170  * Set file.
171  *
172  * \param file file
173  */
174  void set(const JAbstractFile& file)
175  {
176  set(file.getFileDescriptor());
177  }
178 
179 
180  /**
181  * Reset file descriptor.
182  *
183  * \param file_descriptor file descriptor
184  */
185  void reset(const int file_descriptor)
186  {
187  if (has(file_descriptor)) {
188 
189  FD_CLR(file_descriptor, get());
190 
193 
195  }
196  }
197 
198 
199  /**
200  * Reset file.
201  *
202  * \param file file
203  */
204  void reset(const JAbstractFile& file)
205  {
206  reset(file.getFileDescriptor());
207  }
208 
209 
210  /**
211  * Has file descriptor.
212  *
213  * \param file_descriptor file descriptor
214  * \return true if file descriptor set; else false
215  */
216  bool has(const int file_descriptor) const
217  {
218  return FD_ISSET(file_descriptor, get());
219  }
220 
221 
222  /**
223  * Has file.
224  *
225  * \param file file
226  * \return true if file set; else false
227  */
228  bool has(const JAbstractFile& file) const
229  {
230  return has(file.getFileDescriptor());
231  }
232 
233 
234  /**
235  * Get number of file descriptors.
236  *
237  * \return number of file descriptors
238  */
240  {
242  }
243 
244 
245  /**
246  * Check setting of file descriptors.
247  *
248  * \return true if no file descriptors are set; else false
249  */
250  bool empty() const
251  {
252  return number_of_file_descriptors == 0;
253  }
254 
255 
256  /**
257  * Check availability of input.
258  * This method returns true is at least one byte can be read without blocking.
259  * Following a select() call, this method overwrites the internal mask!
260  *
261  * \param timeout timeout
262  * \return true if ready to read; else false
263  */
264  bool in_avail(JTimeval timeout = JTimeval::min())
265  {
266  return ::select(getNumberOfFileDescriptors() + 1, get(), NULL, NULL, &timeout) > 0;
267  }
268 
269 
270  /**
271  * Check availability of output.
272  * This method returns true is at least one byte can be written without blocking.
273  * Following a select() call, this method overwrites the internal mask!
274  *
275  * \param timeout timeout
276  * \return true if ready to write; else false
277  */
278  bool out_avail(JTimeval timeout = JTimeval::min())
279  {
280  return ::select(getNumberOfFileDescriptors() + 1, NULL, get(), NULL, &timeout) > 0;
281  }
282 
283 
284  private:
287  };
288 }
289 
290 #endif
bool has(const JAbstractFile &file) const
Has file.
bool empty() const
Check setting of file descriptors.
bool has(const int file_descriptor) const
Has file descriptor.
void set(const int file_descriptor)
Set file descriptor.
static JTimeval min()
Get minimal time value.
Definition: JTimeval.hh:119
void reset(const int file_descriptor)
Reset file descriptor.
static const int MAXIMUM_FILE_DESCRIPTOR
Auxiliary class for time values.
Definition: JTimeval.hh:26
int getFileDescriptor() const
Get file descriptor.
void reset(const JAbstractFile &file)
Reset file.
bool out_avail(JTimeval timeout=JTimeval::min())
Check availability of output.
const fd_set * operator&() const
Address of operator.
void reset(const bool option=true)
Reset mask.
Auxiliary class for method select.
JFileDescriptorMask(const int file_descriptor)
Constructor.
The JAbstractFile class encapsulates the c-style file descriptor.
int getNumberOfFileDescriptors() const
Get number of file descriptors.
JFileDescriptorMask()
Default constructor.
JFileDescriptorMask(const JAbstractFile &file)
Constructor.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
void set(const JAbstractFile &file)
Set file.