@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2022 Jonathan Schleifer + * Copyright (c) 2008-2024 Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -13,19 +13,16 @@ * file. */ #include "config.h" -#include - -#import "OFMapTableDictionary.h" +#import "OFConcreteDictionary.h" #import "OFArray.h" +#import "OFConcreteMutableDictionary.h" #import "OFMapTable+Private.h" #import "OFMapTable.h" -#import "OFMutableMapTableDictionary.h" #import "OFString.h" -#import "OFXMLElement.h" #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" @@ -70,11 +67,11 @@ .release = release, .hash = hash, .equal = equal }; -@implementation OFMapTableDictionary +@implementation OFConcreteDictionary - (instancetype)init { return [self initWithCapacity: 0]; } @@ -100,17 +97,17 @@ size_t count; if (dictionary == nil) return [self init]; - if ([dictionary isKindOfClass: [OFMapTableDictionary class]] || - [dictionary isKindOfClass: [OFMutableMapTableDictionary class]]) { + if ([dictionary isKindOfClass: [OFConcreteDictionary class]] || + [dictionary isKindOfClass: [OFConcreteMutableDictionary class]]) { self = [super init]; @try { - OFMapTableDictionary *dictionary_ = - (OFMapTableDictionary *)dictionary; + OFConcreteDictionary *dictionary_ = + (OFConcreteDictionary *)dictionary; _mapTable = [dictionary_->_mapTable copy]; } @catch (id e) { [self release]; @throw e; @@ -230,63 +227,10 @@ } return self; } -- (instancetype)initWithSerialization: (OFXMLElement *)element -{ - self = [super init]; - - @try { - void *pool = objc_autoreleasePoolPush(); - OFArray *keys, *objects; - OFEnumerator *keyEnumerator, *objectEnumerator; - OFXMLElement *keyElement, *objectElement; - - keys = [element elementsForName: @"key" - namespace: OFSerializationNS]; - objects = [element elementsForName: @"object" - namespace: OFSerializationNS]; - - if (keys.count != objects.count) - @throw [OFInvalidFormatException exception]; - - _mapTable = [[OFMapTable alloc] - initWithKeyFunctions: keyFunctions - objectFunctions: objectFunctions - capacity: keys.count]; - - keyEnumerator = [keys objectEnumerator]; - objectEnumerator = [objects objectEnumerator]; - while ((keyElement = [keyEnumerator nextObject]) != nil && - (objectElement = [objectEnumerator nextObject]) != nil) { - void *pool2 = objc_autoreleasePoolPush(); - OFXMLElement *key, *object; - - key = [keyElement elementsForNamespace: - OFSerializationNS].firstObject; - object = [objectElement elementsForNamespace: - OFSerializationNS].firstObject; - - if (key == nil || object == nil) - @throw [OFInvalidFormatException exception]; - - [_mapTable setObject: object.objectByDeserializing - forKey: key.objectByDeserializing]; - - objc_autoreleasePoolPop(pool2); - } - - objc_autoreleasePoolPop(pool); - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - - (void)dealloc { [_mapTable release]; [super dealloc]; @@ -302,20 +246,20 @@ return _mapTable.count; } - (bool)isEqual: (id)object { - OFMapTableDictionary *dictionary; + OFConcreteDictionary *dictionary; if (object == self) return true; - if (![object isKindOfClass: [OFMapTableDictionary class]] && - ![object isKindOfClass: [OFMutableMapTableDictionary class]]) + if (![object isKindOfClass: [OFConcreteDictionary class]] && + ![object isKindOfClass: [OFConcreteMutableDictionary class]]) return [super isEqual: object]; - dictionary = (OFMapTableDictionary *)object; + dictionary = (OFConcreteDictionary *)object; return [dictionary->_mapTable isEqual: _mapTable]; } - (bool)containsObject: (id)object @@ -344,11 +288,11 @@ size_t i; i = 0; enumerator = [_mapTable keyEnumerator]; while ((keyPtr = [enumerator nextObject]) != NULL) { - assert(i < count); + OFAssert(i < count); keys[i++] = (id)*keyPtr; } objc_autoreleasePoolPop(pool); @@ -377,11 +321,11 @@ size_t i; i = 0; enumerator = [_mapTable objectEnumerator]; while ((objectPtr = [enumerator nextObject]) != NULL) { - assert(i < count); + OFAssert(i < count); objects[i++] = (id)*objectPtr; } objc_autoreleasePoolPop(pool);