Support for collection subscripting (NSArray and NSDictionary).

Yes, the syntax is ugly, but no doubt people will start using it in June...



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@35033 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
theraven 2012-04-06 12:23:10 +00:00
parent 8ffddbc573
commit ecfd46edb2
7 changed files with 64 additions and 4 deletions

View file

@ -213,6 +213,11 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*);
options: (NSEnumerationOptions)opts
passingTest: (GSPredicateBlock)predicate;
#endif
/**
* Accessor for subscripting. This is called by the compiler when you write
* code like anArray[12]. It should not be called directly.
*/
- (id) objectAtIndexedSubscript: (size_t)anIndex;
@end
@ -264,7 +269,10 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*);
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
- (void) setValue: (id)value forKey: (NSString*)key;
#endif
/**
* Set method called by the compiler with array subscripting.
*/
- (void) setObject: (id)anObject atIndexedSubscript: (size_t)anIndex;
@end
#if defined(__cplusplus)

View file

@ -108,8 +108,10 @@ DEFINE_BLOCK_TYPE(GSKeysAndObjectsPredicateBlock, BOOL, id, id, BOOL*);
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
- (BOOL) writeToURL: (NSURL*)url atomically: (BOOL)useAuxiliaryFile;
#endif
/**
* Method called by array subscripting.
*/
- (id) objectForKeyedSubscript: (id)aKey;
@end
@interface NSMutableDictionary: NSDictionary
@ -128,6 +130,11 @@ DEFINE_BLOCK_TYPE(GSKeysAndObjectsPredicateBlock, BOOL, id, id, BOOL*);
- (void) takeStoredValue: (id)value forKey: (NSString*)key;
- (void) takeValue: (id)value forKey: (NSString*)key;
#endif
/**
* Method called by array subscripting.
*/
- (void) setObject: (id)anObject forKeyedSubscript: (id)aKey;
@end
#if defined(__cplusplus)

View file

@ -270,7 +270,7 @@
#if defined(__clang__) && defined(__OBJC__)
static inline void gs_consumed(id NS_CONSUMED o) __attribute__ ((unused));
static inline void gs_consumed(id NS_CONSUMED o) { return; }
static inline void gs_consumed(id NS_CONSUMED __attribute__ ((unused))o) { return; }
#define GS_CONSUMED(O) gs_consumed(O);
#else
#define GS_CONSUMED(O)

View file

@ -920,6 +920,11 @@ static SEL rlSel;
return nil;
}
- (id) objectAtIndexedSubscript: (size_t)index
{
return [self objectAtIndex: (NSUInteger)index];
}
- (NSArray *) objectsAtIndexes: (NSIndexSet *)indexes
{
//FIXME: probably slow!
@ -1921,6 +1926,11 @@ compare(id elem1, id elem2, void* context)
[self subclassResponsibility: _cmd];
}
- (void)setObject: (id)anObject atIndexedSubscript: (size_t)anIndex
{
[self replaceObjectAtIndex: (NSUInteger)anIndex withObject: anObject];
}
/** Replaces the values in the receiver at the locations given by the
* indexes set with values from the objects array.
*/

View file

@ -252,6 +252,11 @@ static SEL appSel;
return 0;
}
- (id) objectForKeyedSubscript: (id)aKey
{
return [self objectForKey: aKey];
}
/**
* Return an enumerator object containing all the objects of the dictionary.
*/
@ -1321,6 +1326,11 @@ compareIt(id o1, id o2, void* context)
[self subclassResponsibility: _cmd];
}
- (void) setObject: (id)anObject forKeyedSubscript: (id)aKey
{
[self setObject: anObject forKey: aKey];
}
/**
* Remove key-value mapping for given key aKey. No error if there is no
* mapping for the key. A warning will be generated if aKey is nil.

View file

@ -34,6 +34,17 @@ int main()
#endif
obj = [obj objectAtIndex: 0];
PASS([obj isKindOfClass: [NSMutableArray class]] == YES,"array mutable");
START_SET("NSArray subscripting")
# ifndef __has_feature
# define __has_feature(x) 0
# endif
#if __has_feature(objc_subscripting)
NSArray *a = @[ @"foo", @"bar" ];
PASS([@"foo" isEqualToString:a[0]], "Array subscripting works");
# else
SKIP("No collection subscripting support in the compiler.")
# endif
END_SET("NSArray subscripting")
[arp release]; arp = nil;
return 0;
}

View file

@ -2,6 +2,7 @@
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSString.h>
int main()
@ -27,6 +28,19 @@ int main()
test_NSCoding(testObjs);
test_NSCopying(@"NSDictionary", @"NSMutableDictionary", testObjs, YES, NO);
test_NSMutableCopying(@"NSDictionary", @"NSMutableDictionary", testObjs);
START_SET("NSArray subscripting")
# ifndef __has_feature
# define __has_feature(x) 0
# endif
#if __has_feature(objc_subscripting)
NSDictionary *dictionary = @{@123 : @123.4 ,
@"date" : @"today" };
PASS([dictionary[@123] isEqual: @123.4], "Dictionary subscripting works");
# else
SKIP("No dictionary subscripting support in the compiler.")
# endif
END_SET("NSDictionary subscripting")
[arp release]; arp = nil;
return 0;