Overview
Comment: | OFNumber: Add singletons for 0, 1, 2, true & false |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
d8123a1f2647584d8d65b8fe2df3977d |
User & Date: | js on 2020-07-19 16:12:41 |
Other Links: | manifest | tags |
Context
2020-08-10
| ||
20:55 | ofhttp: Average the speed over the last 10 seconds check-in: 30e8df31a0 user: js tags: trunk | |
2020-07-19
| ||
16:17 | Merge trunk into branch "tagged-pointers" check-in: e2a53689c6 user: js tags: tagged-pointers | |
16:12 | OFNumber: Add singletons for 0, 1, 2, true & false check-in: d8123a1f26 user: js tags: trunk | |
14:16 | Disable a Clang warning preventing limit checks check-in: bb73116ce2 user: js tags: trunk | |
Changes
Modified src/OFNumber.m from [537e45b1d4] to [57186c2c7a].
︙ | ︙ | |||
27 28 29 30 31 32 33 | #import "OFXMLAttribute.h" #import "OFData.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" | < < < < < < < < < < < < < < < < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > | | < | | | | | < | | > > > > > > > > > > > > > > > > > > < | < | < | < | | | | | | | | | < | < | < | | | | < | | | < | | < | | | < | < | < | < | | > > > > > > > > > > > > > > > > > > > > > | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 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 272 273 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | #import "OFXMLAttribute.h" #import "OFData.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" @interface OFNumber () + (instancetype)of_alloc; - (OFString *)of_JSONRepresentationWithOptions: (int)options depth: (size_t)depth; @end @interface OFNumberPlaceholder: OFNumber @end @interface OFNumberSingleton: OFNumber @end static struct { Class isa; } placeholder; static OFNumberSingleton *zeroNumber, *oneNumber, *twoNumber; static OFNumberSingleton *trueNumber, *falseNumber; static void initZeroNumber(void) { zeroNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 0]; } static void initOneNumber(void) { oneNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 1]; } static void initTwoNumber(void) { twoNumber = [[OFNumberSingleton alloc] initWithUnsignedChar: 2]; } static void initTrueNumber(void) { trueNumber = [[OFNumberSingleton alloc] initWithBool: true]; } static void initFalseNumber(void) { falseNumber = [[OFNumberSingleton alloc] initWithBool: false]; } @implementation OFNumberPlaceholder - (instancetype)initWithBool: (bool)bool_ { if (bool_) { static of_once_t once; of_once(&once, initTrueNumber); return (id)trueNumber; } else { static of_once_t once; of_once(&once, initFalseNumber); return (id)falseNumber; } } - (instancetype)initWithChar: (signed char)sChar { if (sChar >= 0) return [self initWithUnsignedChar: sChar]; return (id)[[OFNumber of_alloc] initWithChar: sChar]; } - (instancetype)initWithShort: (short)sShort { if (sShort >= 0) return [self initWithUnsignedShort: sShort]; if (sShort >= SCHAR_MIN) return [self initWithChar: (signed char)sShort]; return (id)[[OFNumber of_alloc] initWithShort: sShort]; } - (instancetype)initWithInt: (int)sInt { if (sInt >= 0) return [self initWithUnsignedInt: sInt]; if (sInt >= SHRT_MIN) return [self initWithShort: (short)sInt]; return (id)[[OFNumber of_alloc] initWithInt: sInt]; } - (instancetype)initWithLong: (long)sLong { if (sLong >= 0) return [self initWithUnsignedLong: sLong]; if (sLong >= INT_MIN) return [self initWithShort: (int)sLong]; return (id)[[OFNumber of_alloc] initWithLong: sLong]; } - (instancetype)initWithLongLong: (long long)sLongLong { if (sLongLong >= 0) return [self initWithUnsignedLongLong: sLongLong]; if (sLongLong >= LONG_MIN) return [self initWithLong: (long)sLongLong]; return (id)[[OFNumber of_alloc] initWithLongLong: sLongLong]; } - (instancetype)initWithUnsignedChar: (unsigned char)uChar { switch (uChar) { case 0: { static of_once_t once = OF_ONCE_INIT; of_once(&once, initZeroNumber); return (id)zeroNumber; } case 1: { static of_once_t once = OF_ONCE_INIT; of_once(&once, initOneNumber); return (id)oneNumber; } case 2: { static of_once_t once = OF_ONCE_INIT; of_once(&once, initTwoNumber); return (id)twoNumber; } } return (id)[[OFNumber of_alloc] initWithUnsignedChar: uChar]; } - (instancetype)initWithUnsignedShort: (unsigned short)uShort { if (uShort <= UCHAR_MAX) return [self initWithUnsignedChar: (unsigned char)uShort]; return (id)[[OFNumber of_alloc] initWithUnsignedShort: uShort]; } - (instancetype)initWithUnsignedInt: (unsigned int)uInt { if (uInt <= USHRT_MAX) return [self initWithUnsignedShort: (unsigned short)uInt]; return (id)[[OFNumber of_alloc] initWithUnsignedInt: uInt]; } - (instancetype)initWithUnsignedLong: (unsigned long)uLong { if (uLong <= UINT_MAX) return [self initWithUnsignedInt: (unsigned int)uLong]; return (id)[[OFNumber of_alloc] initWithUnsignedLong: uLong]; } - (instancetype)initWithUnsignedLongLong: (unsigned long long)uLongLong { if (uLongLong <= ULONG_MAX) return [self initWithUnsignedLong: (unsigned long)uLongLong]; return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: uLongLong]; } - (instancetype)initWithInt8: (int8_t)int8 { if (int8 >= 0) return [self initWithUInt8: int8]; return (id)[[OFNumber of_alloc] initWithInt8: int8]; } - (instancetype)initWithInt16: (int16_t)int16 { if (int16 >= 0) return [self initWithUInt16: int16]; if (int16 >= INT8_MIN) return [self initWithInt8: (int8_t)int16]; return (id)[[OFNumber of_alloc] initWithInt16: int16]; } - (instancetype)initWithInt32: (int32_t)int32 { if (int32 >= 0) return [self initWithUInt32: int32]; if (int32 >= INT16_MIN) return [self initWithInt16: (int16_t)int32]; return (id)[[OFNumber of_alloc] initWithInt32: int32]; } - (instancetype)initWithInt64: (int64_t)int64 { if (int64 >= 0) return [self initWithUInt64: int64]; if (int64 >= INT32_MIN) return [self initWithInt32: (int32_t)int64]; return (id)[[OFNumber of_alloc] initWithInt64: int64]; } - (instancetype)initWithUInt8: (uint8_t)uInt8 { return (id)[[OFNumber of_alloc] initWithUInt8: uInt8]; } - (instancetype)initWithUInt16: (uint16_t)uInt16 { if (uInt16 <= UINT8_MAX) return [self initWithUInt8: (uint8_t)uInt16]; return (id)[[OFNumber of_alloc] initWithUInt16: uInt16]; } - (instancetype)initWithUInt32: (uint32_t)uInt32 { if (uInt32 <= UINT16_MAX) return [self initWithUInt16: (uint16_t)uInt32]; return (id)[[OFNumber of_alloc] initWithUInt32: uInt32]; } - (instancetype)initWithUInt64: (uint64_t)uInt64 { if (uInt64 <= UINT32_MAX) return [self initWithUInt32: (uint32_t)uInt64]; return (id)[[OFNumber of_alloc] initWithUInt64: uInt64]; } - (instancetype)initWithSize: (size_t)size { if (size <= ULONG_MAX) return [self initWithUnsignedLong: (unsigned long)size]; return (id)[[OFNumber of_alloc] initWithSize: size]; } - (instancetype)initWithSSize: (ssize_t)sSize { if (sSize >= 0) return [self initWithSize: sSize]; if (sSize <= LONG_MIN) return [self initWithLong: (long)sSize]; return (id)[[OFNumber of_alloc] initWithSSize: sSize]; } - (instancetype)initWithIntMax: (intmax_t)intMax { if (intMax >= 0) return [self initWithUIntMax: intMax]; if (intMax <= LLONG_MIN) return [self initWithLongLong: (long long)intMax]; return (id)[[OFNumber of_alloc] initWithIntMax: intMax]; } - (instancetype)initWithUIntMax: (uintmax_t)uIntMax { if (uIntMax <= ULLONG_MAX) return [self initWithUnsignedLongLong: (unsigned long long)uIntMax]; return (id)[[OFNumber of_alloc] initWithUIntMax: uIntMax]; } #ifdef __clang__ /* * This warning should probably not exist at all, as it prevents checking * whether one type fits into another in a portable way. */ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare" #endif - (instancetype)initWithPtrDiff: (ptrdiff_t)ptrDiff { if (ptrDiff >= LLONG_MIN && ptrDiff <= LLONG_MAX) return [self initWithLongLong: (long long)ptrDiff]; return (id)[[OFNumber of_alloc] initWithPtrDiff: ptrDiff]; } - (instancetype)initWithIntPtr: (intptr_t)intPtr { if (intPtr >= 0) return [self initWithUIntPtr: intPtr]; if (intPtr >= LLONG_MIN) return [self initWithLongLong: (long long)intPtr]; return (id)[[OFNumber of_alloc] initWithIntPtr: intPtr]; } - (instancetype)initWithUIntPtr: (uintptr_t)uIntPtr { if (uIntPtr <= ULLONG_MAX) return [self initWithUnsignedLongLong: (unsigned long long)uIntPtr]; return (id)[[OFNumber of_alloc] initWithUIntPtr: uIntPtr]; } #ifdef __clang__ # pragma clang diagnostic pop #endif - (instancetype)initWithFloat: (float)float_ { if (float_ == (uintmax_t)float_) return [self initWithUIntMax: (uintmax_t)float_]; if (float_ == (intmax_t)float_) return [self initWithIntMax: (intmax_t)float_]; return (id)[[OFNumber of_alloc] initWithFloat: float_]; } - (instancetype)initWithDouble: (double)double_ { if (double_ == (uintmax_t)double_) return [self initWithUIntMax: (uintmax_t)double_]; if (double_ == (intmax_t)double_) return [self initWithIntMax: (intmax_t)double_]; if (double_ == (float)double_) return [self initWithFloat: (float)double_]; return (id)[[OFNumber of_alloc] initWithDouble: double_]; } - (instancetype)initWithSerialization: (OFXMLElement *)element { return (id)[[OFNumber of_alloc] initWithSerialization: element]; } @end @implementation OFNumberSingleton - (instancetype)autorelease { return self; } - (instancetype)retain { return self; } - (void)release { } - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } @end @implementation OFNumber + (void)initialize { if (self == [OFNumber class]) placeholder.isa = [OFNumberPlaceholder class]; } |
︙ | ︙ | |||
888 889 890 891 892 893 894 895 896 897 898 899 900 901 | CASE('d', double, float_) #undef CASE default: @throw [OFInvalidFormatException exception]; } } - (bool)boolValue { RETURN_AS(bool) } - (signed char)charValue { | > > > > > > > > > > > | 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 | CASE('d', double, float_) #undef CASE default: @throw [OFInvalidFormatException exception]; } } #define RETURN_AS(t) \ switch (_type) { \ case OF_NUMBER_TYPE_FLOAT: \ return (t)_value.float_; \ case OF_NUMBER_TYPE_SIGNED: \ return (t)_value.signed_; \ case OF_NUMBER_TYPE_UNSIGNED: \ return (t)_value.unsigned_; \ default: \ @throw [OFInvalidFormatException exception]; \ } - (bool)boolValue { RETURN_AS(bool) } - (signed char)charValue { |
︙ | ︙ | |||
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | RETURN_AS(float) } - (double)doubleValue { RETURN_AS(double) } - (bool)isEqual: (id)object { OFNumber *number; if (object == self) return true; | > | 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | RETURN_AS(float) } - (double)doubleValue { RETURN_AS(double) } #undef RETURN_AS - (bool)isEqual: (id)object { OFNumber *number; if (object == self) return true; |
︙ | ︙ |