Listing 9 (th_list.cpp)

//  class ThingList  -- a list of things
//
//  Version 1.0-- 2/25/91
//
//  Michael Kelly -- Author
//
#pragma warn -pia  //  nix "Possibly incorrect assignment"
#include "th_list.hpp"

static Thing dummy_thing;

ThingList::~ThingList()
{
    while( head ) {
   rover = head;
   head = head->next;
   delete rover;
   rover = NULL;
    }
}

int ThingList::add( Thing &some_thing )
{
    if( ! ( rover = head ) ) {
   if( ! ( head = new thing_node ) )
       return 0;
   head->this thing = &some_thing;
   head->next = NULL;
    }
    else {
   while( rover->next )
       rover = rover->next;
   if( ! ( rover->next = new thing_node ) )
       return 0;
   rover = rover->next;
   rover->this thing = &some_thing;
   rover->next = NULL;
    }

    ++nodes;
    return 1;
}

Thing* ThingList::operator[]( unsigned index )
{
    if(
   ( ! ( rover = head ) ) ||
   (index >= nodes )
    )
   return &dummy_thing;

    while( index-- ) {
   if( ! rover->next )
       return &dummy_thing;
   rover = rover->next;
    }
    return rover->this_thing;
}

unsigned ThingList::iterate( void ( Thing::*funcptr )() )
{
    if( ! ( rover = head ) )
   return 0;

    do {

      if( rover->this_thing )
     (rover->this_thing->*funcptr)();

    }
    while( rover = rover->next );
    return nodes;
}

unsigned ThingList::iterate( int ( Thing::*funcptr )() )
{
    unsigned successes = 0;

    if( ! ( rover = head ) )
   return 0;

    do {

      if( rover->this_thing )
     successes += (rover->this_thing->*funcptr)();

    }
    while( rover = rover->next );

    return successes;
}

#pragma warn .pia  //  restore warning to default state

// End of File