Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOCollectionExtractors.h File Reference
import <Foundation/Foundation.h>
import "OOFunctionAttributes.h"
#include <limits.h>
import "OOMaths.h"
+ Include dependency graph for OOCollectionExtractors.h:

Go to the source code of this file.

Classes

category  NSArray(OOExtractor)
 
category  NSDictionary(OOExtractor)
 
category  NSUserDefaults(OOExtractor)
 
category  NSMutableArray(OOInserter)
 
category  NSMutableDictionary(OOInserter)
 
category  NSMutableSet(OOInserter)
 

Macros

#define OO_DEFINE_CLAMP(type, typeName, min, max)
 
#define OO_DEFINE_CLAMP_PAIR(type, typeName, minMaxSymb)
 
#define OO_ALIAS_CLAMP_LONG_LONG(type, typeName)
 
#define OO_ALIAS_CLAMP_PAIR_LONG_LONG(type, typeName)
 

Functions

BOOL OOBooleanFromObject (id object, BOOL defaultValue)
 
BOOL OOFuzzyBooleanFromObject (id object, float defaultValue)
 
float OOFloatFromObject (id object, float defaultValue)
 
double OODoubleFromObject (id object, double defaultValue)
 
float OONonNegativeFloatFromObject (id object, float defaultValue)
 
double OONonNegativeDoubleFromObject (id object, double defaultValue)
 
Vector OOVectorFromObject (id object, Vector defaultValue)
 
HPVector OOHPVectorFromObject (id object, HPVector defaultValue)
 
Quaternion OOQuaternionFromObject (id object, Quaternion defaultValue)
 
NSDictionary * OOPropertyListFromVector (Vector value)
 
NSDictionary * OOPropertyListFromHPVector (HPVector value)
 
NSDictionary * OOPropertyListFromQuaternion (Quaternion value)
 
OOINLINE long long OOClampInteger (long long value, long long minValue, long long maxValue) ALWAYS_INLINE_FUNC
 
long long OOLongLongFromObject (id object, long long defaultValue)
 
unsigned long long OOUnsignedLongLongFromObject (id object, unsigned long long defaultValue)
 
OOINLINE NSInteger OOIntegerFromObject (id object, NSInteger defaultValue)
 
OOINLINE NSInteger OOUIntegerFromObject (id object, NSUInteger defaultValue)
 

Macro Definition Documentation

◆ OO_ALIAS_CLAMP_LONG_LONG

#define OO_ALIAS_CLAMP_LONG_LONG ( type,
typeName )
Value:
static inline type OO##typeName##FromObject(id object, type defaultValue) \
{ \
return OOLongLongFromObject(object, defaultValue); \
}
long long OOLongLongFromObject(id object, long long defaultValue)

Definition at line 444 of file OOCollectionExtractors.h.

444#define OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
445static inline type OO##typeName##FromObject(id object, type defaultValue) \
446{ \
447 return OOLongLongFromObject(object, defaultValue); \
448}

◆ OO_ALIAS_CLAMP_PAIR_LONG_LONG

