Oolite
Loading...
Searching...
No Matches
OOCollectionExtractors.h File Reference
import "OOFunctionAttributes.h"
import <Foundation/Foundation.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 427 of file OOCollectionExtractors.h.

427#define OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
428 static inline type OO##typeName##FromObject(id object, type defaultValue) \
429 { \
430 return OOLongLongFromObject(object, defaultValue); \
431 }

◆ 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 432 of file OOCollectionExtractors.h.

432#define OO_ALIAS_CLAMP_PAIR_LONG_LONG(type, typeName) \
433 OO_ALIAS_CLAMP_LONG_LONG(type, typeName) \
434 OO_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 407 of file OOCollectionExtractors.h.

407#define OO_DEFINE_CLAMP(type, typeName, min, max) \
408 OOINLINE type OO##typeName##FromObject(id object, type defaultValue) \
409 { \
410 return (type)OOClampInteger(OOLongLongFromObject(object, defaultValue), min, max); \
411 }

◆ 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 413 of file OOCollectionExtractors.h.

413#define OO_DEFINE_CLAMP_PAIR(type, typeName, minMaxSymb) \
414 OO_DEFINE_CLAMP(type, typeName, minMaxSymb##_MIN, minMaxSymb##_MAX) \
415 OO_DEFINE_CLAMP(unsigned type, Unsigned##typeName, 0, U##minMaxSymb##_MAX)

Function Documentation

◆ OOBooleanFromObject()

BOOL OOBooleanFromObject ( id object,
BOOL defaultValue )

Definition at line 1124 of file OOCollectionExtractors.m.

1125{
1126 BOOL result;
1127
1128 if ([object isKindOfClass:[NSString class]]) {
1129 result = BooleanFromString(object, defaultValue);
1130 } else {
1131 if ([object respondsToSelector:@selector(boolValue)])
1132 result = [object boolValue];
1133 else if ([object respondsToSelector:@selector(intValue)])
1134 result = [object intValue] != 0;
1135 else
1136 result = defaultValue;
1137 }
1138
1139 return result;
1140}
static BOOL BooleanFromString(NSString *string, BOOL defaultValue)

References BooleanFromString().

Referenced by NSArray(OOExtractor)::oo_boolAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_boolForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_boolForKey:defaultValue:, OOOXPVerifier(OOPrivate)::setUpLogOverrides, 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 386 of file OOCollectionExtractors.h.

387{
388 return (minValue < value) ? ((value < maxValue) ? value : maxValue) : minValue;
389}

References OOINLINE.

Referenced by GuiDisplayGen(Internal)::drawEquipmentList:z:, and PlayerEntity(OOControlsPrivate)::handleGameOptionsScreenKeys.

Here is the caller graph for this function:

◆ OODoubleFromObject()

double OODoubleFromObject ( id object,
double defaultValue )

Definition at line 1185 of file OOCollectionExtractors.m.

1186{
1187 double result;
1188
1189 if ([object respondsToSelector:@selector(doubleValue)]) {
1190 result = [object doubleValue];
1191 if (result == 0.0 && [object isKindOfClass:[NSString class]] && !IsZeroString(object))
1192 result = defaultValue;
1193 } else if ([object respondsToSelector:@selector(floatValue)])
1194 result = [object floatValue];
1195 else if ([object respondsToSelector:@selector(intValue)])
1196 result = [object intValue];
1197 else
1198 result = defaultValue;
1199
1200 return result;
1201}
static BOOL IsZeroString(NSString *string)

References IsZeroString().

Referenced by Universe::addEntity:, NSArray(OOExtractor)::oo_doubleAtIndex:, NSArray(OOExtractor)::oo_doubleAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_doubleForKey:, NSUserDefaults(OOExtractor)::oo_doubleForKey:, NSDictionary(OOExtractor)::oo_doubleForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_doubleForKey:defaultValue:, 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 1167 of file OOCollectionExtractors.m.

1168{
1169 float result;
1170
1171 if ([object respondsToSelector:@selector(floatValue)]) {
1172 result = [object floatValue];
1173 if (result == 0.0f && [object isKindOfClass:[NSString class]] && !IsZeroString(object))
1174 result = defaultValue;
1175 } else if ([object respondsToSelector:@selector(doubleValue)])
1176 result = [object doubleValue];
1177 else if ([object respondsToSelector:@selector(intValue)])
1178 result = [object intValue];
1179 else
1180 result = defaultValue;
1181
1182 return result;
1183}

References IsZeroString().

Referenced by NSArray(OOExtractor)::oo_floatAtIndex:, NSArray(OOExtractor)::oo_floatAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_floatForKey:, NSDictionary(OOExtractor)::oo_floatForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_floatForKey:defaultValue:, and 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 1143 of file OOCollectionExtractors.m.

1144{
1145 float probability;
1146
1147 if ([object isKindOfClass:[NSString class]]) {
1148 probability = [object floatValue];
1149
1150 // If our string represents zero, it might be erroneous input or simply yes/no,
1151 // true/false or on/off valid boolean strings. Act on it.
1152 if (probability == 0.0f && !IsZeroString(object)) {
1153 probability = FuzzyBooleanProbabilityFromString(object, defaultValue);
1154 }
1155 } else {
1156 probability = OOFloatFromObject(object, defaultValue);
1157 }
1158
1159 /* This will always be NO for negative values and YES for values
1160 greater than 1, as expected. randf() is always less than 1, so
1161 < is the correct operator here.
1162 */
1163 return randf() < probability;
1164}
static float FuzzyBooleanProbabilityFromString(NSString *string, float defaultValue)
float OOFloatFromObject(id object, float defaultValue)
float randf(void)

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

Referenced by StationEntity::hasShipyard, NSArray(OOExtractor)::oo_fuzzyBooleanAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_fuzzyBooleanForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_fuzzyBooleanForKey:defaultValue:.

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

◆ OOHPVectorFromObject()

HPVector OOHPVectorFromObject ( id object,
HPVector defaultValue )

Definition at line 1266 of file OOCollectionExtractors.m.

1267{
1268 HPVector result = defaultValue;
1269 NSDictionary* dict = nil;
1270
1271 if ([object isKindOfClass:[NSString class]]) {
1272 // This will only write result if a valid vector is found, and will write an error message otherwise.
1273 ScanHPVectorFromString(object, &result);
1274 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 3) {
1275 result.x = [object oo_doubleAtIndex:0];
1276 result.y = [object oo_doubleAtIndex:1];
1277 result.z = [object oo_doubleAtIndex:2];
1278 } else if ([object isKindOfClass:[NSDictionary class]]) {
1279 dict = object;
1280 // Require at least one of the keys x, y, or z
1281 if ([dict objectForKey:@"x"] != nil ||
1282 [dict objectForKey:@"y"] != nil ||
1283 [dict objectForKey:@"z"] != nil) {
1284 // Note: uses 0 for unknown components rather than components of defaultValue.
1285 result.x = [dict oo_doubleForKey:@"x" defaultValue:0.0];
1286 result.y = [dict oo_doubleForKey:@"y" defaultValue:0.0];
1287 result.z = [dict oo_doubleForKey:@"z" defaultValue:0.0];
1288 }
1289 }
1290
1291 return result;
1292}
unsigned count
return nil
BOOL ScanHPVectorFromString(NSString *xyzString, HPVector *outVector)
OOHPScalar x
Definition OOHPVector.h:36
OOHPScalar y
Definition OOHPVector.h:37
OOHPScalar z
Definition OOHPVector.h:38

References count, nil, ScanHPVectorFromString(), HPVector::x, HPVector::y, and HPVector::z.

Referenced by NSDictionary(OOExtractor)::oo_hpvectorForKey:defaultValue:.

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

◆ OOIntegerFromObject()

OOINLINE NSInteger OOIntegerFromObject ( id object,
NSInteger defaultValue )

Definition at line 460 of file OOCollectionExtractors.h.

461{
462 return OOLongFromObject(object, defaultValue);
463}

References OOINLINE.

Referenced by NSArray(OOExtractor)::oo_integerAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_integerForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_integerForKey:defaultValue:.

Here is the caller graph for this function:

◆ OOLongLongFromObject()

long long OOLongLongFromObject ( id object,
long long defaultValue )

Definition at line 1037 of file OOCollectionExtractors.m.

1038{
1039 long long llValue;
1040
1041 if ([object respondsToSelector:@selector(longLongValue)])
1042 llValue = [object longLongValue];
1043 else if ([object respondsToSelector:@selector(longValue)])
1044 llValue = [object longValue];
1045 else if ([object respondsToSelector:@selector(intValue)])
1046 llValue = [object intValue];
1047 else
1048 llValue = defaultValue;
1049
1050 return llValue;
1051}

Referenced by NSArray(OOExtractor)::oo_longLongAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_longLongForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_longLongForKey:defaultValue:, and Verify_Integer().

Here is the caller graph for this function:

◆ OONonNegativeDoubleFromObject()

double OONonNegativeDoubleFromObject ( id object,
double defaultValue )

Definition at line 1219 of file OOCollectionExtractors.m.

1220{
1221 double result;
1222
1223 if ([object respondsToSelector:@selector(doubleValue)])
1224 result = [object doubleValue];
1225 else if ([object respondsToSelector:@selector(floatValue)])
1226 result = [object floatValue];
1227 else if ([object respondsToSelector:@selector(intValue)])
1228 result = [object intValue];
1229 else
1230 return defaultValue; // Don't clamp default
1231
1232 return fmax(result, 0.0);
1233}

Referenced by NSArray(OOExtractor)::oo_nonNegativeDoubleAtIndex:, NSArray(OOExtractor)::oo_nonNegativeDoubleAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_nonNegativeDoubleForKey:, NSUserDefaults(OOExtractor)::oo_nonNegativeDoubleForKey:, NSDictionary(OOExtractor)::oo_nonNegativeDoubleForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_nonNegativeDoubleForKey:defaultValue:.

Here is the caller graph for this function:

◆ OONonNegativeFloatFromObject()

float OONonNegativeFloatFromObject ( id object,
float defaultValue )

Definition at line 1203 of file OOCollectionExtractors.m.

1204{
1205 float result;
1206
1207 if ([object respondsToSelector:@selector(floatValue)])
1208 result = [object floatValue];
1209 else if ([object respondsToSelector:@selector(doubleValue)])
1210 result = [object doubleValue];
1211 else if ([object respondsToSelector:@selector(intValue)])
1212 result = [object intValue];
1213 else
1214 return defaultValue; // Don't clamp default
1215
1216 return fmax(result, 0.0f);
1217}

Referenced by NSArray(OOExtractor)::oo_nonNegativeFloatAtIndex:, NSArray(OOExtractor)::oo_nonNegativeFloatAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_nonNegativeFloatForKey:, NSUserDefaults(OOExtractor)::oo_nonNegativeFloatForKey:, NSDictionary(OOExtractor)::oo_nonNegativeFloatForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_nonNegativeFloatForKey:defaultValue:.

Here is the caller graph for this function:

◆ OOPropertyListFromHPVector()

NSDictionary * OOPropertyListFromHPVector ( HPVector value)

Definition at line 1334 of file OOCollectionExtractors.m.

1335{
1336 return [NSDictionary dictionaryWithObjectsAndKeys:
1337 [NSNumber numberWithDouble:value.x], @"x",
1338 [NSNumber numberWithDouble:value.y], @"y",
1339 [NSNumber numberWithDouble:value.z], @"z",
1340 nil];
1341}

◆ OOPropertyListFromQuaternion()

NSDictionary * OOPropertyListFromQuaternion ( Quaternion value)

Definition at line 1343 of file OOCollectionExtractors.m.

1344{
1345 return [NSDictionary dictionaryWithObjectsAndKeys:
1346 [NSNumber numberWithFloat:value.w], @"w",
1347 [NSNumber numberWithFloat:value.x], @"x",
1348 [NSNumber numberWithFloat:value.y], @"y",
1349 [NSNumber numberWithFloat:value.z], @"z",
1350 nil];
1351}
OOScalar w
OOScalar x
OOScalar y
OOScalar z

◆ OOPropertyListFromVector()

NSDictionary * OOPropertyListFromVector ( Vector value)

Definition at line 1325 of file OOCollectionExtractors.m.

1326{
1327 return [NSDictionary dictionaryWithObjectsAndKeys:
1328 [NSNumber numberWithFloat:value.x], @"x",
1329 [NSNumber numberWithFloat:value.y], @"y",
1330 [NSNumber numberWithFloat:value.z], @"z",
1331 nil];
1332}
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34

◆ OOQuaternionFromObject()

Quaternion OOQuaternionFromObject ( id object,
Quaternion defaultValue )

Definition at line 1294 of file OOCollectionExtractors.m.

1295{
1296 Quaternion result = defaultValue;
1297 NSDictionary* dict = nil;
1298
1299 if ([object isKindOfClass:[NSString class]]) {
1300 // This will only write result if a valid quaternion is found, and will write an error message otherwise.
1301 ScanQuaternionFromString(object, &result);
1302 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 4) {
1303 result.w = [object oo_floatAtIndex:0];
1304 result.x = [object oo_floatAtIndex:1];
1305 result.y = [object oo_floatAtIndex:2];
1306 result.z = [object oo_floatAtIndex:3];
1307 } else if ([object isKindOfClass:[NSDictionary class]]) {
1308 dict = object;
1309 // Require at least one of the keys w, x, y, or z
1310 if ([dict objectForKey:@"w"] != nil ||
1311 [dict objectForKey:@"x"] != nil ||
1312 [dict objectForKey:@"y"] != nil ||
1313 [dict objectForKey:@"z"] != nil) {
1314 // Note: uses 0 for unknown components rather than components of defaultValue.
1315 result.w = [dict oo_floatForKey:@"w" defaultValue:0.0f];
1316 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1317 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1318 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1319 }
1320 }
1321
1322 return result;
1323}
BOOL ScanQuaternionFromString(NSString *wxyzString, Quaternion *outQuaternion)

References count, nil, ScanQuaternionFromString(), Quaternion::w, Quaternion::x, Quaternion::y, and Quaternion::z.

Referenced by NSArray(OOExtractor)::oo_quaternionAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_quaternionForKey:defaultValue:, and 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 465 of file OOCollectionExtractors.h.

466{
467 return OOUnsignedLongFromObject(object, defaultValue);
468}

References OOINLINE.

Referenced by OOEquipmentType(Conveniences)::effectiveTechLevel, NSArray(OOExtractor)::oo_unsignedIntegerAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_unsignedIntegerForKey:defaultValue:, and NSUserDefaults(OOExtractor)::oo_unsignedIntegerForKey:defaultValue:.

Here is the caller graph for this function:

◆ OOUnsignedLongLongFromObject()

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

Definition at line 1053 of file OOCollectionExtractors.m.

1054{
1055 unsigned long long ullValue;
1056
1057 if ([object respondsToSelector:@selector(unsignedLongLongValue)])
1058 ullValue = [object unsignedLongLongValue];
1059 else if ([object respondsToSelector:@selector(unsignedLongValue)])
1060 ullValue = [object unsignedLongValue];
1061 else if ([object respondsToSelector:@selector(unsignedIntValue)])
1062 ullValue = [object unsignedIntValue];
1063 else if ([object respondsToSelector:@selector(intValue)])
1064 ullValue = [object intValue];
1065 else
1066 ullValue = defaultValue;
1067
1068 return ullValue;
1069}

Referenced by NSArray(OOExtractor)::oo_unsignedLongLongAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_unsignedLongLongForKey:defaultValue:, NSUserDefaults(OOExtractor)::oo_unsignedLongLongForKey:defaultValue:, OODeciCreditsFromObject(), and Verify_PositiveInteger().

Here is the caller graph for this function:

◆ OOVectorFromObject()

Vector OOVectorFromObject ( id object,
Vector defaultValue )

Definition at line 1236 of file OOCollectionExtractors.m.

1237{
1238 Vector result = defaultValue;
1239 NSDictionary* dict = nil;
1240
1241 if ([object isKindOfClass:[OONativeVector class]]) {
1242 result = [object getVector];
1243 } else if ([object isKindOfClass:[NSString class]]) {
1244 // This will only write result if a valid vector is found, and will write an error message otherwise.
1245 ScanVectorFromString(object, &result);
1246 } else if ([object isKindOfClass:[NSArray class]] && [object count] == 3) {
1247 result.x = [object oo_floatAtIndex:0];
1248 result.y = [object oo_floatAtIndex:1];
1249 result.z = [object oo_floatAtIndex:2];
1250 } else if ([object isKindOfClass:[NSDictionary class]]) {
1251 dict = object;
1252 // Require at least one of the keys x, y, or z
1253 if ([dict objectForKey:@"x"] != nil ||
1254 [dict objectForKey:@"y"] != nil ||
1255 [dict objectForKey:@"z"] != nil) {
1256 // Note: uses 0 for unknown components rather than components of defaultValue.
1257 result.x = [dict oo_floatForKey:@"x" defaultValue:0.0f];
1258 result.y = [dict oo_floatForKey:@"y" defaultValue:0.0f];
1259 result.z = [dict oo_floatForKey:@"z" defaultValue:0.0f];
1260 }
1261 }
1262
1263 return result;
1264}
BOOL ScanVectorFromString(NSString *xyzString, Vector *outVector)

References count, nil, ScanVectorFromString(), Vector::x, Vector::y, and Vector::z.

Referenced by NSArray(OOExtractor)::oo_vectorAtIndex:defaultValue:, NSDictionary(OOExtractor)::oo_vectorForKey:defaultValue:, and Verify_Vector().

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