1996-02-13 01:58:05 +00:00
|
|
|
/* A demonstration of writing and reading with NSArchiver */
|
|
|
|
|
|
|
|
#include <objects/Invocation.h>
|
|
|
|
#include <objects/Array.h>
|
1996-02-29 01:51:38 +00:00
|
|
|
#include <objects/Archiver.h>
|
1996-02-13 01:58:05 +00:00
|
|
|
#include <Foundation/NSValue.h>
|
1996-02-29 01:51:38 +00:00
|
|
|
#include <objects/TextCStream.h>
|
1996-02-13 01:58:05 +00:00
|
|
|
|
|
|
|
@interface NSNumber (printing)
|
|
|
|
- (void) print;
|
1996-02-29 01:51:38 +00:00
|
|
|
- printAddNumber: n;
|
1996-02-13 01:58:05 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSNumber (printing)
|
|
|
|
- (void) print
|
|
|
|
{
|
|
|
|
printf("%d\n", [self intValue]);
|
|
|
|
}
|
1996-02-29 01:51:38 +00:00
|
|
|
- printAddNumber: n
|
|
|
|
{
|
|
|
|
printf("%d\n", [self intValue] + [n intValue]);
|
|
|
|
return self;
|
|
|
|
}
|
1996-02-13 01:58:05 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
id obj;
|
|
|
|
id inv;
|
|
|
|
id array;
|
|
|
|
int i;
|
1996-02-29 01:51:38 +00:00
|
|
|
BOOL b;
|
1996-02-13 01:58:05 +00:00
|
|
|
|
1996-02-29 01:51:38 +00:00
|
|
|
/* Create a simple invocation, and get it's return value. */
|
1996-02-13 01:58:05 +00:00
|
|
|
obj = [NSObject new];
|
|
|
|
inv = [[MethodInvocation alloc]
|
1996-02-29 01:51:38 +00:00
|
|
|
initWithTarget: obj selector: @selector(isInstance)];
|
1996-02-13 01:58:05 +00:00
|
|
|
[inv invoke];
|
1996-02-29 01:51:38 +00:00
|
|
|
[inv getReturnValue: &b];
|
|
|
|
printf ("object is instance %d\n", (int)b);
|
1996-02-13 01:58:05 +00:00
|
|
|
[inv release];
|
|
|
|
|
1996-02-29 01:51:38 +00:00
|
|
|
/* Do a simple invocation on all the contents of a collection. */
|
1996-02-13 01:58:05 +00:00
|
|
|
array = [Array new];
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
[array addObject: [NSNumber numberWithInt: i]];
|
|
|
|
inv = [[MethodInvocation alloc]
|
|
|
|
initWithSelector: @selector(print)];
|
1996-02-29 01:51:38 +00:00
|
|
|
printf ("The numbers\n");
|
|
|
|
[array withObjectsInvoke: inv];
|
|
|
|
[inv release];
|
|
|
|
|
|
|
|
/* Do an invocation on all the contents of the array, but the
|
|
|
|
array contents become the first object argument of the invocation,
|
|
|
|
not the target for the invocation. */
|
|
|
|
inv = [[ObjectMethodInvocation alloc]
|
|
|
|
initWithTarget: [NSNumber numberWithInt: 2]
|
|
|
|
selector: @selector(printAddNumber:), nil];
|
|
|
|
printf ("The numbers adding 2\n");
|
1996-02-13 01:58:05 +00:00
|
|
|
[array withObjectsInvoke: inv];
|
|
|
|
|
1996-02-29 01:51:38 +00:00
|
|
|
/* Get an int return value in a way that is simpler than -getReturnValue: */
|
|
|
|
printf ("The target number was %d\n", [inv intReturnValue]);
|
|
|
|
[inv release];
|
|
|
|
|
|
|
|
/* Use a function instead of a selector for the invocation.
|
|
|
|
Also show the use of filtered enumerating over a collection. */
|
|
|
|
{
|
|
|
|
id inv2;
|
|
|
|
id test_func (id o)
|
|
|
|
{
|
|
|
|
printf ("test_func got %d\n", [o intValue]);
|
|
|
|
return [NSNumber numberWithInt: [o intValue] + 3];
|
|
|
|
}
|
|
|
|
inv = [[ObjectFunctionInvocation alloc]
|
|
|
|
initWithObjectFunction: test_func];
|
|
|
|
inv2 = [[MethodInvocation alloc] initWithSelector: @selector(print)];
|
|
|
|
[array withObjectsTransformedByInvoking: inv
|
|
|
|
invoke: inv2];
|
|
|
|
[inv release];
|
|
|
|
[inv2 release];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Archive the some invocations, read them back and invoke. */
|
|
|
|
{
|
|
|
|
inv = [[MethodInvocation alloc]
|
|
|
|
initWithTarget: array
|
|
|
|
selector: @selector(withObjectsInvoke:),
|
|
|
|
[[[MethodInvocation alloc] initWithSelector: @selector(print)]
|
|
|
|
autorelease]];
|
|
|
|
printf ("Before archiving\n");
|
|
|
|
[inv invoke];
|
|
|
|
[Archiver setDefaultCStreamClass: [TextCStream class]];
|
|
|
|
[Archiver encodeRootObject: inv withName: NULL toFile: @"invocation.txt"];
|
|
|
|
[inv release];
|
|
|
|
printf ("After archiving\n");
|
|
|
|
inv = [Unarchiver decodeObjectWithName: NULL
|
|
|
|
fromFile: @"invocation.txt"];
|
|
|
|
[inv invoke];
|
|
|
|
}
|
|
|
|
|
1996-02-13 01:58:05 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|