/* $Id: Collection,v 1.1.1.1 2008/11/28 17:57:26 kiesling Exp $ -*-c-*-*/

/*
  This file is part of ctalk.
  Copyright  2005 - 2008  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/*
 *    Collection class.
 */

Object class Collection;

Collection instanceMethod atPut (char *__keyString, 
				       OBJECT *__elemObject) {
  Key new keyObject;
  /*
   *  Alias pointer for the renamed key.
   */
  OBJECT *key_alias_ptr;
  char namebuf[MAXLABEL];

  keyObject setValue __elemObject;
  key_alias_ptr = keyObject setName __keyString;
  /*
   *  TODO! The function still cannot use 
   *  key_alias_ptr -> __o_name as an argument.
   *  (It is an OBJECT *, not a struct.)
   */
  strcpy (namebuf, key_alias_ptr -> __o_name);
  self addInstanceVariable namebuf, key_alias_ptr;

  methodReturnSelf
}

Collection instanceMethod at (char *__keyString) {

  OBJECT *key_object, *item_object;
  
  returnObjectClass Any;

  key_object = __ctalkGetInstanceVariable (self, 
					   __ctalk_to_c_char_ptr(ARG(0))
					   , FALSE);
  if (key_object) {
    sscanf (key_object -> instancevars -> __o_value, "0x%p", &item_object);
    return item_object;
  } else {
    return __ctalkCreateObjectInit ("result", "String", "Character",
				    LOCAL_VAR, NULLSTR);
  }
}

/*
 *  If the interpreter sees this message in a method that
 *  is the argument of map, then it will send the getValue
 *  message to the receiver if it the receiver is a Key object, 
 *  or generate an exception otherwise.
 */
Collection instanceMethod getValue (void) {
  Exception new e;

  if (self is Key) {
    return self getValue;
  }
  e raiseCriticalException INVALID_RECEIVER_X, 
    "Message \"getValue\" sent to a Collection object, not a Key object.";
  methodReturnNULL
}
