Index: src/bridge/Makefile ================================================================== --- src/bridge/Makefile +++ src/bridge/Makefile @@ -21,16 +21,18 @@ OFBridging.h \ ObjFWBridge.h SRCS += NSOFArray.m \ NSOFDictionary.m \ + NSOFEnumerator.m \ OFNSArray.m \ OFNSDictionary.m \ + OFNSEnumerator.m includesubdir = ObjFWBridge include ../../buildsys.mk CPPFLAGS += -I. -I.. -I../.. -I../exceptions -DOF_BRIDGE_LOCAL_INCLUDES LD = ${OBJC} FRAMEWORK_LIBS := -framework Foundation -F.. -framework ObjFW ${LIBS} LIBS := -framework Foundation -L.. -lobjfw ${LIBS} Index: src/bridge/NSOFDictionary.m ================================================================== --- src/bridge/NSOFDictionary.m +++ src/bridge/NSOFDictionary.m @@ -14,10 +14,11 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "NSOFDictionary.h" +#import "NSOFEnumerator.h" #import "OFDictionary.h" #import "NSBridging.h" #import "OFBridging.h" @@ -61,6 +62,12 @@ if (count > NSUIntegerMax) @throw [OFOutOfRangeException exception]; return (NSUInteger)count; } + +- (NSEnumerator *)keyEnumerator +{ + return [[[NSOFEnumerator alloc] + initWithOFEnumerator: [_dictionary keyEnumerator]] autorelease]; +} @end ADDED src/bridge/NSOFEnumerator.h Index: src/bridge/NSOFEnumerator.h ================================================================== --- src/bridge/NSOFEnumerator.h +++ src/bridge/NSOFEnumerator.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019 + * 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 + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import + +#import "macros.h" + +@class OFEnumerator; + +OF_ASSUME_NONNULL_BEGIN + +@interface NSOFEnumerator: NSEnumerator +{ + OFEnumerator *_enumerator; +} + +- (instancetype)initWithOFEnumerator: (OFEnumerator *)enumerator; +@end + +OF_ASSUME_NONNULL_END ADDED src/bridge/NSOFEnumerator.m Index: src/bridge/NSOFEnumerator.m ================================================================== --- src/bridge/NSOFEnumerator.m +++ src/bridge/NSOFEnumerator.m @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019 + * 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 + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "NSOFEnumerator.h" +#import "OFEnumerator.h" + +#import "NSBridging.h" +#import "OFBridging.h" + +@implementation NSOFEnumerator +- (instancetype)initWithOFEnumerator: (OFEnumerator *)enumerator +{ + if ((self = [super init]) != nil) + _enumerator = [enumerator retain]; + + return self; +} + +- (void)dealloc +{ + [_enumerator release]; + + [super dealloc]; +} + +- (id)nextObject +{ + id object = [_enumerator nextObject]; + + if ([(OFObject *)object conformsToProtocol: @protocol(OFBridging)]) + return [object NSObject]; + + return object; +} +@end Index: src/bridge/OFNSArray.m ================================================================== --- src/bridge/OFNSArray.m +++ src/bridge/OFNSArray.m @@ -18,22 +18,21 @@ #import #import "OFNSArray.h" #import "NSBridging.h" -#import "OFInitializationFailedException.h" +#import "OFInvalidArgumentException.h" #import "OFOutOfRangeException.h" @implementation OFNSArray - (instancetype)initWithNSArray: (NSArray *)array { self = [super init]; @try { if (array == nil) - @throw [OFInitializationFailedException - exceptionWithClass: self.class]; + @throw [OFInvalidArgumentException exception]; _array = [array retain]; } @catch (id e) { [self release]; @throw e; Index: src/bridge/OFNSDictionary.m ================================================================== --- src/bridge/OFNSDictionary.m +++ src/bridge/OFNSDictionary.m @@ -16,25 +16,25 @@ */ #import #import "OFNSDictionary.h" +#import "OFNSEnumerator.h" #import "NSBridging.h" #import "OFBridging.h" -#import "OFInitializationFailedException.h" +#import "OFInvalidArgumentException.h" @implementation OFNSDictionary - (instancetype)initWithNSDictionary: (NSDictionary *)dictionary { self = [super init]; @try { if (dictionary == nil) - @throw [OFInitializationFailedException - exceptionWithClass: self.class]; + @throw [OFInvalidArgumentException exception]; _dictionary = [dictionary retain]; } @catch (id e) { [self release]; @throw e; @@ -67,6 +67,12 @@ - (size_t)count { return _dictionary.count; } + +- (OFEnumerator *)keyEnumerator +{ + return [[[OFNSEnumerator alloc] + initWithNSEnumerator: [_dictionary keyEnumerator]] autorelease]; +} @end ADDED src/bridge/OFNSEnumerator.h Index: src/bridge/OFNSEnumerator.h ================================================================== --- src/bridge/OFNSEnumerator.h +++ src/bridge/OFNSEnumerator.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019 + * 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 + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#ifdef OF_BRIDGE_LOCAL_INCLUDES +# import "OFEnumerator.h" +#else +# if defined(__has_feature) && __has_feature(modules) +@import ObjFW; +# else +# import +# endif +#endif + +OF_ASSUME_NONNULL_BEGIN + +@class NSEnumerator; + +@interface OFNSEnumerator: OFEnumerator +{ + NSEnumerator *_enumerator; +} + +- (instancetype)initWithNSEnumerator: (NSEnumerator *)enumerator; +@end + +OF_ASSUME_NONNULL_END ADDED src/bridge/OFNSEnumerator.m Index: src/bridge/OFNSEnumerator.m ================================================================== --- src/bridge/OFNSEnumerator.m +++ src/bridge/OFNSEnumerator.m @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019 + * 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 + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import + +#import "OFNSEnumerator.h" + +#import "NSBridging.h" +#import "OFBridging.h" + +#import "OFInvalidArgumentException.h" + +@implementation OFNSEnumerator +- (instancetype)initWithNSEnumerator: (NSEnumerator *)enumerator +{ + self = [super init]; + + @try { + if (enumerator == nil) + @throw [OFInvalidArgumentException exception]; + + _enumerator = [enumerator retain]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_enumerator release]; + + [super dealloc]; +} + +- (id)nextObject +{ + id object = [_enumerator nextObject]; + + if ([(NSObject *)object conformsToProtocol: @protocol(NSBridging)]) + return [object OFObject]; + + return object; +} +@end