/* Code for implementation for Objective-C EltNode objects
   Copyright (C) 1993 Free Software Foundation, Inc.

   Written by:  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 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.
*/ 

/* This file gets included in all the ...EltNode.m files.
   Doing this silly #include stuff is a poor substitute for multiple
   inheritance.  sigh.

   Pattern:

   @implementation FooEltNode : FooNode
   #include <objects/EltNode-m>
   @end
*/

#include <objects/eltfuncs.h>

- initElement: (elt)anElement 
    encoding: (const char *)eltEncoding
{
  [super init];
  _element = anElement;
  _elt_comparison_function = elt_get_comparison_function(eltEncoding);
  return self;
}

/* Archiving must mimic the above designated initializer */

- (void) encodeWithCoder: (Coder*)aCoder
{
  const char *encoding;

  [super encodeWithCoder:aCoder];
  encoding = elt_get_encoding(_elt_comparison_function);
  [aCoder encodeValueOfType:@encode(char*) at:&encoding 
	  withName:"EltNode Content Type Encoding"];
  [aCoder encodeValueOfType:encoding 
	  at:elt_get_ptr_to_member(encoding, &_element)
	  withName:"EltNode Content Element"];
}

- (elt*) _elementDataPtr
{
  return &_element;
}

- (int(**)(elt,elt)) _eltComparisonFunctionPtr
{
  return &_elt_comparison_function;
}

+ newWithCoder: (Coder*)aCoder
{
  id n;
  char *encoding;

  n = [super newWithCoder:aCoder];
  [aCoder decodeValueOfType:@encode(char*)
	  at:&encoding
	  withName:NULL];
  *[n _eltComparisonFunctionPtr] = elt_get_comparison_function(encoding);
  [aCoder decodeValueOfType:encoding
	  at:[n _elementDataPtr]
	  withName:NULL];
  return n;
}

- write: (TypedStream*)aStream
{
  const char *encoding;

  [super write:aStream];
  encoding = elt_get_encoding(_elt_comparison_function);
  objc_write_type(aStream, @encode(char*), &encoding);
  objc_write_types(aStream, encoding, 
		   elt_get_ptr_to_member(encoding, &_element));
  return self;
}

- read: (TypedStream*)aStream
{
  char *encoding;

  [super read:aStream];
  objc_read_type(aStream, @encode(char*), &encoding);
  _elt_comparison_function = elt_get_comparison_function(encoding);
  objc_read_type(aStream, encoding, 
		 elt_get_ptr_to_member(encoding,&_element));
  return self;
}

- (int(*)(elt,elt)) comparisonFunction
{
  return _elt_comparison_function;
}

- (elt) elementData
{
  return _element;
}

- (int) compare: anotherObject
{
  /* perhaps we should do more checking first */
  return _elt_comparison_function(_element, [anotherObject elementData]);
}

- printForDebugger
{
  elt_fprintf_elt(stdout, 
		  elt_get_encoding(_elt_comparison_function),
		  _element);
  printf("\n");
  return self;
}
