Tidied encoding/decoding

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@6824 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-06-27 16:18:02 +00:00
parent 707c63f904
commit b53e8a667e
5 changed files with 109 additions and 108 deletions

View file

@ -1,3 +1,9 @@
2000-06-27 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSPort.m: Add default support for coding using the
encodePortObject and decodePortObject methods.
* Source/NSData.m: Use encodeDataObject and decodeDataObject
2000-06-26 Adam Fedor <fedor@gnu.org> 2000-06-26 Adam Fedor <fedor@gnu.org>
* Source/NSTimer.m (-invalidate): Remove assertion. * Source/NSTimer.m (-invalidate): Remove assertion.

View file

@ -1,5 +1,5 @@
/* Stream of bytes class for serialization and persistance in GNUStep /* Stream of bytes class for serialization and persistance in GNUStep
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu> Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Date: March 1995 Date: March 1995
@ -266,8 +266,7 @@ failure:
{ {
dataMalloc = [NSDataMalloc class]; dataMalloc = [NSDataMalloc class];
mutableDataMalloc = [NSMutableDataMalloc class]; mutableDataMalloc = [NSMutableDataMalloc class];
appendImp = [mutableDataMalloc appendImp = [mutableDataMalloc instanceMethodForSelector: appendSel];
instanceMethodForSelector: appendSel];
} }
} }
@ -339,8 +338,7 @@ failure:
- (id) init - (id) init
{ {
return [self initWithBytesNoCopy: 0 return [self initWithBytesNoCopy: 0 length: 0];
length: 0];
} }
- (id) initWithBytes: (const void*)aBuffer - (id) initWithBytes: (const void*)aBuffer
@ -377,8 +375,7 @@ failure:
- (id) initWithData: (NSData*)data - (id) initWithData: (NSData*)data
{ {
return [self initWithBytes: [data bytes] return [self initWithBytes: [data bytes] length: [data length]];
length: [data length]];
} }
@ -432,19 +429,17 @@ failure:
return AUTORELEASE(str); return AUTORELEASE(str);
} }
- (void)getBytes: (void*)buffer - (void) getBytes: (void*)buffer
{ {
[self getBytes: buffer range: NSMakeRange(0, [self length])]; [self getBytes: buffer range: NSMakeRange(0, [self length])];
} }
- (void)getBytes: (void*)buffer - (void) getBytes: (void*)buffer length: (unsigned)length
length: (unsigned)length
{ {
[self getBytes: buffer range: NSMakeRange(0, length)]; [self getBytes: buffer range: NSMakeRange(0, length)];
} }
- (void)getBytes: (void*)buffer - (void) getBytes: (void*)buffer range: (NSRange)aRange
range: (NSRange)aRange
{ {
unsigned size = [self length]; unsigned size = [self length];
@ -523,7 +518,7 @@ failure:
return (memcmp([self bytes], [other bytes], len) ? NO : YES); return (memcmp([self bytes], [other bytes], len) ? NO : YES);
} }
- (unsigned)length; - (unsigned) length;
{ {
/* This is left to concrete subclasses to implement. */ /* This is left to concrete subclasses to implement. */
[self subclassResponsibility: _cmd]; [self subclassResponsibility: _cmd];
@ -533,16 +528,15 @@ failure:
// Storing Data // Storing Data
- (BOOL) writeToFile: (NSString *)path - (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile
atomically: (BOOL)useAuxiliaryFile
{ {
char thePath[BUFSIZ*2+8]; char thePath[BUFSIZ*2+8];
char theRealPath[BUFSIZ*2]; char theRealPath[BUFSIZ*2];
FILE *theFile; FILE *theFile;
int c; int c;
#if defined(__MINGW__) #if defined(__MINGW__)
return NO; return NO;
#endif #endif
if ([path getFileSystemRepresentation: theRealPath if ([path getFileSystemRepresentation: theRealPath
@ -1050,13 +1044,18 @@ failure:
- (void) encodeWithCoder: (NSCoder*)coder - (void) encodeWithCoder: (NSCoder*)coder
{ {
[self subclassResponsibility: _cmd]; [coder encodeDataObject: self];
} }
- (id) initWithCoder: (NSCoder*)coder - (id) initWithCoder: (NSCoder*)coder
{ {
[self subclassResponsibility: _cmd]; id obj = [coder decodeDataObject];
return nil;
if (obj != self)
{
ASSIGN(self, obj);
}
return self;
} }
@end @end
@ -1257,12 +1256,58 @@ failure:
return [self mutableBytes]; return [self mutableBytes];
} }
- (void) encodeWithCoder: (NSCoder*)aCoder
{
unsigned length = [self length];
void *bytes = [self mutableBytes];
[aCoder encodeValueOfObjCType: @encode(unsigned long)
at: &length];
if (length)
{
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
count: length
at: bytes];
}
}
- (id) initWithCapacity: (unsigned)capacity - (id) initWithCapacity: (unsigned)capacity
{ {
[self subclassResponsibility: _cmd]; [self subclassResponsibility: _cmd];
return nil; return nil;
} }
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned l;
void *b;
NSZone *zone;
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
b = NSZoneMalloc(zone, l);
if (b == 0)
{
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
RELEASE(self);
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
}
else
{
b = 0;
}
return [self initWithBytesNoCopy: b length: l fromZone: zone];
}
- (id) initWithLength: (unsigned)length - (id) initWithLength: (unsigned)length
{ {
[self subclassResponsibility: _cmd]; [self subclassResponsibility: _cmd];
@ -1302,8 +1347,7 @@ failure:
- (void) appendData: (NSData*)other - (void) appendData: (NSData*)other
{ {
[self appendBytes: [other bytes] [self appendBytes: [other bytes] length: [other length]];
length: [other length]];
} }
@ -1336,14 +1380,14 @@ failure:
// Serializing Data // Serializing Data
- (void)serializeAlignedBytesLength: (unsigned)length - (void) serializeAlignedBytesLength: (unsigned)length
{ {
[self serializeInt: length]; [self serializeInt: length];
} }
- (void)serializeDataAt: (const void*)data - (void) serializeDataAt: (const void*)data
ofObjCType: (const char*)type ofObjCType: (const char*)type
context: (id <NSObjCTypeSerializationCallBack>)callback context: (id <NSObjCTypeSerializationCallBack>)callback
{ {
if (!data || !type) if (!data || !type)
return; return;
@ -1679,13 +1723,13 @@ failure:
- (id) mutableCopy - (id) mutableCopy
{ {
return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()] return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()]
initWithBytes: bytes length: length]; initWithBytes: bytes length: length];
} }
- (id) mutableCopyWithZone: (NSZone*)z - (id) mutableCopyWithZone: (NSZone*)z
{ {
return [[mutableDataMalloc allocWithZone: z] return [[mutableDataMalloc allocWithZone: z]
initWithBytes: bytes length: length]; initWithBytes: bytes length: length];
} }
- (void) dealloc - (void) dealloc
@ -1728,18 +1772,6 @@ failure:
return dataMalloc; /* Will not be static data when decoded. */ return dataMalloc; /* Will not be static data when decoded. */
} }
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[aCoder encodeValueOfObjCType: @encode(unsigned long)
at: &length];
if (length)
{
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
count: length
at: bytes];
}
}
/* Basic methods */ /* Basic methods */
- (const void*) bytes - (const void*) bytes
@ -2202,36 +2234,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self; return self;
} }
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned l;
void* b;
#if GS_WITH_GC
zone = GSAtomicMallocZone();
#else
zone = [self zone];
#endif
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
b = NSZoneMalloc(zone, l);
if (b == 0)
{
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
RELEASE(self);
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
}
else
{
b = 0;
}
return [self initWithBytesNoCopy: b length: l fromZone: zone];
}
- (id) initWithContentsOfFile: (NSString *)path - (id) initWithContentsOfFile: (NSString *)path
{ {
#if GS_WITH_GC #if GS_WITH_GC
@ -2598,28 +2600,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
return self; return self;
} }
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned l;
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
if (l)
{
[self initWithCapacity: l];
if (bytes == 0)
{
NSLog(@"[NSMutableDataMalloc -initWithCoder:] unable to allocate %lu bytes", l);
RELEASE(self);
return nil;
}
[aCoder decodeArrayOfObjCType: @encode(unsigned char)
count: l
at: bytes];
length = l;
}
return self;
}
- (id) initWithLength: (unsigned)size - (id) initWithLength: (unsigned)size
{ {
self = [self initWithCapacity: size]; self = [self initWithCapacity: size];

View file

@ -83,12 +83,12 @@ static SEL appSel = @selector(appendString:);
{ {
if (self == [NSDictionary class]) if (self == [NSDictionary class])
{ {
behavior_class_add_class (self, [NSDictionaryNonCore class]);
NSArray_class = [NSArray class]; NSArray_class = [NSArray class];
NSDictionary_abstract_class = [NSDictionary class]; NSDictionary_abstract_class = [NSDictionary class];
NSMutableDictionary_abstract_class = [NSMutableDictionary class]; NSMutableDictionary_abstract_class = [NSMutableDictionary class];
NSDictionary_concrete_class = [NSGDictionary class]; NSDictionary_concrete_class = [NSGDictionary class];
NSMutableDictionary_concrete_class = [NSGMutableDictionary class]; NSMutableDictionary_concrete_class = [NSGMutableDictionary class];
behavior_class_add_class (self, [NSDictionaryNonCore class]);
} }
} }

View file

@ -26,6 +26,7 @@
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSNotificationQueue.h> #include <Foundation/NSNotificationQueue.h>
#include <Foundation/NSPort.h> #include <Foundation/NSPort.h>
#include <Foundation/NSPortCoder.h>
#include <Foundation/NSPortNameServer.h> #include <Foundation/NSPortNameServer.h>
#include <Foundation/NSRunLoop.h> #include <Foundation/NSRunLoop.h>
#include <Foundation/NSAutoreleasePool.h> #include <Foundation/NSAutoreleasePool.h>
@ -40,12 +41,12 @@ NSString *NSPortTimeoutException
+ (NSPort*) port + (NSPort*) port
{ {
return AUTORELEASE([NSPort new]); return AUTORELEASE([self new]);
} }
+ (NSPort*) portWithMachPort: (int)machPort + (NSPort*) portWithMachPort: (int)machPort
{ {
return AUTORELEASE([[NSPort alloc] initWithMachPort: machPort]); return AUTORELEASE([[self alloc] initWithMachPort: machPort]);
} }
- (id) copyWithZone: (NSZone*)aZone - (id) copyWithZone: (NSZone*)aZone
@ -60,7 +61,7 @@ NSString *NSPortTimeoutException
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
[self subclassResponsibility: _cmd]; [(NSPortCoder*)aCoder encodePortObject: self];
} }
- (id) init - (id) init
@ -71,8 +72,14 @@ NSString *NSPortTimeoutException
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
[self subclassResponsibility: _cmd]; id obj = [(NSPortCoder*)aCoder decodePortObject];
return nil;
if (obj != self)
{
RELEASE(self);
self = RETAIN(obj);
}
return self;
} }
- (id) initWithMachPort: (int)machPort - (id) initWithMachPort: (int)machPort

View file

@ -956,20 +956,28 @@ mapClassName(NSUnarchiverObjectInfo *info)
{ {
void *b; void *b;
NSData *d; NSData *d;
NSZone *z;
b = NSZoneMalloc(zone, l); #if GS_WITH_GC
d = [[NSData allocWithZone: zone] initWithBytesNoCopy: b z = GSAtomicMallocZone();
length: l #else
fromZone: zone]; z = zone;
IF_NO_GC(AUTORELEASE(d)); #endif
b = NSZoneMalloc(z, l);
[self decodeArrayOfObjCType: @encode(unsigned char) [self decodeArrayOfObjCType: @encode(unsigned char)
count: l count: l
at: b]; at: b];
d = [[NSData allocWithZone: zone] initWithBytesNoCopy: b
length: l
fromZone: z];
IF_NO_GC(AUTORELEASE(d));
return d; return d;
} }
else else
[NSException raise: NSInternalInconsistencyException {
format: @"Decoding data object with unknown type"]; [NSException raise: NSInternalInconsistencyException
format: @"Decoding data object with unknown type"];
}
} }
return [NSData data]; return [NSData data];
} }