summaryrefslogtreecommitdiff
path: root/ACE/ace/Handle_Set.h
blob: 43fa04c157a8fe23c7a2739f14338722a9b02d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* -*- C++ -*- */

//=============================================================================
/**
 *  @file    Handle_Set.h
 *
 *  @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
 */
//=============================================================================

#ifndef ACE_HANDLE_SET_H
#define ACE_HANDLE_SET_H
#include /**/ "ace/pre.h"

#include /**/ "ace/ACE_export.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/os_include/sys/os_select.h"
#include "ace/os_include/os_limits.h"

// Default size of the ACE Reactor.
#if defined (FD_SETSIZE)
   int const ACE_FD_SETSIZE = FD_SETSIZE;
#else /* !FD_SETSIZE */
#  define ACE_FD_SETSIZE FD_SETSIZE
#endif /* ACE_FD_SETSIZE */

#if defined(FD_SETSIZE) && defined(__FD_SETSIZE) && (FD_SETSIZE > __FD_SETSIZE)
#error FD_SETSIZE definition is too large, please correct!
#endif

#if !defined (ACE_DEFAULT_SELECT_REACTOR_SIZE)
#  define ACE_DEFAULT_SELECT_REACTOR_SIZE ACE_FD_SETSIZE
#endif /* ACE_DEFAULT_SELECT_REACTOR_SIZE */

#if defined (ACE_WIN32) || defined (ACE_MQX)
# define ACE_HANDLE_SET_USES_FD_ARRAY
#endif

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

/**
 * @class ACE_Handle_Set
 *
 * @brief C++ wrapper facade for the socket @c fd_set abstraction.
 *
 * This abstraction is a very efficient wrapper facade over
 * @c fd_set.  In particular, no range checking is performed, so
 * it's important not to set or clear bits that are outside the
 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
 */
class ACE_Export ACE_Handle_Set
{
public:
  friend class ACE_Handle_Set_Iterator;

  enum
  {
    MAXSIZE = ACE_DEFAULT_SELECT_REACTOR_SIZE
  };

  /// Constructor, initializes the bitmask to all 0s.
  ACE_Handle_Set ();
  ~ACE_Handle_Set () = default;

  /**
   * Constructor, initializes the handle set from a given mask.
   */
  ACE_Handle_Set (const fd_set &mask);

  // = Methods for manipulating bitsets.
  /// Initialize the bitmask to all 0s and reset the associated fields.
  void reset ();

  /**
   * Checks whether @a handle is enabled.  No range checking is
   * performed so @a handle must be less than
   * @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
   */
  int is_set (ACE_HANDLE handle) const;

  /// Enables the @a handle.  No range checking is performed so @a handle
  /// must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
  void set_bit (ACE_HANDLE handle);

  /// Disables the @a handle.  No range checking is performed so
  /// @a handle must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
  void clr_bit (ACE_HANDLE handle);

  /// Returns a count of the number of enabled bits.
  int num_set () const;

  /// Returns the number of the large bit.
  ACE_HANDLE max_set () const;

  /**
   * Rescan the underlying @c fd_set up to handle @a max to find the new
   * <max_handle> (highest bit set) and <size> (how many bits set) values.
   * This is useful for evaluating the changes after the handle set has
   * been manipulated in some way other than member functions; for example,
   * after <select> modifies the @c fd_set.
   */
  void sync (ACE_HANDLE max);

  /// Returns a pointer to the underlying @c fd_set.  Returns 0 if
  /// there are no handle bits set (<size_> == 0).
  operator fd_set *();

  /// Returns a pointer to the underlying @c fd_set.  Returns 0 if
  /// there are no handle bits set (<size_> == 0).
  fd_set *fdset ();

#if defined (ACE_HAS_BIG_FD_SET)
  /// Assignment operator optimizes for cases where size_ == 0.
  ACE_Handle_Set & operator= (const ACE_Handle_Set &rhs);
  /// Copy constructor optimizes for cases where size_ == 0
  ACE_Handle_Set (const ACE_Handle_Set &rhs);
#else
  ACE_Handle_Set & operator= (const ACE_Handle_Set &) = default;
  ACE_Handle_Set (const ACE_Handle_Set &) = default;
#endif /* ACE_HAS_BIG_FD_SET */

