mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +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
c629eee3c8
commit
6bb527278f
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>
|
||||
|
||||
* Makefile.postamble (base.make): Depend on configure.
|
||||
|
|
|
@ -243,7 +243,7 @@
|
|||
}
|
||||
|
||||
// NSCoding
|
||||
- (void) encodeWithCoder: (NSCoder *)coder
|
||||
- (void) encodeWithCoder: (NSCoder*)coder
|
||||
{
|
||||
#if TYPE_ORDER == 0
|
||||
[NSException raise: NSInternalInconsistencyException
|
||||
|
@ -252,18 +252,8 @@
|
|||
[NSException raise: NSInternalInconsistencyException
|
||||
format: @"Attempt to encode a pointer to void object"];
|
||||
#else
|
||||
const char *objctype = @encode(TYPE_NAME);
|
||||
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];
|
||||
[super encodeWithCoder: coder];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (id) initWithCoder: (NSCoder *)coder
|
||||
{
|
||||
[coder decodeValueOfObjCType: @encode(TYPE_NAME) at: &data];
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -244,28 +244,17 @@ typeSize(const char* type)
|
|||
// NSCoding
|
||||
- (void) encodeWithCoder: (NSCoder *)coder
|
||||
{
|
||||
NSMutableData *d;
|
||||
unsigned size;
|
||||
|
||||
size = strlen(objctype)+1;
|
||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||
[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
|
||||
{
|
||||
unsigned size;
|
||||
|
||||
[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;
|
||||
d = [NSMutableData new];
|
||||
[d serializeDataAt: data ofObjCType: objctype context: nil];
|
||||
[coder encodeValueOfObjCType: @encode(id) at: &d];
|
||||
RELEASE(d);
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSMapTable.h>
|
||||
#include <Foundation/NSLock.h>
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSDebug.h>
|
||||
|
||||
@interface GSPlaceholderValue : NSValue
|
||||
|
@ -363,6 +364,7 @@ static NSLock *placeholderLock;
|
|||
unsigned size;
|
||||
char *data;
|
||||
const char *objctype = [self objCType];
|
||||
NSMutableData *d;
|
||||
|
||||
size = strlen(objctype)+1;
|
||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||
|
@ -370,8 +372,10 @@ static NSLock *placeholderLock;
|
|||
size = objc_sizeof_type(objctype);
|
||||
data = (void *)NSZoneMalloc(GSObjCZone(self), size);
|
||||
[self getValue: (void*)data];
|
||||
[coder encodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||
[coder encodeArrayOfObjCType: @encode(unsigned char) count: size at: data];
|
||||
d = [NSMutableData new];
|
||||
[d serializeDataAt: data ofObjCType: objctype context: nil];
|
||||
[coder encodeValueOfObjCType: @encode(id) at: &d];
|
||||
RELEASE(d);
|
||||
NSZoneFree(NSDefaultMallocZone(), data);
|
||||
}
|
||||
|
||||
|
@ -381,6 +385,9 @@ static NSLock *placeholderLock;
|
|||
Class c;
|
||||
id o;
|
||||
unsigned size;
|
||||
NSData *d;
|
||||
char *data;
|
||||
unsigned cursor = 0;
|
||||
|
||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
|
@ -389,26 +396,17 @@ static NSLock *placeholderLock;
|
|||
at: (void*)objctype];
|
||||
c = [abstractClass valueClassWithObjCType: objctype];
|
||||
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);
|
||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
[coder decodeArrayOfObjCType: @encode(unsigned char)
|
||||
count: size
|
||||
at: data];
|
||||
o = [o initWithBytes: data objCType: objctype];
|
||||
NSZoneFree(NSDefaultMallocZone(), data);
|
||||
}
|
||||
size = objc_sizeof_type(objctype);
|
||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
[coder decodeValueOfObjCType: @encode(id) at: &d];
|
||||
[d deserializeDataAt: data
|
||||
ofObjCType: objctype
|
||||
atCursor: &cursor
|
||||
context: nil];
|
||||
o = [o initWithBytes: data objCType: objctype];
|
||||
RELEASE(d);
|
||||
NSZoneFree(NSDefaultMallocZone(), data);
|
||||
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
||||
RELEASE(self);
|
||||
self = o;
|
||||
|
@ -444,6 +442,9 @@ static NSLock *placeholderLock;
|
|||
Class c;
|
||||
id o;
|
||||
unsigned size;
|
||||
NSData *d;
|
||||
char *data;
|
||||
unsigned cursor = 0;
|
||||
|
||||
[coder decodeValueOfObjCType: @encode(unsigned) at: &size];
|
||||
objctype = (void*)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
|
@ -452,26 +453,17 @@ static NSLock *placeholderLock;
|
|||
at: (void*)objctype];
|
||||
c = [abstractClass valueClassWithObjCType: objctype];
|
||||
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);
|
||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
[coder decodeArrayOfObjCType: @encode(unsigned char)
|
||||
count: size
|
||||
at: data];
|
||||
o = [o initWithBytes: data objCType: objctype];
|
||||
NSZoneFree(NSDefaultMallocZone(), data);
|
||||
}
|
||||
size = objc_sizeof_type(objctype);
|
||||
data = (void *)NSZoneMalloc(NSDefaultMallocZone(), size);
|
||||
[coder decodeValueOfObjCType: @encode(id) at: &d];
|
||||
[d deserializeDataAt: data
|
||||
ofObjCType: objctype
|
||||
atCursor: &cursor
|
||||
context: nil];
|
||||
o = [o initWithBytes: data objCType: objctype];
|
||||
RELEASE(d);
|
||||
NSZoneFree(NSDefaultMallocZone(), data);
|
||||
NSZoneFree(NSDefaultMallocZone(), (void*)objctype);
|
||||
self = o;
|
||||
return self;
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSGeometry.h>
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSAutoreleasePool.h>
|
||||
#include <Foundation/NSArchiver.h>
|
||||
#include <Foundation/NSAutoreleasePool.h>
|
||||
|
||||
|
||||
int main()
|
||||
|
@ -18,7 +19,7 @@ int main()
|
|||
NSRect rect;
|
||||
NSValue *v1, *v2;
|
||||
NSNumber *nc, *ns, *n1, *n2, *n3, *n4, *n5, *n6, *n7;
|
||||
NSArray *a1, *a2;
|
||||
NSMutableArray *a1, *a2;
|
||||
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
||||
|
||||
// Numbers
|
||||
|
@ -58,8 +59,8 @@ printf("nc compare: ns is %d\n", [nc compare: ns]);
|
|||
printf("n1 isEqual: n3 is %d\n", [n1 isEqual: n3]);
|
||||
printf("n4 isEqual: n5 is %d\n", [n4 isEqual: n5]);
|
||||
|
||||
a1 = [NSArray arrayWithObjects:
|
||||
[NSNumber numberWithChar: 111],
|
||||
a1 = [NSMutableArray arrayWithObjects:
|
||||
[NSNumber numberWithChar: 111],
|
||||
[NSNumber numberWithUnsignedChar: 112],
|
||||
[NSNumber numberWithShort: 121],
|
||||
[NSNumber numberWithUnsignedShort: 122],
|
||||
|
@ -70,7 +71,7 @@ printf("nc compare: ns is %d\n", [nc compare: ns]);
|
|||
[NSNumber numberWithFloat: 151],
|
||||
[NSNumber numberWithDouble: 152], nil];
|
||||
|
||||
a2 = [NSArray arrayWithObjects:
|
||||
a2 = [NSMutableArray arrayWithObjects:
|
||||
[NSNumber numberWithChar: 111],
|
||||
[NSNumber numberWithUnsignedChar: 112],
|
||||
[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]);
|
||||
|
||||
// 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 = NSIntersectionRect(rect, NSMakeRect(20, 78., 89., 30));
|
||||
v1 = [NSValue valueWithRect: rect];
|
||||
[a1 addObject: v1];
|
||||
printf("Encoding for rect is %s\n", [v1 objCType]);
|
||||
rect = [v1 rectValue];
|
||||
printf("Rect is %f %f %f %f\n", NSMinX(rect), NSMinY(rect), NSMaxX(rect),
|
||||
NSMaxY(rect));
|
||||
v2 = [NSValue valueWithPoint: NSMakePoint(3,4)];
|
||||
[a1 addObject: v1];
|
||||
v1 = [NSValue valueWithNonretainedObject: v2];
|
||||
[[v1 nonretainedObjectValue] getValue: &p];
|
||||
printf("point is %f %f\n", p.x, p.y);
|
||||
range = NSMakeRange(1, 103);
|
||||
range = NSIntersectionRange(range, NSMakeRange(2, 73));
|
||||
v1 = [NSValue valueWithRange: range];
|
||||
[a1 addObject: v1];
|
||||
printf("Encoding for range is %s\n", [v1 objCType]);
|
||||
range = [v1 rangeValue];
|
||||
printf("Range is %u %u\n", range.location, range.length);
|
||||
|
||||
printf("Try getting a null NSValue, should get a NSLog error message: \n");
|
||||
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];
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue