Public Member Functions | Public Attributes | Private Attributes

ACE_Handle_Set_Iterator Class Reference

Iterator for the ACE_Handle_Set abstraction. More...

#include <Handle_Set.h>

Collaboration diagram for ACE_Handle_Set_Iterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ACE_Handle_Set_Iterator (const ACE_Handle_Set &hs)
 Constructor.
 ~ACE_Handle_Set_Iterator (void)
 Default dtor.
void reset_state (void)
ACE_HANDLE operator() (void)
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Private Attributes

const ACE_Handle_Sethandles_
 The <Handle_Set> we are iterating through.
int handle_index_
 Index of the bit we're examining in the current <word_num_> word.
int word_num_
 Number of the word we're iterating over (typically between 0..7).
fd_mask word_val_
 Value of the bits in the word we're iterating on.

Detailed Description

Iterator for the ACE_Handle_Set abstraction.

Definition at line 170 of file Handle_Set.h.


Constructor & Destructor Documentation

ACE_Handle_Set_Iterator::ACE_Handle_Set_Iterator ( const ACE_Handle_Set hs  ) 

Constructor.

Definition at line 432 of file Handle_Set.cpp.

  : handles_ (hs),
#if !defined (ACE_HAS_BIG_FD_SET) || defined (ACE_WIN32)
    handle_index_ (0),
    word_num_ (-1)
#elif defined (ACE_HAS_BIG_FD_SET)
    oldlsb_ (0),
    word_max_ (hs.max_handle_ == ACE_INVALID_HANDLE
               ? 0
               : ((ACE_DIV_BY_WORDSIZE (hs.max_handle_)) + 1))
#endif /* ACE_HAS_BIG_FD_SET */
{
  ACE_TRACE ("ACE_Handle_Set_Iterator::ACE_Handle_Set_Iterator");
#if !defined (ACE_WIN32) && !defined (ACE_HAS_BIG_FD_SET)
  // No sense searching further than the max_handle_ + 1;
  ACE_HANDLE maxhandlep1 =
    this->handles_.max_handle_ + 1;

  fd_mask *maskp =
    (fd_mask *)(this->handles_.mask_.fds_bits);

  // Loop until we've found the first non-zero bit or we run past the
  // <maxhandlep1> of the bitset.
  while (this->handle_index_ < maxhandlep1
         && maskp[++this->word_num_] == 0)
    this->handle_index_ += ACE_Handle_Set::WORDSIZE;

  // If the bit index becomes >= the maxhandlep1 that means there
  // weren't any bits set.  Therefore, we'll just store the
  // maxhandlep1, which will cause <operator()> to return
  // <ACE_INVALID_HANDLE> immediately.
  if (this->handle_index_ >= maxhandlep1)
    this->handle_index_ = maxhandlep1;
  else
    // Loop until we get <word_val_> to have its least significant bit
    // enabled, keeping track of which <handle_index> this represents
    // (this information is used by <operator()>).
#if defined (ACE_TANDEM_NSK_BIT_ORDER)
    // bits are in reverse order, MSB (sign bit) = bit 0.
    for (this->word_val_ = maskp[this->word_num_];
         this->word_val_ > 0;
         this->word_val_ = (this->word_val_ << 1))
      this->handle_index_++;
#  else
    for (this->word_val_ = maskp[this->word_num_];
         ACE_BIT_DISABLED (this->word_val_, 1)
           && this->handle_index_ < maxhandlep1;
         this->handle_index_++)
      this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
#  endif /* ACE_TANDEM_NSK_BIT_ORDER */
#elif !defined (ACE_WIN32) && defined (ACE_HAS_BIG_FD_SET)
    if (this->word_max_==0)
      {
        this->word_num_ = -1;
        this->word_val_ = 0;
      }
    else
      {
        this->word_num_ =
          ACE_DIV_BY_WORDSIZE (this->handles_.min_handle_) - 1;
        this->word_val_ = 0;
      }
#endif /* !ACE_WIN32 && !ACE_HAS_BIG_FD_SET */
}

ACE_Handle_Set_Iterator::~ACE_Handle_Set_Iterator ( void   )  [inline]

Default dtor.

Definition at line 189 of file Handle_Set.inl.

{
}


Member Function Documentation

void ACE_Handle_Set_Iterator::dump ( void   )  const

Dump the state of an object.

Definition at line 260 of file Handle_Set.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Handle_Set_Iterator::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
#if defined(ACE_WIN32) || !defined(ACE_HAS_BIG_FD_SET)
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhandle_index_ = %d"), this->handle_index_));
#elif defined(ACE_HAS_BIG_FD_SET)
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_max_ = %d"), this->word_max_));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_val_ = %d"), this->word_val_));
#endif
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_num_ = %d"), this->word_num_));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

ACE_HANDLE ACE_Handle_Set_Iterator::operator() ( void   ) 

"Next" operator. Returns the next unseen ACE_HANDLE in the <Handle_Set> up to <handle_set_.max_handle_>). When all the handles have been seen returns <ACE_INVALID_HANDLE>. Advances the iterator automatically, so you need not call <operator++> (which is now obsolete).

Definition at line 278 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set_Iterator::operator");
#if defined (ACE_WIN32)
  if (this->handle_index_ < this->handles_.mask_.fd_count)
    // Return the handle and advance the iterator.
    return (ACE_HANDLE) this->handles_.mask_.fd_array[this->handle_index_++];
  else
    return ACE_INVALID_HANDLE;

#elif !defined (ACE_HAS_BIG_FD_SET) /* !ACE_WIN32 */
  // No sense searching further than the max_handle_ + 1;
  ACE_HANDLE maxhandlep1 = this->handles_.max_handle_ + 1;

  // HP-UX 11 plays some games with the fd_mask type - fd_mask is
  // defined as an int_32t, but the fds_bits is an array of longs.
  // This makes plainly indexing through the array by hand tricky,
  // since the FD_* macros treat the array as int32_t.  So the bits
  // are in the right place for int32_t, even though the array is
  // long.  This, they say, is to preserve the same in-memory layout
  // for 32-bit and 64-bit processes.  So, we play the same game as
  // the FD_* macros to get the bits right.  On all other systems,
  // this amounts to practically a NOP, since this is what would have
  // been done anyway, without all this type jazz.
  fd_mask * maskp = (fd_mask *)(this->handles_.mask_.fds_bits);

  if (this->handle_index_ >= maxhandlep1)
    // We've seen all the handles we're interested in seeing for this
    // iterator.
    return ACE_INVALID_HANDLE;
  else
    {
      ACE_HANDLE result = this->handle_index_;

      // Increment the iterator and advance to the next bit in this
      // word.
      this->handle_index_++;
#if defined (ACE_TANDEM_NSK_BIT_ORDER)
      // bits are in reverse order, MSB (sign bit) = bit 0.
      this->word_val_ = (this->word_val_ << 1);
#  else
      this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
#  endif /* ACE_TANDEM_NSK_BIT_ORDER */

      // If we've examined all the bits in this word, we'll go onto
      // the next word.

      if (this->word_val_ == 0)
        {
          // Start the handle_index_ at the beginning of the next word
          // and then loop until we've found the first non-zero bit or
          // we run past the <maxhandlep1> of the bitset.

          for (this->handle_index_ = ACE_MULT_BY_WORDSIZE(++this->word_num_);
               this->handle_index_ < maxhandlep1
                 && maskp[this->word_num_] == 0;
               this->word_num_++)
            this->handle_index_ += ACE_Handle_Set::WORDSIZE;

          // If the bit index becomes >= the maxhandlep1 that means
          // there weren't any more bits set that we want to consider.
          // Therefore, we'll just store the maxhandlep1, which will
          // cause <operator()> to return <ACE_INVALID_HANDLE>
          // immediately next time it's called.
          if (this->handle_index_ >= maxhandlep1)
            {
              this->handle_index_ = maxhandlep1;
              return result;
            }
          else
            // Load the bits of the next word.
            this->word_val_ = maskp[this->word_num_];
        }

      // Loop until we get <word_val_> to have its least significant
      // bit enabled, keeping track of which <handle_index> this
      // represents (this information is used by subsequent calls to
      // <operator()>).

#if defined (ACE_TANDEM_NSK_BIT_ORDER)
      // bits are in reverse order, MSB (sign bit) = bit 0.
      for (;
           this->word_val_ > 0;
           this->word_val_ = (this->word_val_ << 1))
        this->handle_index_++;
#  else
      for (;
           ACE_BIT_DISABLED (this->word_val_, 1);
           this->handle_index_++)
        this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
#  endif /* ACE_TANDEM_NSK_BIT_ORDER */

      return result;
    }
