3 //=============================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //=============================================================================
11 #ifndef ACE_HANDLE_SET_H
12 #define ACE_HANDLE_SET_H
13 #include /**/ "ace/pre.h"
15 #include /**/ "ace/ACE_export.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/os_include/sys/os_select.h"
22 #include "ace/os_include/os_limits.h"
24 // Default size of the ACE Reactor.
25 #if defined (FD_SETSIZE)
26 int const ACE_FD_SETSIZE
= FD_SETSIZE
;
27 #else /* !FD_SETSIZE */
28 # define ACE_FD_SETSIZE FD_SETSIZE
29 #endif /* ACE_FD_SETSIZE */
31 #if defined(FD_SETSIZE) && defined(__FD_SETSIZE) && (FD_SETSIZE > __FD_SETSIZE)
32 #error FD_SETSIZE definition is too large, please correct!
35 #if !defined (ACE_DEFAULT_SELECT_REACTOR_SIZE)
36 # define ACE_DEFAULT_SELECT_REACTOR_SIZE ACE_FD_SETSIZE
37 #endif /* ACE_DEFAULT_SELECT_REACTOR_SIZE */
39 #if defined (ACE_WIN32) || defined (ACE_MQX)
40 # define ACE_HANDLE_SET_USES_FD_ARRAY
43 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
46 * @class ACE_Handle_Set
48 * @brief C++ wrapper facade for the socket @c fd_set abstraction.
50 * This abstraction is a very efficient wrapper facade over
51 * @c fd_set. In particular, no range checking is performed, so
52 * it's important not to set or clear bits that are outside the
53 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
55 class ACE_Export ACE_Handle_Set
58 friend class ACE_Handle_Set_Iterator
;
62 MAXSIZE
= ACE_DEFAULT_SELECT_REACTOR_SIZE
65 /// Constructor, initializes the bitmask to all 0s.
67 ~ACE_Handle_Set () = default;
70 * Constructor, initializes the handle set from a given mask.
72 ACE_Handle_Set (const fd_set
&mask
);
74 // = Methods for manipulating bitsets.
75 /// Initialize the bitmask to all 0s and reset the associated fields.
79 * Checks whether @a handle is enabled. No range checking is
80 * performed so @a handle must be less than
81 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
83 int is_set (ACE_HANDLE handle
) const;
85 /// Enables the @a handle. No range checking is performed so @a handle
86 /// must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
87 void set_bit (ACE_HANDLE handle
);
89 /// Disables the @a handle. No range checking is performed so
90 /// @a handle must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
91 void clr_bit (ACE_HANDLE handle
);
93 /// Returns a count of the number of enabled bits.
96 /// Returns the number of the large bit.
97 ACE_HANDLE
max_set () const;
100 * Rescan the underlying @c fd_set up to handle @a max to find the new
101 * <max_handle> (highest bit set) and <size> (how many bits set) values.
102 * This is useful for evaluating the changes after the handle set has
103 * been manipulated in some way other than member functions; for example,
104 * after <select> modifies the @c fd_set.
106 void sync (ACE_HANDLE max
);
108 /// Returns a pointer to the underlying @c fd_set. Returns 0 if
109 /// there are no handle bits set (<size_> == 0).
112 /// Returns a pointer to the underlying @c fd_set. Returns 0 if
113 /// there are no handle bits set (<size_> == 0).
116 #if defined (ACE_HAS_BIG_FD_SET)
117 /// Assignment operator optimizes for cases where size_ == 0.
118 ACE_Handle_Set
& operator= (const ACE_Handle_Set
&rhs
);
119 /// Copy constructor optimizes for cases where size_ == 0
120 ACE_Handle_Set (const ACE_Handle_Set
&rhs
);
122 ACE_Handle_Set
& operator= (const ACE_Handle_Set
&) = default;
123 ACE_Handle_Set (const ACE_Handle_Set
&) = default;
124 #endif /* ACE_HAS_BIG_FD_SET */
126 ACE_Handle_Set
& operator= (ACE_Handle_Set
&&) = default;
127 ACE_Handle_Set (ACE_Handle_Set
&&) = default;
129 /// Dump the state of an object.
132 /// Declare the dynamic allocation hooks.
133 ACE_ALLOC_HOOK_DECLARE
;
136 /// Size of the set, i.e., a count of the number of enabled bits.
139 /// Current max handle.
140 ACE_HANDLE max_handle_
;
142 #if defined (ACE_HAS_BIG_FD_SET)
143 /// Current min handle.
144 ACE_HANDLE min_handle_
;
145 #endif /* ACE_HAS_BIG_FD_SET */
153 #if !defined (ACE_HANDLE_SET_USES_FD_ARRAY)
154 NUM_WORDS
= howmany (MAXSIZE
, NFDBITS
),
155 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
159 /// Counts the number of bits enabled in N. Uses a table lookup to
160 /// speed up the count.
161 static int count_bits (u_long n
);
163 #if defined (ACE_HAS_BIG_FD_SET)
164 /// Find the position of the bit counting from right to left.
165 static int bitpos (u_long bit
);
166 #endif /* ACE_HAS_BIG_FD_SET */
168 /// Resets the <max_handle_> after a clear of the original
170 void set_max (ACE_HANDLE max
);
172 /// Table that maps bytes to counts of the enabled bits in each value
174 static const char nbits_
[NBITS
];
178 * @class ACE_Handle_Set_Iterator
180 * @brief Iterator for the ACE_Handle_Set abstraction.
182 class ACE_Export ACE_Handle_Set_Iterator
186 ACE_Handle_Set_Iterator (const ACE_Handle_Set
&hs
);
187 ACE_Handle_Set_Iterator () = delete;
188 ~ACE_Handle_Set_Iterator () = default;
190 /// Reset the state of the iterator by reinitializing the state
191 /// that we maintain.
195 * "Next" operator. Returns the next unseen ACE_HANDLE in the
196 * <Handle_Set> up to <handle_set_.max_handle_>). When all the
197 * handles have been seen returns <ACE_INVALID_HANDLE>. Advances
198 * the iterator automatically, so you need not call <operator++>
199 * (which is now obsolete).
201 ACE_HANDLE
operator () ();
203 /// Dump the state of an object.
206 /// Declare the dynamic allocation hooks.
207 ACE_ALLOC_HOOK_DECLARE
;
210 /// The Handle_Set we are iterating through.
211 const ACE_Handle_Set
&handles_
;
213 /// Index of the bit we're examining in the current word_num_() word.
214 #if defined (ACE_HANDLE_SET_USES_FD_ARRAY)
216 #elif !defined (ACE_HAS_BIG_FD_SET)
218 #elif defined (ACE_HAS_BIG_FD_SET)
221 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
223 /// Number of the word we're iterating over (typically between 0..7).
226 #if defined (ACE_HAS_BIG_FD_SET)
227 /// Number max of the words with a possible bit on.
229 #endif /* ACE_HAS_BIG_FD_SET */
231 #if !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && !defined (ACE_HAS_BIG_FD_SET)
232 /// Value of the bits in the word we're iterating on.
234 #elif !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && defined (ACE_HAS_BIG_FD_SET)
235 /// Value of the bits in the word we're iterating on.
237 #endif /* !ACE_HANDLE_SET_USES_FD_ARRAY && !ACE_HAS_BIG_FD_SET */
240 ACE_END_VERSIONED_NAMESPACE_DECL
242 #if defined (__ACE_INLINE__)
243 #include "ace/Handle_Set.inl"
244 #endif /* __ACE_INLINE__ */
246 #include /**/ "ace/post.h"
247 #endif /* ACE_HANDLE_SET */