/* 
   MyObject.m

   Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   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.
*/

#include "MyObject.h"

#include <stdio.h>
#include <Foundation/NSInvocation.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSString.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSUtilities.h>
#include <Foundation/NSScanner.h>

#if !NeXT_foundation_LIBRARY
#include <Foundation/NSTimer.h>
#include <Foundation/NSRunLoop.h>
#else /* NeXT_foundation_LIBRARY */
#define NSDefaultRunLoopMode NSRunLoopDefaultMode

extern id NSDefaultRunLoopMode;
#endif /* NeXT_foundation_LIBRARY */

#if GNUSTEP_BASE_LIBRARY || LIBOBJECTS_LIBRARY
#include <Foundation/RunLoop.h>
#endif

#include <extensions/objc-runtime.h>

#include "tests.h"

@implementation MyObject

- method
{
    printf("%u method\n", (unsigned)self);
    return self;
}

- method:arg
{
    printf("%u method: %u\n", (unsigned)self, (unsigned)arg);
    return self;
}

- setFlag:(BOOL)f
{
    flag = f;
    printf("setFlag: flag = %d\n", (int)flag);
    return self;
}

- setShortValue:(short)s
{
    shortValue = s;
    printf("setShortValue: shortValue = %d\n", shortValue);
    return self;
}

- setIntValue:(int)n
{
    intValue = n;
    printf("setIntValue: intValue = %d\n", intValue);
    return self;
}

- setString:(char*)s
{
    str = s;
    printf("setString: value = '%s'\n", str);
    return self;
}

- setFlag:(BOOL)b intValue:(int)i floatValue:(float)f doubleValue:(double)d
{
    flag = b;
    intValue = i;
    floatValue = f;
    doubleValue = d;
    puts("setFlag:intValue:floatValue:doubleValue:");
    printf("flag = %d\n", (int)flag);
    printf("intValue = %d\n", intValue);
    printf("floatValue = %f\n", floatValue);
    printf("doubleValue = %f\n", doubleValue);

    return self;
}

