Overview
Comment: | Add -[valueForKeyPath:] / -[setValue:forKeyPath:] |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
d7824704f9f1cde1a54fa570747d1b17 |
User & Date: | js on 2019-03-23 22:03:15 |
Other Links: | manifest | tags |
Context
2019-03-24
| ||
13:19 | Derive of_std{in,out,err} from std{in,out,err} check-in: 0f8a72d43c user: js tags: trunk | |
2019-03-23
| ||
22:03 | Add -[valueForKeyPath:] / -[setValue:forKeyPath:] check-in: d7824704f9 user: js tags: trunk | |
14:44 | ofhash: Add language dir before parsing options check-in: df94d134d9 user: js tags: trunk | |
Changes
Modified src/OFArray.h from [366448c221] to [f538b1be6a].
︙ | ︙ | |||
228 229 230 231 232 233 234 | */ - (ObjectType)objectAtIndex: (size_t)index; - (ObjectType)objectAtIndexedSubscript: (size_t)index; /*! * @brief Returns the value for the specified key * | < < | | > > < < | < | | 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | */ - (ObjectType)objectAtIndex: (size_t)index; - (ObjectType)objectAtIndexedSubscript: (size_t)index; /*! * @brief Returns the value for the specified key * * A new array with the value for the specified key for each object is * returned. * * The special key `@count` can be used to retrieve the count as an OFNumber. * * @note Any nil values are replaced with @ref OFNull! * * @param key The key of the value to return * @return The value for the specified key */ - (nullable id)valueForKey: (OFString *)key; /*! * @brief Set the value for the specified key * * @ref setValue:forKey: is called for each object in the array. * * @note A @ref OFNull value is translated to nil! * * @param value The value for the specified key * @param key The key of the value to set */ - (void)setValue: (nullable id)value forKey: (OFString *)key; /*! * @brief Copies the objects at the specified range to the specified buffer. * * @param buffer The buffer to copy the objects to * @param range The range to copy |
︙ | ︙ |
Modified src/OFArray.m from [58353278e7] to [d37721f3e3].
︙ | ︙ | |||
274 275 276 277 278 279 280 | return [self objectAtIndex: idx]; } - (id)valueForKey: (OFString *)key { id ret; | < < | < | < < < < < < < < < < < < < < < < | 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | return [self objectAtIndex: idx]; } - (id)valueForKey: (OFString *)key { id ret; if ([key isEqual: @"@count"]) return [super valueForKey: @"count"]; ret = [OFMutableArray arrayWithCapacity: self.count]; for (id object in self) { id value = [object valueForKey: key]; if (value == nil) value = [OFNull null]; [ret addObject: value]; } [ret makeImmutable]; return ret; } - (void)setValue: (id)value forKey: (OFString *)key { for (id object in self) [object setValue: value forKey: key]; } - (size_t)indexOfObject: (id)object { |
︙ | ︙ |
Modified src/OFDictionary.h from [236193e334] to [d1fb2089af].
︙ | ︙ | |||
211 212 213 214 215 216 217 | - (nullable ObjectType)objectForKey: (KeyType)key; - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key; /*! * @brief Returns the value for the given key or `nil` if the key was not * found. * | < < < | > > < < < | | | | 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | - (nullable ObjectType)objectForKey: (KeyType)key; - (nullable ObjectType)objectForKeyedSubscript: (KeyType)key; /*! * @brief Returns the value for the given key or `nil` if the key was not * found. * * This is equivalent to @ref objectForKey:. * * The special key `@count` can be used to retrieve the count as an OFNumber. * * @param key The key whose value should be returned * @return The value for the given key or `nil` if the key was not found */ - (nullable id)valueForKey: (OFString *)key; /*! * @brief Sets a value for a key. * * This is equivalent to OFMutableDictionary#setObject:forKey:. If the * dictionary is immutable, an @ref OFUndefinedKeyException is thrown. * * @param key The key to set * @param value The value to set the key to */ - (void)setValue: (nullable id)value forKey: (OFString *)key; /*! * @brief Checks whether the dictionary contains an object equal to the * specified object. * * @param object The object which is checked for being in the dictionary |
︙ | ︙ |
Modified src/OFDictionary.m from [f5551c8463] to [17f313b4bd].
︙ | ︙ | |||
346 347 348 349 350 351 352 | - (id)objectForKeyedSubscript: (id)key { return [self objectForKey: key]; } - (id)valueForKey: (OFString *)key { | < < < | < | < < < < < < < < < < < < < < < < | 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | - (id)objectForKeyedSubscript: (id)key { return [self objectForKey: key]; } - (id)valueForKey: (OFString *)key { if ([key isEqual: @"@count"]) return [super valueForKey: @"count"]; return [self objectForKey: key]; } - (void)setValue: (id)value forKey: (OFString *)key { if (![self isKindOfClass: [OFMutableDictionary class]]) @throw [OFUndefinedKeyException exceptionWithObject: self key: key value: value]; [(OFMutableDictionary *)self setObject: value forKey: key]; |
︙ | ︙ |
Modified src/OFKeyValueCoding.h from [e174eddd08] to [240832dbe1].
︙ | ︙ | |||
27 28 29 30 31 32 33 | * @brief A protocol for Key Value Coding. * * Key Value Coding makes it possible to access properties dynamically using * the interface described by this protocol. */ @protocol OFKeyValueCoding /*! | | > > > > > > > > | | > > > > > > > > > | 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | * @brief A protocol for Key Value Coding. * * Key Value Coding makes it possible to access properties dynamically using * the interface described by this protocol. */ @protocol OFKeyValueCoding /*! * @brief Returns the value for the specified key. * * @param key The key of the value to return * @return The value for the specified key */ - (nullable id)valueForKey: (OFString *)key; /*! * @brief Returns the value for the specified key path. * * @param keyPath The key path of the value to return * @return The value for the specified key path */ - (nullable id)valueForKeyPath: (OFString *)keyPath; /*! * @brief This is called by @ref valueForKey: if the specified key does not * exist. * * By default, this throws an @ref OFUndefinedKeyException. * * @param key The undefined key of the value to return * @return The value for the specified undefined key */ - (nullable id)valueForUndefinedKey: (OFString *)key; /*! * @brief Set the value for the specified key. * * @param value The value for the specified key * @param key The key of the value to set */ - (void)setValue: (nullable id)value forKey: (OFString *)key; /*! * @brief Set the value for the specified key path. * * @param value The value for the specified key path * @param keyPath The key path of the value to set */ - (void)setValue: (nullable id)value forKeyPath: (OFString *)keyPath; /*! * @brief This is called by @ref setValue:forKey: if the specified key does not * exist. * * By default, this throws an @ref OFUndefinedKeyException. * * @param value The value for the specified undefined key |
︙ | ︙ |
Modified src/OFObject+KeyValueCoding.m from [96857463c3] to [3ce407bdca].
︙ | ︙ | |||
17 18 19 20 21 22 23 24 | #include "config.h" #include <stdlib.h> #import "OFObject.h" #import "OFObject+KeyValueCoding.h" #import "OFMethodSignature.h" | > | | > | > > | > > > | > > | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include "config.h" #include <stdlib.h> #import "OFObject.h" #import "OFObject+KeyValueCoding.h" #import "OFArray.h" #import "OFMethodSignature.h" #import "OFNumber.h" #import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFOutOfMemoryException.h" #import "OFUndefinedKeyException.h" int _OFObject_KeyValueCoding_reference; @implementation OFObject (KeyValueCoding) - (id)valueForKey: (OFString *)key { void *pool = objc_autoreleasePoolPush(); SEL selector = sel_registerName(key.UTF8String); OFMethodSignature *methodSignature = [self methodSignatureForSelector: selector]; id ret; if (methodSignature == nil) { size_t keyLength; char *name; if ((keyLength = key.UTF8StringLength) < 1) { objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } if ((name = malloc(keyLength + 3)) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: keyLength + 3]; @try { memcpy(name, "is", 2); memcpy(name + 2, key.UTF8String, keyLength); name[keyLength + 2] = '\0'; name[2] = of_ascii_toupper(name[2]); selector = sel_registerName(name); } @finally { free(name); } methodSignature = [self methodSignatureForSelector: selector]; if (methodSignature == NULL) { objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } switch (*methodSignature.methodReturnType) { case '@': case '#': objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } } if (methodSignature.numberOfArguments != 2 || *[methodSignature argumentTypeAtIndex: 0] != '@' || *[methodSignature argumentTypeAtIndex: 1] != ':') { objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } switch (*methodSignature.methodReturnType) { case '@': case '#': ret = [self performSelector: selector]; break; #define CASE(encoding, type, method) \ |
︙ | ︙ | |||
103 104 105 106 107 108 109 110 111 112 | CASE('I', unsigned int, numberWithUnsignedInt:) CASE('L', unsigned long, numberWithUnsignedLong:) CASE('Q', unsigned long long, numberWithUnsignedLongLong:) CASE('f', float, numberWithFloat:) CASE('d', double, numberWithDouble:) #undef CASE default: return [self valueForUndefinedKey: key]; } | > > > > > | > > > > > > > > > > > > > > > > > | | | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | CASE('I', unsigned int, numberWithUnsignedInt:) CASE('L', unsigned long, numberWithUnsignedLong:) CASE('Q', unsigned long long, numberWithUnsignedLongLong:) CASE('f', float, numberWithFloat:) CASE('d', double, numberWithDouble:) #undef CASE default: objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (id)valueForKeyPath: (OFString *)keyPath { void *pool = objc_autoreleasePoolPush(); id ret = self; for (OFString *key in [keyPath componentsSeparatedByString: @"."]) ret = [ret valueForKey: key]; [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (id)valueForUndefinedKey: (OFString *)key { @throw [OFUndefinedKeyException exceptionWithObject: self key: key]; } - (void)setValue: (id)value forKey: (OFString *)key { void *pool = objc_autoreleasePoolPush(); size_t keyLength; char *name; SEL selector; OFMethodSignature *methodSignature; const char *valueType; if ((keyLength = key.UTF8StringLength) < 1) { objc_autoreleasePoolPop(pool); [self setValue: value forUndefinedKey: key]; return; } if ((name = malloc(keyLength + 5)) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: keyLength + 5]; |
︙ | ︙ | |||
153 154 155 156 157 158 159 | methodSignature = [self methodSignatureForSelector: selector]; if (methodSignature == nil || methodSignature.numberOfArguments != 3 || *methodSignature.methodReturnType != 'v' || *[methodSignature argumentTypeAtIndex: 0] != '@' || *[methodSignature argumentTypeAtIndex: 1] != ':') { | > | | > | 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | methodSignature = [self methodSignatureForSelector: selector]; if (methodSignature == nil || methodSignature.numberOfArguments != 3 || *methodSignature.methodReturnType != 'v' || *[methodSignature argumentTypeAtIndex: 0] != '@' || *[methodSignature argumentTypeAtIndex: 1] != ':') { objc_autoreleasePoolPop(pool); [self setValue: value forUndefinedKey: key]; return; } valueType = [methodSignature argumentTypeAtIndex: 2]; if (*valueType != '@' && *valueType != '#' && value == nil) { objc_autoreleasePoolPop(pool); [self setNilValueForKey: key]; return; } switch (*valueType) { case '@': case '#': |
︙ | ︙ | |||
198 199 200 201 202 203 204 | CASE('I', unsigned int, unsignedIntValue) CASE('L', unsigned long, unsignedLongValue) CASE('Q', unsigned long long, unsignedLongLongValue) CASE('f', float, floatValue) CASE('d', double, doubleValue) #undef CASE default: | > | | > > > > > > > > > > > > > > > > > > > > > > | 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | CASE('I', unsigned int, unsignedIntValue) CASE('L', unsigned long, unsignedLongValue) CASE('Q', unsigned long long, unsignedLongLongValue) CASE('f', float, floatValue) CASE('d', double, doubleValue) #undef CASE default: objc_autoreleasePoolPop(pool); [self setValue: value forUndefinedKey: key]; return; } objc_autoreleasePoolPop(pool); } - (void)setValue: (id)value forKeyPath: (OFString *)keyPath { void *pool = objc_autoreleasePoolPush(); OFArray *keys = [keyPath componentsSeparatedByString: @"."]; size_t keysCount = keys.count; id object = self; size_t i = 0; for (OFString *key in keys) { if (++i == keysCount) [object setValue: value forKey: key]; else object = [object valueForKey: key]; } objc_autoreleasePoolPop(pool); } - (void)setValue: (id)value forUndefinedKey: (OFString *)key { @throw [OFUndefinedKeyException exceptionWithObject: self key: key |
︙ | ︙ |
Modified src/OFSet.h from [5e61cccf26] to [2986242031].
︙ | ︙ | |||
213 214 215 216 217 218 219 | * @return A boolean whether the set contains the specified object */ - (bool)containsObject: (ObjectType)object; /*! * @brief Returns the value for the specified key * | < < < | > > < < | < | | 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | * @return A boolean whether the set contains the specified object */ - (bool)containsObject: (ObjectType)object; /*! * @brief Returns the value for the specified key * * A new set with the value for the specified key for each object is returned. * * The special key `@count` can be used to retrieve the count as an OFNumber. * * @note Unlike with @ref OFArray, any nil values are removed! * * @param key The key of the value to return * @return The value for the specified key */ - (nullable id)valueForKey: (OFString *)key; /*! * @brief Set the value for the specified key * * @ref setValue:forKey: is called for each object. * * @note A @ref OFNull value is translated to nil! * * @param value The value for the specified key * @param key The key of the value to set */ - (void)setValue: (nullable id)value forKey: (OFString *)key; #ifdef OF_HAVE_BLOCKS /*! * @brief Executes a block for each object in the set. * * @param block The block to execute for each object in the set |
︙ | ︙ |
Modified src/OFSet.m from [136b012e98] to [a05b34b142].
︙ | ︙ | |||
212 213 214 215 216 217 218 | OF_UNRECOGNIZED_SELECTOR } - (id)valueForKey: (OFString *)key { id ret; | < < | < | < < < < < < < < < < < < < < < < | 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | OF_UNRECOGNIZED_SELECTOR } - (id)valueForKey: (OFString *)key { id ret; if ([key isEqual: @"@count"]) return [super valueForKey: @"count"]; ret = [OFMutableSet setWithCapacity: self.count]; for (id object in self) { id value = [object valueForKey: key]; if (value != nil) [ret addObject: value]; } [ret makeImmutable]; return ret; } - (void)setValue: (id)value forKey: (OFString *)key { for (id object in self) [object setValue: value forKey: key]; } - (bool)containsObject: (id)object { |
︙ | ︙ |
Modified tests/OFObjectTests.m from [cc12013248] to [d457d186e1].
︙ | ︙ | |||
246 247 248 249 250 251 252 253 254 255 | [m floatValue] == 110 && [m doubleValue] == 120) EXPECT_EXCEPTION(@"Catch -[setValue:forKey:] with nil key for scalar", OFInvalidArgumentException, [m setValue: (id _Nonnull)nil forKey: @"intValue"]) [pool drain]; } @end | > > > > > > > > > > > > > > | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | [m floatValue] == 110 && [m doubleValue] == 120) EXPECT_EXCEPTION(@"Catch -[setValue:forKey:] with nil key for scalar", OFInvalidArgumentException, [m setValue: (id _Nonnull)nil forKey: @"intValue"]) TEST(@"-[valueForKeyPath:]", (m = [[[MyObj alloc] init] autorelease]) && (m.objectValue = [[[MyObj alloc] init] autorelease]) && R([m.objectValue setObjectValue: [[[MyObj alloc] init] autorelease]]) && R([[m.objectValue objectValue] setDoubleValue: 0.5]) && [[m valueForKeyPath: @"objectValue.objectValue.doubleValue"] doubleValue] == 0.5) TEST(@"[-setValue:forKeyPath:]", R([m setValue: [OFNumber numberWithDouble: 0.75] forKeyPath: @"objectValue.objectValue.doubleValue"]) && [[[m objectValue] objectValue] doubleValue] == 0.75) [pool drain]; } @end |