#else /* !ACE_HAS_BIG_FD_SET */
   // Find the first word in fds_bits with bit on
   register u_long lsb = this->word_val_;

   if (lsb == 0)
     {
       do
         {
           // We have exceeded the word count in Handle_Set?
           if (++this->word_num_ >= this->word_max_)
             return ACE_INVALID_HANDLE;

           lsb = this->handles_.mask_.fds_bits[this->word_num_];
         }
       while (lsb == 0);

       // Set index to word boundary.
       this->handle_index_ = ACE_MULT_BY_WORDSIZE (this->word_num_);

       // Put new word_val.
       this->word_val_ = lsb;

       // Find the least significative bit.
       lsb &= ~(lsb - 1);

       // Remove least significative bit.
       this->word_val_ ^= lsb;

       // Save to calculate bit distance.
       this->oldlsb_ = lsb;

       // Move index to least significative bit.
       while (lsb >>= 1)
         this->handle_index_++;
     }
   else
     {
        // Find the least significative bit.
        lsb &= ~(lsb - 1);

        // Remove least significative bit.
        this->word_val_ ^= lsb;

        register u_long n = lsb - this->oldlsb_;

        // Move index to bit distance between new lsb and old lsb.
        do
          {
            this->handle_index_++;
            n &= n >> 1;
          }
        while (n != 0);

        this->oldlsb_ = lsb;
      }

   return this->handle_index_;
#endif /* ACE_WIN32 */
}

void ACE_Handle_Set_Iterator::reset_state ( void   ) 

Reset the state of the iterator by reinitializing the state that we maintain.

Definition at line 499 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set_Iterator::reset_state");

#if !defined (ACE_HAS_BIG_FD_SET) || defined (ACE_WIN32)
  this->handle_index_  = 0;
  this->word_num_ = -1;
#elif defined (ACE_HAS_BIG_FD_SET)
  this->oldlsb_ = 0;
  this->word_max_  =
    this->handles_.max_handle_ == ACE_INVALID_HANDLE  ? 0
    : ((ACE_DIV_BY_WORDSIZE (this->handles_.max_handle_)) + 1);
#endif /* ACE_HAS_BIG_FD_SET */

#if !defined (ACE_WIN32) && !defined (ACE_HAS_BIG_FD_SET)
  // No sense searching further than the max_handle_ + 1;
  ACE_HANDLE maxhandlep1 =
    this->handles_.max_handle_ + 1;

  fd_mask *maskp =
    (fd_mask *)(this->handles_.mask_.fds_bits);

  // Loop until we've found the first non-zero bit or we run past the
  // <maxhandlep1> of the bitset.
  while (this->handle_index_ < maxhandlep1
         && maskp[++this->word_num_] == 0)
    this->handle_index_ += ACE_Handle_Set::WORDSIZE;

  // If the bit index becomes >= the maxhandlep1 that means there
  // weren't any bits set.  Therefore, we'll just store the
  // maxhandlep1, which will cause <operator()> to return
  // <ACE_INVALID_HANDLE> immediately.
  if (this->handle_index_ >= maxhandlep1)
    this->handle_index_ = maxhandlep1;
  else
    // Loop until we get <word_val_> to have its least significant bit
    // enabled, keeping track of which <handle_index> this represents
    // (this information is used by <operator()>).
#if defined (ACE_TANDEM_NSK_BIT_ORDER)
    // bits are in reverse order, MSB (sign bit) = bit 0.
    for (this->word_val_ = maskp[this->word_num_];
         this->word_val_ > 0;
         this->word_val_ = (this->word_val_ << 1))
      this->handle_index_++;
#  else
    for (this->word_val_ = maskp[this->word_num_];
         ACE_BIT_DISABLED (this->word_val_, 1)
           && this->handle_index_ < maxhandlep1;
         this->handle_index_++)
      this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
#  endif /* ACE_TANDEM_NSK_BIT_ORDER */
#elif !defined (ACE_WIN32) && defined (ACE_HAS_BIG_FD_SET)
    if (this->word_max_==0)
      {
        this->word_num_ = -1;
        this->word_val_ = 0;
      }
    else
      {
        this->word_num_ =
          ACE_DIV_BY_WORDSIZE (this->handles_.min_handle_) - 1;
        this->word_val_ = 0;
      }
#endif /* !ACE_WIN32 && !ACE_HAS_BIG_FD_SET */
}


Member Data Documentation

Declare the dynamic allocation hooks.

Definition at line 196 of file Handle_Set.h.

Index of the bit we're examining in the current <word_num_> word.

Definition at line 206 of file Handle_Set.h.

The <Handle_Set> we are iterating through.

Definition at line 200 of file Handle_Set.h.

Number of the word we're iterating over (typically between 0..7).

Definition at line 213 of file Handle_Set.h.

Value of the bits in the word we're iterating on.

Definition at line 222 of file Handle_Set.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines