From b53e8a667e9cd10acce9baaed1a225160dc1aef8 Mon Sep 17 00:00:00 2001 From: richard Date: Tue, 27 Jun 2000 16:18:02 +0000 Subject: [PATCH] Tidied encoding/decoding git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@6824 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++ Source/NSData.m | 170 +++++++++++++++++++----------------------- Source/NSDictionary.m | 2 +- Source/NSPort.m | 17 +++-- Source/NSUnarchiver.m | 22 ++++-- 5 files changed, 109 insertions(+), 108 deletions(-) diff --git a/ChangeLog b/ChangeLog index 95eb2c937..8347dd0c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2000-06-27 Richard Frith-Macdonald + + * 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 * Source/NSTimer.m (-invalidate): Remove assertion. diff --git a/Source/NSData.m b/Source/NSData.m index 116aeea1f..6e90a7f47 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -1,5 +1,5 @@ /* 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 Date: March 1995 @@ -266,8 +266,7 @@ failure: { dataMalloc = [NSDataMalloc class]; mutableDataMalloc = [NSMutableDataMalloc class]; - appendImp = [mutableDataMalloc - instanceMethodForSelector: appendSel]; + appendImp = [mutableDataMalloc instanceMethodForSelector: appendSel]; } } @@ -339,8 +338,7 @@ failure: - (id) init { - return [self initWithBytesNoCopy: 0 - length: 0]; + return [self initWithBytesNoCopy: 0 length: 0]; } - (id) initWithBytes: (const void*)aBuffer @@ -377,8 +375,7 @@ failure: - (id) initWithData: (NSData*)data { - return [self initWithBytes: [data bytes] - length: [data length]]; + return [self initWithBytes: [data bytes] length: [data length]]; } @@ -432,19 +429,17 @@ failure: return AUTORELEASE(str); } -- (void)getBytes: (void*)buffer +- (void) getBytes: (void*)buffer { [self getBytes: buffer range: NSMakeRange(0, [self length])]; } -- (void)getBytes: (void*)buffer - length: (unsigned)length +- (void) getBytes: (void*)buffer length: (unsigned)length { [self getBytes: buffer range: NSMakeRange(0, length)]; } -- (void)getBytes: (void*)buffer - range: (NSRange)aRange +- (void) getBytes: (void*)buffer range: (NSRange)aRange { unsigned size = [self length]; @@ -523,7 +518,7 @@ failure: return (memcmp([self bytes], [other bytes], len) ? NO : YES); } -- (unsigned)length; +- (unsigned) length; { /* This is left to concrete subclasses to implement. */ [self subclassResponsibility: _cmd]; @@ -533,16 +528,15 @@ failure: // Storing Data -- (BOOL) writeToFile: (NSString *)path - atomically: (BOOL)useAuxiliaryFile +- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile { - char thePath[BUFSIZ*2+8]; - char theRealPath[BUFSIZ*2]; - FILE *theFile; - int c; + char thePath[BUFSIZ*2+8]; + char theRealPath[BUFSIZ*2]; + FILE *theFile; + int c; #if defined(__MINGW__) - return NO; + return NO; #endif if ([path getFileSystemRepresentation: theRealPath @@ -1050,13 +1044,18 @@ failure: - (void) encodeWithCoder: (NSCoder*)coder { - [self subclassResponsibility: _cmd]; + [coder encodeDataObject: self]; } - (id) initWithCoder: (NSCoder*)coder { - [self subclassResponsibility: _cmd]; - return nil; + id obj = [coder decodeDataObject]; + + if (obj != self) + { + ASSIGN(self, obj); + } + return self; } @end @@ -1257,12 +1256,58 @@ failure: 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 { [self subclassResponsibility: _cmd]; 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 { [self subclassResponsibility: _cmd]; @@ -1302,8 +1347,7 @@ failure: - (void) appendData: (NSData*)other { - [self appendBytes: [other bytes] - length: [other length]]; + [self appendBytes: [other bytes] length: [other length]]; } @@ -1336,14 +1380,14 @@ failure: // Serializing Data -- (void)serializeAlignedBytesLength: (unsigned)length +- (void) serializeAlignedBytesLength: (unsigned)length { [self serializeInt: length]; } -- (void)serializeDataAt: (const void*)data - ofObjCType: (const char*)type - context: (id )callback +- (void) serializeDataAt: (const void*)data + ofObjCType: (const char*)type + context: (id )callback { if (!data || !type) return; @@ -1679,13 +1723,13 @@ failure: - (id) mutableCopy { return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()] - initWithBytes: bytes length: length]; + initWithBytes: bytes length: length]; } - (id) mutableCopyWithZone: (NSZone*)z { return [[mutableDataMalloc allocWithZone: z] - initWithBytes: bytes length: length]; + initWithBytes: bytes length: length]; } - (void) dealloc @@ -1728,18 +1772,6 @@ failure: 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 */ - (const void*) bytes @@ -2202,36 +2234,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) 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 { #if GS_WITH_GC @@ -2598,28 +2600,6 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) 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 { self = [self initWithCapacity: size]; diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index b5652b6e0..1d9732c57 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -83,12 +83,12 @@ static SEL appSel = @selector(appendString:); { if (self == [NSDictionary class]) { + behavior_class_add_class (self, [NSDictionaryNonCore class]); NSArray_class = [NSArray class]; NSDictionary_abstract_class = [NSDictionary class]; NSMutableDictionary_abstract_class = [NSMutableDictionary class]; NSDictionary_concrete_class = [NSGDictionary class]; NSMutableDictionary_concrete_class = [NSGMutableDictionary class]; - behavior_class_add_class (self, [NSDictionaryNonCore class]); } } diff --git a/Source/NSPort.m b/Source/NSPort.m index 94a971449..38f57efb3 100644 --- a/Source/NSPort.m +++ b/Source/NSPort.m @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -40,12 +41,12 @@ NSString *NSPortTimeoutException + (NSPort*) port { - return AUTORELEASE([NSPort new]); + return AUTORELEASE([self new]); } + (NSPort*) portWithMachPort: (int)machPort { - return AUTORELEASE([[NSPort alloc] initWithMachPort: machPort]); + return AUTORELEASE([[self alloc] initWithMachPort: machPort]); } - (id) copyWithZone: (NSZone*)aZone @@ -60,7 +61,7 @@ NSString *NSPortTimeoutException - (void) encodeWithCoder: (NSCoder*)aCoder { - [self subclassResponsibility: _cmd]; + [(NSPortCoder*)aCoder encodePortObject: self]; } - (id) init @@ -71,8 +72,14 @@ NSString *NSPortTimeoutException - (id) initWithCoder: (NSCoder*)aCoder { - [self subclassResponsibility: _cmd]; - return nil; + id obj = [(NSPortCoder*)aCoder decodePortObject]; + + if (obj != self) + { + RELEASE(self); + self = RETAIN(obj); + } + return self; } - (id) initWithMachPort: (int)machPort diff --git a/Source/NSUnarchiver.m b/Source/NSUnarchiver.m index 26777cde7..25a7d4249 100644 --- a/Source/NSUnarchiver.m +++ b/Source/NSUnarchiver.m @@ -956,20 +956,28 @@ mapClassName(NSUnarchiverObjectInfo *info) { void *b; NSData *d; + NSZone *z; - b = NSZoneMalloc(zone, l); - d = [[NSData allocWithZone: zone] initWithBytesNoCopy: b - length: l - fromZone: zone]; - IF_NO_GC(AUTORELEASE(d)); +#if GS_WITH_GC + z = GSAtomicMallocZone(); +#else + z = zone; +#endif + b = NSZoneMalloc(z, l); [self decodeArrayOfObjCType: @encode(unsigned char) count: l at: b]; + d = [[NSData allocWithZone: zone] initWithBytesNoCopy: b + length: l + fromZone: z]; + IF_NO_GC(AUTORELEASE(d)); return d; } else - [NSException raise: NSInternalInconsistencyException - format: @"Decoding data object with unknown type"]; + { + [NSException raise: NSInternalInconsistencyException + format: @"Decoding data object with unknown type"]; + } } return [NSData data]; }