/* Implementation of Objective-C "collection of delegates" object
   
   Copyright (C) 1993 R. Andrew McCallum <mccallum@cs.rochester.edu>
   Dept. of Computer Science, U. of Rochester, Rochester, NY  14627
   
   This file is part of the GNU Objective-C Collection library.
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */ 

#include <coll/DelegateList.h>

@implementation DelegateList
  
+ new
{
  printf("DelegateList class name %s\n", [self name]);
  self = class_create_instance(self);
  list = [[Array alloc] init];
  _send_behavior = SEND_TO_ALL;
  return self;
}

- free
{
  [list free];
  return object_dispose(self);
}


// MANIPULATING LIST OF DELEGATES;

- delegateListAddObject: anObject
{
  [list addObject: anObject];
  return self;
}

- delegateListRemoveObject: anObject
{
  return [list removeObject:anObject];
}

- (BOOL) delegateListIncludesObject: anObject
{
  return [list includesObject:anObject];
}

- delegateListList
{
  return list;
}

- (enum DelegateListSendBehavior) delegateListSendBehavior
{
  return _send_behavior;
}

- delegateListSetSendBehavior: (enum DelegateListSendBehavior)b
{
  _send_behavior = b;
  return self;
}


// FOR PASSING ALL OTHER MESSAGES TO DELEGATES;

- forward: (SEL)aSel :(arglist_t)argFrame
{
  void *ret = 0;
  id delegate;
  
  switch (_send_behavior) 
    {
    case SEND_TO_ALL:
      FOR_ARRAY(list, delegate)
	{
	  if ([delegate respondsTo:aSel]) 
	    ret = [delegate performv:aSel :argFrame];
	}
      FOR_ARRAY_END;
      break;
      
    case SEND_TO_FIRST_RESPONDER:
      FOR_ARRAY(list, delegate)
	{
	  if ([delegate respondsTo:aSel]) 
	    return [delegate performv:aSel :argFrame];
	}
      FOR_ARRAY_END;
      break;
      
    case SEND_UNTIL_YES:
      FOR_ARRAY(list, delegate)
	{
	  if ([delegate respondsTo:aSel]) 
	    if (ret = [delegate performv:aSel :argFrame])
	      return ret;
	}
      FOR_ARRAY_END;
      break;
      
    case SEND_UNTIL_NO:
      FOR_ARRAY(list, delegate)
	{
	  if ([delegate respondsTo:aSel]) 
	    if (!(ret = [delegate performv:aSel :argFrame]))
	      return ret;
	}
      FOR_ARRAY_END;
      break;
    }
  return ret;
}

@end
