mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-26 02:01:03 +00:00
Simplified archiving
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@11613 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
07f2755388
commit
fbd5244d11
5 changed files with 121 additions and 73 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
2001-12-03 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* Source/NSValue.m: Fix coding/decoding to use serialization code to
|
||||||
|
maintain byte order and word size compatibility.
|
||||||
|
* Source/GSValue.m: ditto
|
||||||
|
* Testing/values.m: added encoding/decoding tests.
|
||||||
|
|
||||||
Sat Dec 1 10:11:18 2001 Nicola Pero <n.pero@mi.flashnet.it>
|
Sat Dec 1 10:11:18 2001 Nicola Pero <n.pero@mi.flashnet.it>
|
||||||
|
|
||||||
* Makefile.postamble (base.make): Depend on configure.
|
* Makefile.postamble (base.make): Depend on configure.
|
||||||
|
|
|
@ -252,18 +252,8 @@
|
||||||
[NSException raise: NSInternalInconsistencyException
|
[NSException raise: NSInternalInconsistencyException
|
||||||
format: @"Attempt to encode a pointer to void object"];
|
format: @"Attempt to encode a pointer to void object"];
|
||||||
#else
|
#else
|
||||||
const char *objctype = @encode(TYPE_NAME);
|
[super encodeWithCoder: coder];
|
||||||
unsigned size = strlen(objctype)+1;
|
|
||||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
||||||
[coder encodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
|
|
||||||
[coder encodeValueOfObjCType: @encode(TYPE_NAME) at: &data];
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithCoder: (NSCoder *)coder
|
|
||||||
{
|
|
||||||
[coder decodeValueOfObjCType: @encode(TYPE_NAME) at: &data];
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -244,28 +244,17 @@ typeSize(const char* type)
|
||||||
// NSCoding
|
// NSCoding
|
||||||
- (void) encodeWithCoder: (NSCoder *)coder
|
- (void) encodeWithCoder: (NSCoder *)coder
|
||||||
{
|
{
|
||||||
|
NSMutableData *d;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
|
|
||||||
size = strlen(objctype)+1;
|
size = strlen(objctype)+1;
|
||||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||||
[coder encodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
|
[coder encodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
|
||||||
size = (unsigned)typeSize(objctype);
|
|
||||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
||||||
[coder encodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (id) initWithCoder: (NSCoder *)coder
|
d = [NSMutableData new];
|
||||||
{
|
[d serializeDataAt: data ofObjCType: objctype context: nil];
|
||||||
unsigned size;
|
[coder encodeValueOfObjCType: @encode(id) at: &d];
|
||||||
|
RELEASE(d);
|
||||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
||||||
objctype = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
|
||||||
[coder decodeArrayOfObjCType: @encode(signed char) count: size at: objctype];
|
|
||||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
|
||||||
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
|
||||||
[coder decodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#include <Foundation/NSException.h>
|
#include <Foundation/NSException.h>
|
||||||
#include <Foundation/NSMapTable.h>
|
#include <Foundation/NSMapTable.h>
|
||||||
#include <Foundation/NSLock.h>
|
#include <Foundation/NSLock.h>
|
||||||
|
#include <Foundation/NSData.h>
|
||||||
#include <Foundation/NSDebug.h>
|
#include <Foundation/NSDebug.h>
|
||||||
|
|
||||||
@interface GSPlaceholderValue : NSValue
|
@interface GSPlaceholderValue : NSValue
|
||||||
|
@ -363,6 +364,7 @@ static NSLock *placeholderLock;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
char *data;
|
char *data;
|
||||||
const char *objctype = [self objCType];
|
const char *objctype = [self objCType];
|
||||||
|
NSMutableData *d;
|
||||||
|
|
||||||
size = strlen(objctype)+1;
|
size = strlen(objctype)+1;
|
||||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||||
|
@ -370,8 +372,10 @@ static NSLock *placeholderLock;
|
||||||
size = objc_sizeof_type(objctype);
|
size = objc_sizeof_type(objctype);
|
||||||
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
||||||
[self getValue: (void*)data];
|
[self getValue: (void*)data];
|
||||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
d = [NSMutableData new];
|
||||||
[coder encodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
[d serializeDataAt: data ofObjCType: objctype context: nil];
|
||||||
|
[coder encodeValueOfObjCType: @encode(id) at: &d];
|
||||||
|
RELEASE(d);
|
||||||
NSZoneFree(NSDefaultMallocZone(), data);
|
NSZoneFree(NSDefaultMallocZone(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,6 +385,9 @@ static NSLock *placeholderLock;
|
||||||
Class c;
|
Class c;
|
||||||
id o;
|
id o;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
|
NSData *d;
|
||||||
|
char *data;
|
||||||
|
unsigned cursor = 0;
|
||||||
|
|
||||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||||
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||||
|
@ -389,26 +396,17 @@ static NSLock *placeholderLock;
|
||||||
at: (void*)objctype];
|
at: (void*)objctype];
|
||||||
c = [abstractClass valueClassWithObjCType: objctype];
|
c = [abstractClass valueClassWithObjCType: objctype];
|
||||||
o = [c alloc];
|
o = [c alloc];
|
||||||
/*
|
|
||||||
* Special case classes can decode themselves.
|
|
||||||
*/
|
|
||||||
if (c == pointValueClass || c == rangeValueClass
|
|
||||||
|| c == rectValueClass || c == sizeValueClass)
|
|
||||||
{
|
|
||||||
o = [o initWithCoder: coder];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *data;
|
|
||||||
|
|
||||||
size = objc_sizeof_type(objctype);
|
size = objc_sizeof_type(objctype);
|
||||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||||
[coder decodeArrayOfObjCType: @encode(unsigned char)
|
[coder decodeValueOfObjCType: @encode(id) at: &d];
|
||||||
count: size
|
[d deserializeDataAt: data
|
||||||
at: data];
|
ofObjCType: objctype
|
||||||
|
atCursor: &cursor
|
||||||
|
context: nil];
|
||||||
o = [o initWithBytes: data objCType: objctype];
|
o = [o initWithBytes: data objCType: objctype];
|
||||||
|
RELEASE(d);
|
||||||
NSZoneFree(NSDefaultMallocZone(), data);
|
NSZoneFree(NSDefaultMallocZone(), data);
|
||||||
}
|
|
||||||
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
||||||
RELEASE(self);
|
RELEASE(self);
|
||||||
self = o;
|
self = o;
|
||||||
|
@ -444,6 +442,9 @@ static NSLock *placeholderLock;
|
||||||
Class c;
|
Class c;
|
||||||
id o;
|
id o;
|
||||||
unsigned size;
|
unsigned size;
|
||||||
|
NSData *d;
|
||||||
|
char *data;
|
||||||
|
unsigned cursor = 0;
|
||||||
|
|
||||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||||
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||||
|
@ -452,26 +453,17 @@ static NSLock *placeholderLock;
|
||||||
at: (void*)objctype];
|
at: (void*)objctype];
|
||||||
c = [abstractClass valueClassWithObjCType: objctype];
|
c = [abstractClass valueClassWithObjCType: objctype];
|
||||||
o = [c alloc];
|
o = [c alloc];
|
||||||
/*
|
|
||||||
* Special case classes can decode themselves.
|
|
||||||
*/
|
|
||||||
if (c == pointValueClass || c == rangeValueClass
|
|
||||||
|| c == rectValueClass || c == sizeValueClass)
|
|
||||||
{
|
|
||||||
o = [o initWithCoder: coder];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *data;
|
|
||||||
|
|
||||||
size = objc_sizeof_type(objctype);
|
size = objc_sizeof_type(objctype);
|
||||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||||
[coder decodeArrayOfObjCType: @encode(unsigned char)
|
[coder decodeValueOfObjCType: @encode(id) at: &d];
|
||||||
count: size
|
[d deserializeDataAt: data
|
||||||
at: data];
|
ofObjCType: objctype
|
||||||
|
atCursor: &cursor
|
||||||
|
context: nil];
|
||||||
o = [o initWithBytes: data objCType: objctype];
|
o = [o initWithBytes: data objCType: objctype];
|
||||||
|
RELEASE(d);
|
||||||
NSZoneFree(NSDefaultMallocZone(), data);
|
NSZoneFree(NSDefaultMallocZone(), data);
|
||||||
}
|
|
||||||
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
||||||
self = o;
|
self = o;
|
||||||
return self;
|
return self;
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <Foundation/NSString.h>
|
#include <Foundation/NSString.h>
|
||||||
#include <Foundation/NSGeometry.h>
|
#include <Foundation/NSGeometry.h>
|
||||||
#include <Foundation/NSArray.h>
|
#include <Foundation/NSArray.h>
|
||||||
|
#include <Foundation/NSArchiver.h>
|
||||||
#include <Foundation/NSAutoreleasePool.h>
|
#include <Foundation/NSAutoreleasePool.h>
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +19,7 @@ int main()
|
||||||
NSRect rect;
|
NSRect rect;
|
||||||
NSValue *v1, *v2;
|
NSValue *v1, *v2;
|
||||||
NSNumber *nc, *ns, *n1, *n2, *n3, *n4, *n5, *n6, *n7;
|
NSNumber *nc, *ns, *n1, *n2, *n3, *n4, *n5, *n6, *n7;
|
||||||
NSArray *a1, *a2;
|
NSMutableArray *a1, *a2;
|
||||||
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
||||||
|
|
||||||
// Numbers
|
// Numbers
|
||||||
|
@ -58,7 +59,7 @@ printf("nc compare: ns is %d\n", [nc compare: ns]);
|
||||||
printf("n1 isEqual: n3 is %d\n", [n1 isEqual: n3]);
|
printf("n1 isEqual: n3 is %d\n", [n1 isEqual: n3]);
|
||||||
printf("n4 isEqual: n5 is %d\n", [n4 isEqual: n5]);
|
printf("n4 isEqual: n5 is %d\n", [n4 isEqual: n5]);
|
||||||
|
|
||||||
a1 = [NSArray arrayWithObjects:
|
a1 = [NSMutableArray arrayWithObjects:
|
||||||
[NSNumber numberWithChar: 111],
|
[NSNumber numberWithChar: 111],
|
||||||
[NSNumber numberWithUnsignedChar: 112],
|
[NSNumber numberWithUnsignedChar: 112],
|
||||||
[NSNumber numberWithShort: 121],
|
[NSNumber numberWithShort: 121],
|
||||||
|
@ -70,7 +71,7 @@ printf("nc compare: ns is %d\n", [nc compare: ns]);
|
||||||
[NSNumber numberWithFloat: 151],
|
[NSNumber numberWithFloat: 151],
|
||||||
[NSNumber numberWithDouble: 152], nil];
|
[NSNumber numberWithDouble: 152], nil];
|
||||||
|
|
||||||
a2 = [NSArray arrayWithObjects:
|
a2 = [NSMutableArray arrayWithObjects:
|
||||||
[NSNumber numberWithChar: 111],
|
[NSNumber numberWithChar: 111],
|
||||||
[NSNumber numberWithUnsignedChar: 112],
|
[NSNumber numberWithUnsignedChar: 112],
|
||||||
[NSNumber numberWithShort: 121],
|
[NSNumber numberWithShort: 121],
|
||||||
|
@ -85,26 +86,95 @@ printf("nc compare: ns is %d\n", [nc compare: ns]);
|
||||||
printf("a1 isEqual: a2 is %d\n", [a1 isEqual: a2]);
|
printf("a1 isEqual: a2 is %d\n", [a1 isEqual: a2]);
|
||||||
|
|
||||||
// Test values, Geometry
|
// Test values, Geometry
|
||||||
|
{
|
||||||
|
unsigned char v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(unsigned char)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
signed char v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(signed char)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
unsigned short v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(unsigned short)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
signed short v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(signed short)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
unsigned int v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(unsigned int)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
signed int v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(signed int)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
unsigned long v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(unsigned long)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
signed long v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(signed long)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
float v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(float)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
double v = 99;
|
||||||
|
v1 = [NSValue value: &v withObjCType: @encode(double)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
}
|
||||||
|
v1 = [NSValue valueWithPoint: NSMakePoint(1, 1)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
v1 = [NSValue valueWithRange: NSMakeRange(1, 1)];
|
||||||
|
[a1 addObject: v1];
|
||||||
rect = NSMakeRect(1.0, 103.3, 40.0, 843.);
|
rect = NSMakeRect(1.0, 103.3, 40.0, 843.);
|
||||||
rect = NSIntersectionRect(rect, NSMakeRect(20, 78., 89., 30));
|
rect = NSIntersectionRect(rect, NSMakeRect(20, 78., 89., 30));
|
||||||
v1 = [NSValue valueWithRect: rect];
|
v1 = [NSValue valueWithRect: rect];
|
||||||
|
[a1 addObject: v1];
|
||||||
printf("Encoding for rect is %s\n", [v1 objCType]);
|
printf("Encoding for rect is %s\n", [v1 objCType]);
|
||||||
rect = [v1 rectValue];
|
rect = [v1 rectValue];
|
||||||
printf("Rect is %f %f %f %f\n", NSMinX(rect), NSMinY(rect), NSMaxX(rect),
|
printf("Rect is %f %f %f %f\n", NSMinX(rect), NSMinY(rect), NSMaxX(rect),
|
||||||
NSMaxY(rect));
|
NSMaxY(rect));
|
||||||
v2 = [NSValue valueWithPoint: NSMakePoint(3,4)];
|
v2 = [NSValue valueWithPoint: NSMakePoint(3,4)];
|
||||||
|
[a1 addObject: v1];
|
||||||
v1 = [NSValue valueWithNonretainedObject: v2];
|
v1 = [NSValue valueWithNonretainedObject: v2];
|
||||||
[[v1 nonretainedObjectValue] getValue: &p];
|
[[v1 nonretainedObjectValue] getValue: &p];
|
||||||
printf("point is %f %f\n", p.x, p.y);
|
printf("point is %f %f\n", p.x, p.y);
|
||||||
range = NSMakeRange(1, 103);
|
range = NSMakeRange(1, 103);
|
||||||
range = NSIntersectionRange(range, NSMakeRange(2, 73));
|
range = NSIntersectionRange(range, NSMakeRange(2, 73));
|
||||||
v1 = [NSValue valueWithRange: range];
|
v1 = [NSValue valueWithRange: range];
|
||||||
|
[a1 addObject: v1];
|
||||||
printf("Encoding for range is %s\n", [v1 objCType]);
|
printf("Encoding for range is %s\n", [v1 objCType]);
|
||||||
range = [v1 rangeValue];
|
range = [v1 rangeValue];
|
||||||
printf("Range is %u %u\n", range.location, range.length);
|
printf("Range is %u %u\n", range.location, range.length);
|
||||||
|
|
||||||
printf("Try getting a null NSValue, should get a NSLog error message: \n");
|
printf("Try getting a null NSValue, should get a NSLog error message: \n");
|
||||||
v2 = [NSValue value: NULL withObjCType: @encode(int)];
|
v2 = [NSValue value: NULL withObjCType: @encode(int)];
|
||||||
|
[a1 addObject: v1];
|
||||||
|
|
||||||
|
a2 = [NSUnarchiver unarchiveObjectWithData:
|
||||||
|
[NSArchiver archivedDataWithRootObject: a1]];
|
||||||
|
|
||||||
|
printf("After archiving, a1 isEqual: a2 is %d\n", [a1 isEqual: a2]);
|
||||||
|
if ([a1 isEqual: a2] == NO)
|
||||||
|
{
|
||||||
|
printf("a1 - %s\n", [[a1 description] cString]);
|
||||||
|
printf("a2 - %s\n", [[a2 description] cString]);
|
||||||
|
}
|
||||||
|
|
||||||
[arp release];
|
[arp release];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue