/* 
   NGObjCMethod.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: NGObjCMethod.m,v 1.1 2000/03/20 15:45:34 mguesdon Exp $

#include "NGReflectionCommonPrivate.h"
#include "NGObjCMethod.h"

@interface NGObjCMethodDescription(PrivateMethods)

- (id)initWithName:(NSString *)_name signature:(NSString *)_signature;

@end

@implementation NGObjCMethodDescription

- (id)initWithName:(NSString *)_name signature:(NSString *)_signature {
  if ((self = [super init])) {
    self->name      = sel_register_typed_name([_name cString],
                                              [_signature cString]);
    self->types     = [_signature copyWithZone:[self zone]];
    self->signature = nil; // created on demand
  }
  return self;
}

- (id)init {
  RELEASE(self);
  NSLog(@"do not use init with NGObjCMethodDescription !");
  return nil;
}

- (void)dealloc {
  RELEASE(self->types);     self->types     = nil;
  RELEASE(self->signature); self->signature = nil;
  [super dealloc];
}

// accessors

- (SEL)selector {
  return self->name;
}

- (NSString *)methodName {
  return [NSString stringWithCString:sel_get_name(self->name)];
}
- (NSString *)methodTypes {
  return self->types;
}

- (NSMethodSignature *)methodSignature {
  if (self->signature == nil) {
    self->signature =
      [NSMethodSignature signatureWithObjCTypes:[self->types cString]];
    RETAIN(self->signature);
  }
  return self->signature;
}

@end

@interface NGObjCMethod(PrivateMethods)

- (id)initWithName:(NSString *)_name
  signature:(NSString *)_signature
  implementation:(IMP)_method;

- (id)initWithInternalRepresentation:(void *)_irep;

@end

@implementation NGObjCMethod

+ (id)methodWithName:(NSString *)_name
  signature:(NSString *)_signature
  implementation:(IMP)_method {

  return AUTORELEASE([[self alloc] initWithName:_name
                                   signature:_signature
                                   implementation:_method]);
}

- (id)initWithName:(NSString *)_name
  signature:(NSString *)_signature
  implementation:(IMP)_method {

  if ((self = [super initWithName:_name signature:_signature])) {
    self->implementation = _method;
  }
  return self;
}
- (id)initWithName:(NSString *)_name signature:(NSString *)_signature {
  return [self initWithName:_name signature:_signature implementation:NULL];
}

- (id)initWithInternalRepresentation:(void *)_irep {
  Method_t method = (Method_t)_irep;
  
  return [self initWithName:NSStringFromSelector(method->method_name)
               signature:[NSString stringWithCString:method->method_types]
               implementation:method->method_imp];
}

// accessors

- (IMP)methodImplementation {
  return self->implementation;
}

// description

- (NSString *)description {
  return [NSString stringWithFormat:
                     @"<%s[0x%08X]: name=%@ signature=%@ implementation=0x%08X>",
                     self->isa->name, (unsigned)self,
                     [self methodName],
                     [self methodTypes],
                     self->implementation
                   ];
}

@end
