Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Handle_Set.h

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

Generated on Mon Jun 16 11:19:45 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002