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.
66 ACE_Handle_Set (void);
69 * Constructor, initializes the handle set from a given mask.
71 ACE_Handle_Set (const fd_set
&mask
);
73 // = Methods for manipulating bitsets.
74 /// Initialize the bitmask to all 0s and reset the associated fields.
78 * Checks whether @a handle is enabled. No range checking is
79 * performed so @a handle must be less than
80 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
82 int is_set (ACE_HANDLE handle
) const;
84 /// Enables the @a handle. No range checking is performed so @a handle
85 /// must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
86 void set_bit (ACE_HANDLE handle
);
88 /// Disables the @a handle. No range checking is performed so
89 /// @a handle must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE.
90 void clr_bit (ACE_HANDLE handle
);
92 /// Returns a count of the number of enabled bits.
93 int num_set (void) const;
95 /// Returns the number of the large bit.
96 ACE_HANDLE
max_set (void) const;
99 * Rescan the underlying @c fd_set up to handle @a max to find the new
100 * <max_handle> (highest bit set) and <size> (how many bits set) values.
101 * This is useful for evaluating the changes after the handle set has
102 * been manipulated in some way other than member functions; for example,
103 * after <select> modifies the @c fd_set.
105 void sync (ACE_HANDLE max
);
107 /// Returns a pointer to the underlying @c fd_set. Returns 0 if
108 /// there are no handle bits set (<size_> == 0).
111 /// Returns a pointer to the underlying @c fd_set. Returns 0 if
112 /// there are no handle bits set (<size_> == 0).
113 fd_set
*fdset (void);
115 #if defined (ACE_HAS_BIG_FD_SET)
116 /// Assignment operator optimizes for cases where <size_> == 0.
117 ACE_Handle_Set
& operator= (const ACE_Handle_Set
&);
118 #endif /* ACE_HAS_BIG_FD_SET */
120 /// Dump the state of an object.
121 void dump (void) const;
123 /// Declare the dynamic allocation hooks.
124 ACE_ALLOC_HOOK_DECLARE
;
127 /// Size of the set, i.e., a count of the number of enabled bits.
130 /// Current max handle.
131 ACE_HANDLE max_handle_
;
133 #if defined (ACE_HAS_BIG_FD_SET)
134 /// Current min handle.
135 ACE_HANDLE min_handle_
;
136 #endif /* ACE_HAS_BIG_FD_SET */
144 #if !defined (ACE_HANDLE_SET_USES_FD_ARRAY)
145 NUM_WORDS
= howmany (MAXSIZE
, NFDBITS
),
146 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
150 /// Counts the number of bits enabled in N. Uses a table lookup to
151 /// speed up the count.
152 static int count_bits (u_long n
);
154 #if defined (ACE_HAS_BIG_FD_SET)
155 /// Find the position of the bit counting from right to left.
156 static int bitpos (u_long bit
);
157 #endif /* ACE_HAS_BIG_FD_SET */
159 /// Resets the <max_handle_> after a clear of the original
161 void set_max (ACE_HANDLE max
);
163 /// Table that maps bytes to counts of the enabled bits in each value
165 static const char nbits_
[NBITS
];
169 * @class ACE_Handle_Set_Iterator
171 * @brief Iterator for the ACE_Handle_Set abstraction.
173 class ACE_Export ACE_Handle_Set_Iterator
177 ACE_Handle_Set_Iterator (const ACE_Handle_Set
&hs
);
180 ~ACE_Handle_Set_Iterator (void);
182 /// Reset the state of the iterator by reinitializing the state
183 /// that we maintain.
184 void reset_state (void);
187 * "Next" operator. Returns the next unseen ACE_HANDLE in the
188 * <Handle_Set> up to <handle_set_.max_handle_>). When all the
189 * handles have been seen returns <ACE_INVALID_HANDLE>. Advances
190 * the iterator automatically, so you need not call <operator++>
191 * (which is now obsolete).
193 ACE_HANDLE
operator () (void);
195 /// Dump the state of an object.
196 void dump (void) const;
198 /// Declare the dynamic allocation hooks.
199 ACE_ALLOC_HOOK_DECLARE
;
202 /// The Handle_Set we are iterating through.
203 const ACE_Handle_Set
&handles_
;
205 /// Index of the bit we're examining in the current word_num_() word.
206 #if defined (ACE_HANDLE_SET_USES_FD_ARRAY)
208 #elif !defined (ACE_HAS_BIG_FD_SET)
210 #elif defined (ACE_HAS_BIG_FD_SET)
213 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
215 /// Number of the word we're iterating over (typically between 0..7).
218 #if defined (ACE_HAS_BIG_FD_SET)
219 /// Number max of the words with a possible bit on.
221 #endif /* ACE_HAS_BIG_FD_SET */
223 #if !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && !defined (ACE_HAS_BIG_FD_SET)
224 /// Value of the bits in the word we're iterating on.
226 #elif !defined (ACE_HANDLE_SET_USES_FD_ARRAY) && defined (ACE_HAS_BIG_FD_SET)
227 /// Value of the bits in the word we're iterating on.
229 #endif /* !ACE_HANDLE_SET_USES_FD_ARRAY && !ACE_HAS_BIG_FD_SET */
232 ACE_END_VERSIONED_NAMESPACE_DECL
234 #if defined (__ACE_INLINE__)
235 #include "ace/Handle_Set.inl"
236 #endif /* __ACE_INLINE__ */
238 #include /**/ "ace/post.h"
239 #endif /* ACE_HANDLE_SET */