  ACE_Handle_Set & operator= (ACE_Handle_Set &&) = default;
  ACE_Handle_Set (ACE_Handle_Set &&) = default;

  /// Dump the state of an object.
  void dump () const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

private:
  /// Size of the set, i.e., a count of the number of enabled bits.
  int size_;

  /// Current max handle.
  ACE_HANDLE max_handle_;

#if defined (ACE_HAS_BIG_FD_SET)
  /// Current min handle.
  ACE_HANDLE min_handle_;
#endif /* ACE_HAS_BIG_FD_SET */

  /// Bitmask.
  fd_set mask_;

  enum
  {
    WORDSIZE = NFDBITS,
#if !defined (ACE_HANDLE_SET_USES_FD_ARRAY)
    NUM_WORDS = howmany (MAXSIZE, NFDBITS),
#endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
    NBITS = 256
  };

  /// Counts the number of bits enabled in N.  Uses a table lookup to
  /// speed up the count.
  static int count_bits (u_long n);

#if defined (ACE_HAS_BIG_FD_SET)
  /// Find the position of the bit counting from right to left.
  static int bitpos (u_long bit);
#endif /* ACE_HAS_BIG_FD_SET */

  /// Resets the <max_handle_> after a clear of the original
  /// <max_handle_>.
  void set_max (ACE_HANDLE max);

  /// Table that maps bytes to counts of the enabled bits in each value
  /// from 0 to 255.
  static const char nbits_[NBITS];
};

/**
 * @class ACE_Handle_Set_Iterator
 *
 * @brief Iterator for the ACE_Handle_Set abstraction.
 */
class ACE_Export ACE_Handle_Set_Iterator
{
public:
  /// Constructor.
  ACE_Handle_Set_Iterator (const ACE_Handle_Set &hs);
  ACE_Handle_Set_Iterator () = delete;
  ~ACE_Handle_Set_Iterator () = default;

  /// Reset the state of the iterator by reinitializing the state
  /// that we maintain.
  void reset_state ();

  /**
   * "Next" operator.  Returns the next unseen ACE_HANDLE in the
   * <Handle_Set> up to <handle_set_.max_handle_>).  When all the
   * handles have been seen returns <ACE_INVALID_HANDLE>.  Advances
   * the iterator automatically, so you need not call <operator++>
   * (which is now obsolete).
   */
  ACE_HANDLE operator () ();

  /// Dump the state of an object.
  void dump () const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

private:
  /// The Handle_Set we are iterating through.
  const ACE_Handle_Set &handles_;

  /// Index of the bit we're examining in the current word_num_() word.
#if defined (ACE_HANDLE_SET_USES_FD_ARRAY)
  u_int handle_index_;
#elif !defined (ACE_HAS_BIG_FD_SET)
  int handle_index_;
#elif defined (ACE_HAS_BIG_FD_SET)
  int handle_index_;
  u_long oldlsb_;
#endif /* ACE_HANDLE_SET_USES_FD_ARRAY */

  /// Number of the word we're iterating over (typically between 0..7).
  int word_num_;

#if defined (ACE_HAS_BIG_FD_SET)
  /// Number max of the words with a possible bit on.
  int word_max_;
#endif /* ACE_HAS_BIG_FD_SET */

#if !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && !defined (ACE_HAS_BIG_FD_SET)
  /// Value of the bits in the word we're iterating on.
  fd_mask word_val_;
#elif !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && defined (ACE_HAS_BIG_FD_SET)
  /// Value of the bits in the word we're iterating on.
  u_long word_val_;
#endif /* !ACE_HANDLE_SET_USES_FD_ARRAY && !ACE_HAS_BIG_FD_SET */
};

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Handle_Set.inl"
#endif /* __ACE_INLINE__ */

#include /**/ "ace/post.h"
#endif /* ACE_HANDLE_SET */