#define OO_ALIAS_CLAMP_PAIR_LONG_LONG ( type,
typeName )
Value:
OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
OO_ALIAS_CLAMP_LONG_LONG(unsigned type, Unsigned##typeName)
#define OO_ALIAS_CLAMP_LONG_LONG(type, typeName)

Definition at line 449 of file OOCollectionExtractors.h.

449#define OO_ALIAS_CLAMP_PAIR_LONG_LONG(type, typeName) \
450OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
451OO_ALIAS_CLAMP_LONG_LONG(unsigned type, Unsigned##typeName)

◆ OO_DEFINE_CLAMP

#define OO_DEFINE_CLAMP ( type,
typeName,
min,
max )
Value:
OOINLINE type OO ## typeName ## FromObject(id object, type defaultValue) \
{ \
return (type)OOClampInteger(OOLongLongFromObject(object, defaultValue), min, max); \
}
OOINLINE long long OOClampInteger(long long value, long long minValue, long long maxValue) ALWAYS_INLINE_FUNC
#define OOINLINE

Definition at line 424 of file OOCollectionExtractors.h.

424#define OO_DEFINE_CLAMP(type, typeName, min, max) \
425 OOINLINE type OO ## typeName ## FromObject(id object, type defaultValue) \
426 { \
427 return (type)OOClampInteger(OOLongLongFromObject(object, defaultValue), min, max); \
428 }

◆ OO_DEFINE_CLAMP_PAIR

#define OO_DEFINE_CLAMP_PAIR ( type,
typeName,
minMaxSymb )
Value:
OO_DEFINE_CLAMP(type, typeName, minMaxSymb ## _MIN, minMaxSymb ## _MAX) \
OO_DEFINE_CLAMP(unsigned type, Unsigned ## typeName, 0, U ## minMaxSymb ## _MAX)
#define OO_DEFINE_CLAMP(type, typeName, min, max)

Definition at line 430 of file OOCollectionExtractors.h.

430#define OO_DEFINE_CLAMP_PAIR(type, typeName, minMaxSymb) \
431 OO_DEFINE_CLAMP(type, typeName, minMaxSymb ## _MIN, minMaxSymb ## _MAX) \
432 OO_DEFINE_CLAMP(unsigned type, Unsigned ## typeName, 0, U ## minMaxSymb ## _MAX)

Function Documentation

◆ OOBooleanFromObject()

BOOL OOBooleanFromObject ( id object,
BOOL defaultValue )

Definition at line 1297 of file OOCollectionExtractors.m.

1298{
1299 BOOL result;
1300
1301 if ([object isKindOfClass:[NSString class]])
1302 {
1303 result = BooleanFromString(object, defaultValue);
1304 }
1305 else
1306 {
1307 if ([object respondsToSelector:@selector(boolValue)]) result = [object boolValue];
1308 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue] != 0;
1309 else result = defaultValue;
1310 }
1311
1312 return result;
1313}
static BOOL BooleanFromString(NSString *string, BOOL defaultValue)

References BooleanFromString().

Referenced by Verify_Boolean(), and Verify_FuzzyBoolean().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOClampInteger()

OOINLINE long long OOClampInteger ( long long value,
long long minValue,
long long maxValue )

Definition at line 402 of file OOCollectionExtractors.h.

403{
404 return (minValue < value) ? ((value < maxValue) ? value : maxValue) : minValue;
405}

◆ OODoubleFromObject()

double OODoubleFromObject ( id object,
double defaultValue )

Definition at line 1363 of file OOCollectionExtractors.m.

1364{
1365 double result;
1366
1367 if ([object respondsToSelector:@selector(doubleValue)])
1368 {
1369 result = [object doubleValue];
1370 if (result == 0.0 && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1371 }
1372 else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1373 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1374 else result = defaultValue;
1375
1376 return result;
1377}
static BOOL IsZeroString(NSString *string)

References IsZeroString().

Referenced by Verify_Float(), Verify_FuzzyBoolean(), and Verify_PositiveFloat().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOFloatFromObject()

float OOFloatFromObject ( id object,
float defaultValue )

Definition at line 1346 of file OOCollectionExtractors.m.

1347{
1348 float result;
1349
1350 if ([object respondsToSelector:@selector(floatValue)])
1351 {
1352 result = [object floatValue];
1353 if (result == 0.0f && [object isKindOfClass:[NSString class]] && !IsZeroString(object)) result = defaultValue;
1354 }
1355 else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1356 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1357 else result = defaultValue;
1358
1359 return result;
1360}

References IsZeroString().

Referenced by OOFuzzyBooleanFromObject().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOFuzzyBooleanFromObject()

BOOL OOFuzzyBooleanFromObject ( id object,
float defaultValue )

Definition at line 1317 of file OOCollectionExtractors.m.

1318{
1319 float probability;
1320
1321 if ([object isKindOfClass:[NSString class]])
1322 {
1323 probability = [object floatValue];
1324
1325 // If our string represents zero, it might be erroneous input or simply yes/no,
1326 // true/false or on/off valid boolean strings. Act on it.
1327 if (probability == 0.0f && !IsZeroString(object))
1328 {
1329 probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
1330 }
1331 }
1332 else
1333 {
1334 probability = OOFloatFromObject(object, defaultValue);
1335 }
1336
1337 /* This will always be NO for negative values and YES for values
1338 greater than 1, as expected. randf() is always less than 1, so
1339 < is the correct operator here.
1340 */
1341 return randf() < probability;
1342}
static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
float OOFloatFromObject(id object, float defaultValue)
float randf(void)

References FuzzyBooleanProbabilityFromString(), IsZeroString(), OOFloatFromObject(), and randf().

+ Here is the call graph for this function:

◆ OOHPVectorFromObject()

HPVector OOHPVectorFromObject ( id object,
HPVector defaultValue )

Definition at line 1445 of file OOCollectionExtractors.m.

1446{
1447 HPVector result = defaultValue;
1448 NSDictionary *dict = nil;
1449
1450 if ([object isKindOfClass:[NSString class]])
1451 {
1452 // This will only write result if a valid vector is found, and will write an error message otherwise.
1453 ScanHPVectorFromString(object, &result);
1454 }
1455 else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1456 {
1457 result.x = [object oo_doubleAtIndex:0];
1458 result.y = [object oo_doubleAtIndex:1];
1459 result.z = [object oo_doubleAtIndex:2];
1460 }
1461 else if ([object isKindOfClass:[NSDictionary class]])
1462 {
1463 dict = object;
1464 // Require at least one of the keys x, y, or z
1465 if ([dict objectForKey:@"x"] != nil ||
1466 [dict objectForKey:@"y"] != nil ||
1467 [dict objectForKey:@"z"] != nil)
1468 {
1469 // Note: uses 0 for unknown components rather than components of defaultValue.
1470 result.x = [dict oo_doubleForKey:@"x" defaultValue:0.0];
1471 result.y = [dict oo_doubleForKey:@"y" defaultValue:0.0];
1472 result.z = [dict oo_doubleForKey:@"z" defaultValue:0.0];
1473 }
1474 }
1475
1476 return result;
1477}
unsigned count
return nil
BOOL ScanHPVectorFromString(NSString *xyzString, HPVector *outVector)

References count, nil, and ScanHPVectorFromString().

+ Here is the call graph for this function:

◆ OOIntegerFromObject()

OOINLINE NSInteger OOIntegerFromObject ( id object,
NSInteger defaultValue )

Definition at line 478 of file OOCollectionExtractors.h.

479{
480 return OOLongFromObject(object, defaultValue);
481}

◆ OOLongLongFromObject()

long long OOLongLongFromObject ( id object,
long long defaultValue )

Definition at line 1201 of file OOCollectionExtractors.m.

1202{
1203 long long llValue;
1204
1205 if ([object respondsToSelector:@selector(longLongValue)]) llValue = [object longLongValue];
1206 else if ([object respondsToSelector:@selector(longValue)]) llValue = [object longValue];
1207 else if ([object respondsToSelector:@selector(intValue)]) llValue = [object intValue];
1208 else llValue = defaultValue;
1209
1210 return llValue;
1211}

Referenced by Verify_Integer().

+ Here is the caller graph for this function:

◆ OONonNegativeDoubleFromObject()

double OONonNegativeDoubleFromObject ( id object,
double defaultValue )

Definition at line 1393 of file OOCollectionExtractors.m.

1394{
1395 double result;
1396
1397 if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1398 else if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1399 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1400 else return defaultValue; // Don't clamp default
1401
1402 return fmax(result, 0.0);
1403}

◆ OONonNegativeFloatFromObject()

float OONonNegativeFloatFromObject ( id object,
float defaultValue )

Definition at line 1380 of file OOCollectionExtractors.m.

1381{
1382 float result;
1383
1384 if ([object respondsToSelector:@selector(floatValue)]) result = [object floatValue];
1385 else if ([object respondsToSelector:@selector(doubleValue)]) result = [object doubleValue];
1386 else if ([object respondsToSelector:@selector(intValue)]) result = [object intValue];
1387 else return defaultValue; // Don't clamp default
1388
1389 return fmax(result, 0.0f);
1390}

◆ OOPropertyListFromHPVector()

NSDictionary * OOPropertyListFromHPVector ( HPVector value)

Definition at line 1527 of file OOCollectionExtractors.m.

1528{
1529 return [NSDictionary dictionaryWithObjectsAndKeys:
1530 [NSNumber numberWithDouble:value.x], @"x",
1531 [NSNumber numberWithDouble:value.y], @"y",
1532 [NSNumber numberWithDouble:value.z], @"z",
1533 nil];
1534}

◆ OOPropertyListFromQuaternion()

NSDictionary * OOPropertyListFromQuaternion ( Quaternion value)

Definition at line 1537 of file OOCollectionExtractors.m.

1538{
1539 return [NSDictionary dictionaryWithObjectsAndKeys:
1540 [NSNumber numberWithFloat:value.w], @"w",
1541 [NSNumber numberWithFloat:value.x], @"x",
1542 [NSNumber numberWithFloat:value.y], @"y",
1543 [NSNumber numberWithFloat:value.z], @"z",
1544 nil];
1545}

◆ OOPropertyListFromVector()

NSDictionary * OOPropertyListFromVector ( Vector value)

Definition at line 1518 of file OOCollectionExtractors.m.

1519{
1520 return [NSDictionary dictionaryWithObjectsAndKeys:
1521 [NSNumber numberWithFloat:value.x], @"x",
1522 [NSNumber numberWithFloat:value.y], @"y",
1523 [NSNumber numberWithFloat:value.z], @"z",
1524 nil];
1525}

◆ OOQuaternionFromObject()

Quaternion OOQuaternionFromObject ( id object,
Quaternion defaultValue )

Definition at line 1480 of file OOCollectionExtractors.m.

1481{
1482 Quaternion result = defaultValue;
1483 NSDictionary *dict = nil;
1484
1485 if ([object isKindOfClass:[NSString class]])
1486 {
1487 // This will only write result if a valid quaternion is found, and will write an error message otherwise.
1488 ScanQuaternionFromString(object, &result);
1489 }
1490 else if ([object isKindOfClass:[NSArray class]] && [object count] == 4)
1491 {
1492 result.w = [object oo_floatAtIndex:0];
1493 result.x = [object oo_floatAtIndex:1];
1494 result.y = [object oo_floatAtIndex:2];
1495 result.z = [object oo_floatAtIndex:3];
1496 }
1497 else if ([object isKindOfClass:[NSDictionary class]])
1498 {
1499 dict = object;
1500 // Require at least one of the keys w, x, y, or z
1501 if ([dict objectForKey:@"w"] != nil ||
1502 [dict objectForKey:@"x"] != nil ||
1503 [dict objectForKey:@"y"] != nil ||
1504 [dict objectForKey:@"z"] != nil)
1505 {
1506 // Note: uses 0 for unknown components rather than components of defaultValue.
1507 result.w = [dict oo_floatForKey:@"w" defaultValue:0.0f];
1508 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1509 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1510 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1511 }
1512 }
1513
1514 return result;
1515}
BOOL ScanQuaternionFromString(NSString *wxyzString, Quaternion *outQuaternion)

References count, nil, and ScanQuaternionFromString().

Referenced by Verify_Quaternion().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OOUIntegerFromObject()

OOINLINE NSInteger OOUIntegerFromObject ( id object,
NSUInteger defaultValue )

Definition at line 483 of file OOCollectionExtractors.h.

484{
485 return OOUnsignedLongFromObject(object, defaultValue);
486}

◆ OOUnsignedLongLongFromObject()

unsigned long long OOUnsignedLongLongFromObject ( id object,
unsigned long long defaultValue )

Definition at line 1214 of file OOCollectionExtractors.m.

1215{
1216 unsigned long long ullValue;
1217
1218 if ([object respondsToSelector:@selector(unsignedLongLongValue)]) ullValue = [object unsignedLongLongValue];
1219 else if ([object respondsToSelector:@selector(unsignedLongValue)]) ullValue = [object unsignedLongValue];
1220 else if ([object respondsToSelector:@selector(unsignedIntValue)]) ullValue = [object unsignedIntValue];
1221 else if ([object respondsToSelector:@selector(intValue)]) ullValue = [object intValue];
1222 else ullValue = defaultValue;
1223
1224 return ullValue;
1225}

Referenced by PlayerEntity(LoadSave)::addScenarioModel:, and Verify_PositiveInteger().

+ Here is the caller graph for this function:

◆ OOVectorFromObject()

Vector OOVectorFromObject ( id object,
Vector defaultValue )

Definition at line 1407 of file OOCollectionExtractors.m.

1408{
1409 Vector result = defaultValue;
1410 NSDictionary *dict = nil;
1411
1412 if ([object isKindOfClass:[OONativeVector class]])
1413 {
1414 result = [object getVector];
1415 }
1416 else if ([object isKindOfClass:[NSString class]])
1417 {
1418 // This will only write result if a valid vector is found, and will write an error message otherwise.
1419 ScanVectorFromString(object, &result);
1420 }
1421 else if ([object isKindOfClass:[NSArray class]] && [object count] == 3)
1422 {
1423 result.x = [object oo_floatAtIndex:0];
1424 result.y = [object oo_floatAtIndex:1];
1425 result.z = [object oo_floatAtIndex:2];
1426 }
1427 else if ([object isKindOfClass:[NSDictionary class]])
1428 {
1429 dict = object;
1430 // Require at least one of the keys x, y, or z
1431 if ([dict objectForKey:@"x"] != nil ||
1432 [dict objectForKey:@"y"] != nil ||
1433 [dict objectForKey:@"z"] != nil)
1434 {
1435 // Note: uses 0 for unknown components rather than components of defaultValue.
1436 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1437 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1438 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1439 }
1440 }
1441
1442 return result;
1443}
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)

References count, nil, and ScanVectorFromString().

Referenced by Verify_Vector().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: