/* 
   NGObjCCategory.m

   Copyright (C) 1998 MDlink online service center, Helge Hess
   All rights reserved.

   Author: Helge Hess (helge@mdlink.de)

   This file is part of the MDlink Object Framework 2 (MOF2)

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/
// $Id: NGObjCCategory.m,v 1.1 2000/03/20 15:45:33 mguesdon Exp $

#include "NGReflectionCommonPrivate.h"
#include "NGObjCCategory.h"
#include "NGObjCInternals.h"

@implementation NGObjCCategory

- (id)initWithName:(NSString *)_name class:(Class)_class {
  if ((self = [super init])) {
    self->class           = RETAIN(_class);
    self->name            = [name copyWithZone:[self zone]];
    self->instanceMethods = [[NSMutableArray allocWithZone:[self zone]] init];
    self->classMethods    = [[NSMutableArray allocWithZone:[self zone]] init];
  }
  return self;
}
- (id)init {
  return [self initWithName:nil class:nil];
}

+ (id)category:(NSString *)_name forClass:(Class)_class {
  return AUTORELEASE([[self alloc] initWithName:_name class:_class]);
}
+ (id)category:(NSString *)_name forClassNamed:(NSString *)_className {
  return AUTORELEASE([[self alloc] initWithName:_name
                                   class:NSClassFromString(_className)]);
}

- (void)dealloc {
  RELEASE(self->class);           self->class           = nil;
  RELEASE(self->name);            self->name            = nil;
  RELEASE(self->instanceMethods); self->instanceMethods = nil;
  RELEASE(self->classMethods);    self->classMethods    = nil;
  RELEASE(self->protocols);       self->protocols       = nil;
  [super dealloc];
}

// accessors

- (NSString *)categoryName {
  return AUTORELEASE([self->name copy]);
}
- (Class)categoryClass {
  return self->class;
}

- (NSEnumerator *)instanceMethods {
  return [self->instanceMethods objectEnumerator];
}
- (NSEnumerator *)classMethods {
  return [self->classMethods objectEnumerator];
}
- (NSEnumerator *)protocols {
  return [self->protocols objectEnumerator];
}

- (void)addInstanceMethod:(NGObjCMethod *)_method {
  [self->instanceMethods addObject:_method];
}
- (void)removeInstanceMethod:(NGObjCMethod *)_method {
  [self->instanceMethods removeObject:_method];
}

- (void)addClassMethod:(NGObjCMethod *)_method {
  [self->classMethods addObject:_method];
}
- (void)removeClassMethod:(NGObjCMethod *)_method {
  [self->classMethods removeObject:_method];
}

// representation

- (void *)internalRepresentation {
  if (self->internalRepresentation == NULL) {
    self->internalRepresentation = _makeCategory(self->name,
                                                 self->class,
                                                 self->instanceMethods,
                                                 self->classMethods,
                                                 self->protocols);
  }

  return self->internalRepresentation;
}

// description

- (NSDictionary *)propertyList {
  NSMutableDictionary *pl = [[NSMutableDictionary alloc] init];

  [pl setObject:[self categoryName] forKey:@"name"];
  if (self->class)
    [pl setObject:NSStringFromClass(self->class) forKey:@"class"];
  if ([self->instanceMethods count] > 0)
    [pl setObject:self->instanceMethods forKey:@"-methods"];
  if ([self->classMethods count] > 0)
    [pl setObject:self->classMethods forKey:@"+methods"];
  if ([self->protocols count] > 0)
    [pl setObject:self->protocols forKey:@"protocols"];

  return AUTORELEASE(pl);
}

- (NSString *)description {
  return [NSString stringWithFormat:
                     @"<%s[0x%08X]: "
                     @"class=%s name=%@ -methods=%@ +methods=%@ protocols=%@>",
                     self->isa->name, (unsigned)self,
                     self->class->name, self->name,
                     self->instanceMethods,
                     self->classMethods,
                     self->protocols
                   ];
}

@end