- setStruct1:(Struct1)s
{
    structure.s1 = s;
    puts("setStruct1:");
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct2:(Struct2)s
{
    structure.s2 = s;
    puts("setStruct2:");
    printf("s = %hd\n", s.s);
    return self;
}

- setStruct3:(Struct3)s
{
    structure.s3 = s;
    puts("setStruct3:");
    printf("s = %hd\n", s.s);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct4:(Struct4)s
{
    structure.s4 = s;
    puts("setStruct4:");
    printf("i = %d\n", s.i);
    return self;
}

- setStruct5:(Struct5)s
{
    structure.s5 = s;
    puts("setStruct5:");
    printf("i = %d\n", s.i);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct6:(Struct6)s
{
    structure.s6 = s;
    puts("setStruct6:");
    printf("i = %d\n", s.i);
    printf("s = %hd\n", s.s);
    return self;
}

- setStruct7:(Struct7)s
{
    structure.s7 = s;
    puts("setStruct7:");
    printf("i = %d\n", s.i);
    printf("s = %hd\n", s.s);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct8:(Struct8)s
{
    structure.s8 = s;
    puts("setStruct8:");
    printf("i = %d\n", s.i);
    printf("j = %d\n", s.j);
    return self;
}

- setStruct9:(Struct9)s
{
    structure.s9 = s;
    puts("setStruct9:");
    printf("i = %d\n", s.i);
    printf("j = %d\n", s.j);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct10:(Struct10)s
{
    structure.s10 = s;
    puts("setStruct10:");
    printf("i = %d\n", s.i);
    printf("j = %d\n", s.j);
    printf("s = %hd\n", s.s);
    return self;
}

- setStruct11:(Struct11)s
{
    structure.s11 = s;
    puts("setStruct11:");
    printf("i = %d\n", s.i);
    printf("j = %d\n", s.j);
    printf("s = %hd\n", s.s);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct12:(Struct12)s
{
    structure.s12 = s;
    puts("setStruct12:");
    printf("d = %f\n", s.d);
    printf("i = %d\n", s.i);
    return self;
}

- setStruct13:(Struct13)s
{
    structure.s13 = s;
    puts("setStruct13:");
    printf("d = %f\n", s.d);
    printf("i = %d\n", s.i);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct14:(Struct14)s
{
    structure.s14 = s;
    puts("setStruct14:");
    printf("d = %f\n", s.d);
    printf("i = %d\n", s.i);
    printf("s = %hd\n", s.s);
    return self;
}

- setStruct15:(Struct15)s
{
    structure.s15 = s;
    puts("setStruct15:");
    printf("d = %f\n", s.d);
    printf("i = %d\n", s.i);
    printf("s = %hd\n", s.s);
    printf("c = %d\n", (int)s.c);
    return self;
}

- setStruct16:(Struct16)s
{
    structure.s16 = s;
    puts("setStruct16:");
    printf("c1 = %d\n", (int)s.c1);
    printf("s = %hd\n", s.s);
    printf("c2 = %d\n", (int)s.c2);
    printf("i = %d\n", s.i);
    printf("d = %f\n", s.d);
    return self;
}

- (BOOL)flag		{ return flag; }
- (short)shortValue	{ return shortValue; }
- (int)intValue		{ return intValue; }
- (float)floatValue	{ return floatValue; }
- (double)doubleValue	{ return doubleValue; }
- (char*)string		{ return str; }
- (Struct1)struct1	{ return structure.s1; }
- (Struct2)struct2	{ return structure.s2; }
- (Struct3)struct3	{ return structure.s3; }
- (Struct4)struct4	{ return structure.s4; }
- (Struct5)struct5	{ return structure.s5; }
- (Struct6)struct6	{ return structure.s6; }
- (Struct7)struct7	{ return structure.s7; }
- (Struct8)struct8	{ return structure.s8; }
- (Struct9)struct9	{ return structure.s9; }
- (Struct10)struct10	{ return structure.s10; }
- (Struct11)struct11	{ return structure.s11; }
- (Struct12)struct12	{ return structure.s12; }
- (Struct13)struct13	{ return structure.s13; }
- (Struct14)struct14	{ return structure.s14; }
- (Struct15)struct15	{ return structure.s15; }
- (Struct16)struct16	{ return structure.s16; }

@end /* MyObject */


#if !NeXT_foundation_LIBRARY
@implementation MyObject (TimerTest)

+ (NSTimer*)newTimerWithInterval:(double)interval repeats:(int)repeats
{
    return [NSTimer timerWithTimeInterval:interval
		    target:[[MyObject new] autorelease]
		    selector:@selector(alarm:)
		    userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithDouble:interval],
			@"time interval", nil]
		    repeats:repeats];
}

+ (NSTimer*)newScheduledTimerWithInterval:(double)interval repeats:(int)repeats
{
    return [NSTimer scheduledTimerWithTimeInterval:interval
		    target:[[MyObject new] autorelease]
		    selector:@selector(alarm:)
		    userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithDouble:interval],
			@"time interval", nil]
		    repeats:repeats];
}

- alarm:(NSTimer*)timer
{
    printf("alarm from timer with time interval = %s\n",
	[[[[timer userInfo] objectForKey:@"time interval"] stringValue]
		cString]);
    return self;
}

+ printTimeIntervalSinceReferenceDateFor:(NSDate*)date
{
    printf("timeIntervalSinceReferenceDate = %18.6f\n", [date timeIntervalSinceReferenceDate]);
    return self;
}

@end /* MyObject (TimerTest) */

@implementation MyObject (RunLoopTest)
+ (NSString*)defaultMode	{ return NSDefaultRunLoopMode; }
@end
#endif /* !NeXT_foundation_LIBRARY */


@implementation MyObject (PropertyListTest)
+ (BOOL)verifyObject:anObject
{
    if([anObject retainCount] != 1) {
	printf("%u object has ref count %d (description is '%s')\n", \
		(unsigned)anObject, [anObject retainCount], \
		[[anObject description] cString]); \
	return NO;
    }

    if([anObject isKindOfClass:[NSArray class]]) {
	if([self verifyArray:anObject] == NO)
	    return NO;
    }

    if([anObject isKindOfClass:[NSDictionary class]]) {
	if([self verifyDict:anObject] == NO)
	    return NO;
    }
    return YES;
}

+ (BOOL)verifyArray:anObject
{
    id pool = [NSAutoreleasePool new];
    id enumerator = [anObject objectEnumerator];
    id elem;

    while((elem = [enumerator nextObject])) {
	if([self verifyObject:elem] == NO) {
	    [pool release];
	    return NO;
	}
    }
    [pool release];
    return YES;
}

+ (BOOL)verifyDict:anObject
{
    id pool = [NSAutoreleasePool new];
    id enumerator = [anObject keyEnumerator];
    id key, value;

    while((key = [enumerator nextObject])) {
	value = [anObject objectForKey:key];

	if([self verifyObject:key] == NO) {
	    [pool release];
	    return NO;
	}
	if([self verifyObject:value] == NO) {
	    [pool release];
	    return NO;
	}
    }
    [pool release];
    return YES;
}
@end /* MyObject (PropertyListTest) */


@implementation MyObject (NSScannerTest)
+ (void)scanInt:scanner
{
    int result;
    [scanner scanInt:&result];
    printf ("value = %d\n", result);
}

+ (void)scanFloat:scanner
{
    float result;
    [scanner scanFloat:&result];
    printf ("value = %f\n", result);
}

+ (void)scanDouble:scanner
{
    double result;
    [scanner scanDouble:&result];
    printf ("value = %f\n", result);
}

+ (void)scanLongLong:scanner
{
    long long result;
    id number;

    [scanner scanLongLong:&result];
    number = [NSNumber numberWithLongLong:result];
    printf ("value = %s\n", [[number stringValue] cString]);
}
@end


@implementation Forwarder

- setObject:anObject
{
    [anObject retain];
    [object release];
    object = anObject;
    return self;
}

- (void)dealloc
{
    [object release];
    [super dealloc];
}

- object		{ return object; }

- (void)forwardInvocation:(NSInvocation*)invocation
{
    [invocation invokeWithTarget:object];
}

@end /* Forwarder */


@implementation MySecondForwarder
- (void)forwardInvocation:(NSInvocation*)invocation
{
    [invocation invokeWithTarget:object];
    printf("%u\n", (unsigned)invocation);
    do_action();
}
@end /* MySecondForwarder */
