1
/* Test/example program for the base library
3
Copyright (C) 2005 Free Software Foundation, Inc.
5
Copying and distribution of this file, with or without modification,
6
are permitted in any medium without royalty provided the copyright
7
notice and this notice are preserved.
9
This file is part of the GNUstep Base Library.
11
/* Test NSArchiver on encoding of self-referential forward references. */
13
#include <Foundation/NSArchiver.h>
14
#include <Foundation/NSArray.h>
15
#include <Foundation/NSAutoreleasePool.h>
17
/* This object encodes an -encodeConditionalObject: reference to a Foo. */
18
@interface SubFoo : NSObject
25
/* This object encodes an -encodeObject: reference to a SubFoo. */
26
@interface Foo : NSObject
34
@implementation SubFoo
42
- (id) initWithSuperFoo: (id)o label: (int)l
45
super_foo = RETAIN(o);
55
- (void) encodeWithCoder: (NSCoder*)coder
57
printf ("In [SubFoo encodeWithCoder:]\n");
58
[coder encodeConditionalObject: super_foo];
59
[coder encodeValueOfObjCType: @encode(int)
63
- (id) initWithCoder: (NSCoder*)coder
65
[coder decodeValueOfObjCType: @encode(id)
67
[coder decodeValueOfObjCType: @encode(int)
74
printf ("label = %d, super label = %d\n",
75
label, [super_foo label]);
106
- (void) encodeWithCoder: (NSCoder*)coder
108
printf ("In [Foo encodeWithCoder:]\n");
109
[coder encodeObject: sub_foo];
110
[coder encodeValueOfObjCType: @encode(int)
114
- (id) initWithCoder: (NSCoder*)coder
116
[coder decodeValueOfObjCType: @encode(id)
118
[coder decodeValueOfObjCType: @encode(int)
128
- (void) setLabel: (int)l
135
/* Test the use of -encodeConditional to encode a forward reference
142
printf ("\nTest encoding of forward references\n");
144
array = [[NSMutableArray alloc] init];
145
foo = [[Foo alloc] init];
147
sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
148
[foo setSubFoo: sub_foo];
149
[array addObject: foo];
150
[array insertObject: sub_foo atIndex: 0];
152
[NSArchiver archiveRootObject: array toFile: @"fref.dat"];
153
printf ("Encoded: ");
159
array = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
160
foo = [array objectAtIndex: 1];
161
sub_foo = [foo subFoo];
162
printf ("Decoded: ");
166
/* Test the encode of a self-referential forward reference. */
171
printf ("\nTest encoding of self-referential forward references\n");
173
foo = [[Foo alloc] init];
175
sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
176
[foo setSubFoo: sub_foo];
178
[NSArchiver archiveRootObject: foo toFile: @"fref.dat"];
179
printf ("Encoded: ");
184
foo = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
185
sub_foo = [foo subFoo];
186
printf ("Decoded: ");
193
CREATE_AUTORELEASE_POOL(arp);