Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Handle_Set.h
blob43fa04c157a8fe23c7a2739f14338722a9b02d7b
1 /* -*- C++ -*- */
3 //=============================================================================
4 /**
5 * @file Handle_Set.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
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)
18 # 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!
33 #endif
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
41 #endif
43 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
45 /**
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
57 public:
58 friend class ACE_Handle_Set_Iterator;
60 enum
62 MAXSIZE = ACE_DEFAULT_SELECT_REACTOR_SIZE
65 /// Constructor, initializes the bitmask to all 0s.
66 ACE_Handle_Set ();
67 ~ACE_Handle_Set () = default;
69 /**
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.
76 void reset ();
78 /**
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.
94 int num_set () const;
96 /// Returns the number of the large bit.
97 ACE_HANDLE max_set () const;
99 /**
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).
110 operator fd_set *();
112 /// Returns a pointer to the underlying @c fd_set. Returns 0 if
113 /// there are no handle bits set (<size_> == 0).
114 fd_set *fdset ();
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);
121 #else
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.
130 void dump () const;
132 /// Declare the dynamic allocation hooks.
133 ACE_ALLOC_HOOK_DECLARE;
135 private:
136 /// Size of the set, i.e., a count of the number of enabled bits.
137 int size_;
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 */
147 /// Bitmask.
148 fd_set mask_;
150 enum
152 WORDSIZE = NFDBITS,
153 #if !defined (ACE_HANDLE_SET_USES_FD_ARRAY)
154 NUM_WORDS = howmany (MAXSIZE, NFDBITS),
155 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
156 NBITS = 256
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
169 /// <max_handle_>.
170 void set_max (ACE_HANDLE max);
172 /// Table that maps bytes to counts of the enabled bits in each value
173 /// from 0 to 255.
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
184 public:
185 /// Constructor.
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.
192 void reset_state ();
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.
204 void dump () const;
206 /// Declare the dynamic allocation hooks.
207 ACE_ALLOC_HOOK_DECLARE;
209 private:
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)
215 u_int handle_index_;
216 #elif !defined (ACE_HAS_BIG_FD_SET)
217 int handle_index_;
218 #elif defined (ACE_HAS_BIG_FD_SET)
219 int handle_index_;
220 u_long oldlsb_;
221 #endif /* ACE_HANDLE_SET_USES_FD_ARRAY */
223 /// Number of the word we're iterating over (typically between 0..7).
224 int word_num_;
226 #if defined (ACE_HAS_BIG_FD_SET)
227 /// Number max of the words with a possible bit on.
228 int word_max_;
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.
233 fd_mask word_val_;
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.
236 u_long word_val_;
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 */