00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file Handle_Set.h 00006 * 00007 * $Id: Handle_Set.h 80826 2008-03-04 14:51:23Z wotte $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_HANDLE_SET_H 00014 #define ACE_HANDLE_SET_H 00015 #include /**/ "ace/pre.h" 00016 00017 #include /**/ "ace/ACE_export.h" 00018 00019 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00020 # pragma once 00021 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00022 00023 #include "ace/os_include/sys/os_select.h" 00024 #include "ace/os_include/os_limits.h" 00025 00026 #if defined (__QNX__) 00027 typedef long fd_mask; 00028 #endif /* __QNX__ */ 00029 00030 // Default size of the ACE Reactor. 00031 #if defined (FD_SETSIZE) 00032 int const ACE_FD_SETSIZE = FD_SETSIZE; 00033 #else /* !FD_SETSIZE */ 00034 # define ACE_FD_SETSIZE FD_SETSIZE 00035 #endif /* ACE_FD_SETSIZE */ 00036 00037 #if !defined (ACE_DEFAULT_SELECT_REACTOR_SIZE) 00038 # define ACE_DEFAULT_SELECT_REACTOR_SIZE ACE_FD_SETSIZE 00039 #endif /* ACE_DEFAULT_SELECT_REACTOR_SIZE */ 00040 00041 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00042 00043 /** 00044 * @class ACE_Handle_Set 00045 * 00046 * @brief C++ wrapper facade for the socket @c fd_set abstraction. 00047 * 00048 * This abstraction is a very efficient wrapper facade over 00049 * @c fd_set. In particular, no range checking is performed, so 00050 * it's important not to set or clear bits that are outside the 00051 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE. 00052 */ 00053 class ACE_Export ACE_Handle_Set 00054 { 00055 public: 00056 friend class ACE_Handle_Set_Iterator; 00057 00058 // = Initialization and termination. 00059 00060 enum 00061 { 00062 MAXSIZE = ACE_DEFAULT_SELECT_REACTOR_SIZE 00063 }; 00064 00065 // = Initialization methods. 00066 /// Constructor, initializes the bitmask to all 0s. 00067 ACE_Handle_Set (void); 00068 00069 /** 00070 * Constructor, initializes the handle set from a given mask. 00071 */ 00072 ACE_Handle_Set (const fd_set &mask); 00073 00074 // = Methods for manipulating bitsets. 00075 /// Initialize the bitmask to all 0s and reset the associated fields. 00076 void reset (void); 00077 00078 /** 00079 * Checks whether @a handle is enabled. No range checking is 00080 * performed so @a handle must be less than 00081 * @c ACE_DEFAULT_SELECT_REACTOR_SIZE. 00082 */ 00083 int is_set (ACE_HANDLE handle) const; 00084 00085 /// Enables the @a handle. No range checking is performed so @a handle 00086 /// must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE. 00087 void set_bit (ACE_HANDLE handle); 00088 00089 /// Disables the @a handle. No range checking is performed so 00090 /// @a handle must be less than @c ACE_DEFAULT_SELECT_REACTOR_SIZE. 00091 void clr_bit (ACE_HANDLE handle); 00092 00093 /// Returns a count of the number of enabled bits. 00094 int num_set (void) const; 00095 00096 /// Returns the number of the large bit. 00097 ACE_HANDLE max_set (void) const; 00098 00099 /** 00100 * Rescan the underlying @c fd_set up to handle @a max to find the new 00101 * <max_handle> (highest bit set) and <size> (how many bits set) values. 00102 * This is useful for evaluating the changes after the handle set has 00103 * been manipulated in some way other than member functions; for example, 00104 * after <select> modifies the @c fd_set. 00105 */ 00106 void sync (ACE_HANDLE max); 00107 00108 /// Returns a pointer to the underlying @c fd_set. Returns 0 if 00109 /// there are no handle bits set (<size_> == 0). 00110 operator fd_set *(); 00111 00112 /// Returns a pointer to the underlying @c fd_set. Returns 0 if 00113 /// there are no handle bits set (<size_> == 0). 00114 fd_set *fdset (void); 00115 00116 #if defined (ACE_HAS_BIG_FD_SET) 00117 /// Assignment operator optimizes for cases where <size_> == 0. 00118 ACE_Handle_Set & operator= (const ACE_Handle_Set &); 00119 #endif /* ACE_HAS_BIG_FD_SET */ 00120 00121 /// Dump the state of an object. 00122 void dump (void) const; 00123 00124 /// Declare the dynamic allocation hooks. 00125 ACE_ALLOC_HOOK_DECLARE; 00126 00127 private: 00128 /// Size of the set, i.e., a count of the number of enabled bits. 00129 int size_; 00130 00131 /// Current max handle. 00132 ACE_HANDLE max_handle_; 00133 00134 #if defined (ACE_HAS_BIG_FD_SET) 00135 /// Current min handle. 00136 ACE_HANDLE min_handle_; 00137 #endif /* ACE_HAS_BIG_FD_SET */ 00138 00139 /// Bitmask. 00140 fd_set mask_; 00141 00142 enum 00143 { 00144 WORDSIZE = NFDBITS, 00145 #if !defined (ACE_WIN32) 00146 NUM_WORDS = howmany (MAXSIZE, NFDBITS), 00147 #endif /* ACE_WIN32 */ 00148 NBITS = 256 00149 }; 00150 00151 /// Counts the number of bits enabled in N. Uses a table lookup to 00152 /// speed up the count. 00153 static int count_bits (u_long n); 00154 00155 #if defined (ACE_HAS_BIG_FD_SET) 00156 /// Find the position of the bit counting from right to left. 00157 static int bitpos (u_long bit); 00158 #endif /* ACE_HAS_BIG_FD_SET */ 00159 00160 /// Resets the <max_handle_> after a clear of the original 00161 /// <max_handle_>. 00162 void set_max (ACE_HANDLE max); 00163 00164 /// Table that maps bytes to counts of the enabled bits in each value 00165 /// from 0 to 255. 00166 static const char nbits_[NBITS]; 00167 }; 00168 00169 /** 00170 * @class ACE_Handle_Set_Iterator 00171 * 00172 * @brief Iterator for the ACE_Handle_Set abstraction. 00173 */ 00174 class ACE_Export ACE_Handle_Set_Iterator 00175 { 00176 public: 00177 /// Constructor. 00178 ACE_Handle_Set_Iterator (const ACE_Handle_Set &hs); 00179 00180 /// Default dtor. 00181 ~ACE_Handle_Set_Iterator (void); 00182 00183 /// Reset the state of the iterator by reinitializing the state 00184 /// that we maintain. 00185 void reset_state (void); 00186 00187 /** 00188 * "Next" operator. Returns the next unseen ACE_HANDLE in the 00189 * <Handle_Set> up to <handle_set_.max_handle_>). When all the 00190 * handles have been seen returns <ACE_INVALID_HANDLE>. Advances 00191 * the iterator automatically, so you need not call <operator++> 00192 * (which is now obsolete). 00193 */ 00194 ACE_HANDLE operator () (void); 00195 00196 /// Dump the state of an object. 00197 void dump (void) const; 00198 00199 /// Declare the dynamic allocation hooks. 00200 ACE_ALLOC_HOOK_DECLARE; 00201 00202 private: 00203 /// The <Handle_Set> we are iterating through. 00204 const ACE_Handle_Set &handles_; 00205 00206 /// Index of the bit we're examining in the current <word_num_> word. 00207 #if defined (ACE_WIN32) 00208 u_int handle_index_; 00209 #elif !defined (ACE_HAS_BIG_FD_SET) 00210 int handle_index_; 00211 #elif defined (ACE_HAS_BIG_FD_SET) 00212 int handle_index_; 00213 u_long oldlsb_; 00214 #endif /* ACE_WIN32 */ 00215 00216 /// Number of the word we're iterating over (typically between 0..7). 00217 int word_num_; 00218 00219 #if defined (ACE_HAS_BIG_FD_SET) 00220 /// Number max of the words with a possible bit on. 00221 int word_max_; 00222 #endif /* ACE_HAS_BIG_FD_SET */ 00223 00224 #if !defined (ACE_WIN32) && !defined (ACE_HAS_BIG_FD_SET) 00225 /// Value of the bits in the word we're iterating on. 00226 fd_mask word_val_; 00227 #elif !defined (ACE_WIN32) && defined (ACE_HAS_BIG_FD_SET) 00228 /// Value of the bits in the word we're iterating on. 00229 u_long word_val_; 00230 #endif /* !ACE_WIN32 && !ACE_HAS_BIG_FD_SET */ 00231 }; 00232 00233 ACE_END_VERSIONED_NAMESPACE_DECL 00234 00235 #if defined (__ACE_INLINE__) 00236 #include "ace/Handle_Set.inl" 00237 #endif /* __ACE_INLINE__ */ 00238 00239 #include /**/ "ace/post.h" 00240 #endif /* ACE_HANDLE_SET */