ObjFW
 All Classes Functions Variables
OFNumber.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012
3  * Jonathan Schleifer <js@webkeks.org>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #ifndef __STDC_LIMIT_MACROS
18 # define __STDC_LIMIT_MACROS
19 #endif
20 #ifndef __STDC_CONSTANT_MACROS
21 # define __STDC_CONSTANT_MACROS
22 #endif
23 
24 #include <sys/types.h>
25 
26 #import "OFObject.h"
27 #import "OFSerialization.h"
28 #import "OFJSONRepresentation.h"
29 
33 typedef enum of_number_type_t {
34  OF_NUMBER_BOOL = 0x01,
35  OF_NUMBER_UCHAR = 0x02,
36  OF_NUMBER_USHORT = 0x03,
37  OF_NUMBER_UINT = 0x04,
38  OF_NUMBER_ULONG = 0x05,
39  OF_NUMBER_SIZE = 0x06,
40  OF_NUMBER_UINT8 = 0x07,
41  OF_NUMBER_UINT16 = 0x08,
42  OF_NUMBER_UINT32 = 0x09,
43  OF_NUMBER_UINT64 = 0x0A,
44  OF_NUMBER_UINTPTR = 0x0B,
45  OF_NUMBER_UINTMAX = 0x0C,
46  OF_NUMBER_SIGNED = 0x10,
47  OF_NUMBER_CHAR = OF_NUMBER_UCHAR | OF_NUMBER_SIGNED,
48  OF_NUMBER_SHORT = OF_NUMBER_USHORT | OF_NUMBER_SIGNED,
49  OF_NUMBER_INT = OF_NUMBER_UINT | OF_NUMBER_SIGNED,
50  OF_NUMBER_LONG = OF_NUMBER_ULONG | OF_NUMBER_SIGNED,
51  OF_NUMBER_INT8 = OF_NUMBER_UINT8 | OF_NUMBER_SIGNED,
52  OF_NUMBER_INT16 = OF_NUMBER_UINT16 | OF_NUMBER_SIGNED,
53  OF_NUMBER_INT32 = OF_NUMBER_UINT32 | OF_NUMBER_SIGNED,
54  OF_NUMBER_INT64 = OF_NUMBER_UINT64 | OF_NUMBER_SIGNED,
55  OF_NUMBER_SSIZE = OF_NUMBER_SIZE | OF_NUMBER_SIGNED,
56  OF_NUMBER_INTMAX = OF_NUMBER_UINTMAX | OF_NUMBER_SIGNED,
57  OF_NUMBER_PTRDIFF = 0x0D | OF_NUMBER_SIGNED,
58  OF_NUMBER_INTPTR = 0x0E | OF_NUMBER_SIGNED,
59  OF_NUMBER_FLOAT = 0x20,
60  OF_NUMBER_DOUBLE = 0x40 | OF_NUMBER_FLOAT,
61 } of_number_type_t;
62 
68 {
69  union of_number_value {
70  BOOL bool_;
71  signed char char_;
72  signed short short_;
73  signed int int_;
74  signed long long_;
75  unsigned char uchar;
76  unsigned short ushort;
77  unsigned int uint;
78  unsigned long ulong;
79  int8_t int8;
80  int16_t int16;
81  int32_t int32;
82  int64_t int64;
83  uint8_t uint8;
84  uint16_t uint16;
85  uint32_t uint32;
86  uint64_t uint64;
87  size_t size;
88  ssize_t ssize;
89  intmax_t intmax;
90  uintmax_t uintmax;
91  ptrdiff_t ptrdiff;
92  intptr_t intptr;
93  uintptr_t uintptr;
94  float float_;
95  double double_;
96  } value;
97  of_number_type_t type;
98 }
99 
100 #ifdef OF_HAVE_PROPERTIES
101 @property (readonly) of_number_type_t type;
102 #endif
103 
110 + (instancetype)numberWithBool: (BOOL)bool_;
111 
118 + (instancetype)numberWithChar: (signed char)char_;
119 
126 + (instancetype)numberWithShort: (signed short)short_;
127 
134 + (instancetype)numberWithInt: (signed int)int_;
135 
142 + (instancetype)numberWithLong: (signed long)long_;
143 
150 + (instancetype)numberWithUnsignedChar: (unsigned char)uchar;
151 
158 + (instancetype)numberWithUnsignedShort: (unsigned short)ushort;
159 
166 + (instancetype)numberWithUnsignedInt: (unsigned int)uint;
167 
174 + (instancetype)numberWithUnsignedLong: (unsigned long)ulong;
175 
182 + (instancetype)numberWithInt8: (int8_t)int8;
183 
190 + (instancetype)numberWithInt16: (int16_t)int16;
191 
198 + (instancetype)numberWithInt32: (int32_t)int32;
199 
206 + (instancetype)numberWithInt64: (int64_t)int64;
207 
214 + (instancetype)numberWithUInt8: (uint8_t)uint8;
215 
222 + (instancetype)numberWithUInt16: (uint16_t)uint16;
223 
230 + (instancetype)numberWithUInt32: (uint32_t)uint32;
231 
238 + (instancetype)numberWithUInt64: (uint64_t)uint64;
239 
246 + (instancetype)numberWithSize: (size_t)size;
247 
254 + (instancetype)numberWithSSize: (ssize_t)ssize;
255 
262 + (instancetype)numberWithIntMax: (intmax_t)intmax;
263 
270 + (instancetype)numberWithUIntMax: (uintmax_t)uintmax;
271 
278 + (instancetype)numberWithPtrDiff: (ptrdiff_t)ptrdiff;
279 
286 + (instancetype)numberWithIntPtr: (intptr_t)intptr;
287 
294 + (instancetype)numberWithUIntPtr: (uintptr_t)uintptr;
295 
302 + (instancetype)numberWithFloat: (float)float_;
303 
310 + (instancetype)numberWithDouble: (double)double_;
311 
318 - initWithBool: (BOOL)bool_;
319 
327 - initWithChar: (signed char)char_;
328 
336 - initWithShort: (signed short)short_;
337 
345 - initWithInt: (signed int)int_;
346 
354 - initWithLong: (signed long)long_;
355 
363 - initWithUnsignedChar: (unsigned char)uchar;
364 
372 - initWithUnsignedShort: (unsigned short)ushort;
373 
381 - initWithUnsignedInt: (unsigned int)uint;
382 
390 - initWithUnsignedLong: (unsigned long)ulong;
391 
398 - initWithInt8: (int8_t)int8;
399 
406 - initWithInt16: (int16_t)int16;
407 
414 - initWithInt32: (int32_t)int32;
415 
422 - initWithInt64: (int64_t)int64;
423 
430 - initWithUInt8: (uint8_t)uint8;
431 
438 - initWithUInt16: (uint16_t)uint16;
439 
446 - initWithUInt32: (uint32_t)uint32;
447 
454 - initWithUInt64: (uint64_t)uint64;
455 
462 - initWithSize: (size_t)size;
463 
470 - initWithSSize: (ssize_t)ssize;
471 
478 - initWithIntMax: (intmax_t)intmax;
479 
487 - initWithUIntMax: (uintmax_t)uintmax;
488 
496 - initWithPtrDiff: (ptrdiff_t)ptrdiff;
497 
504 - initWithIntPtr: (intptr_t)intptr;
505 
513 - initWithUIntPtr: (uintptr_t)uintptr;
514 
521 - initWithFloat: (float)float_;
522 
529 - initWithDouble: (double)double_;
530 
536 - (of_number_type_t)type;
537 
543 - (BOOL)boolValue;
544 
550 - (signed char)charValue;
551 
557 - (signed short)shortValue;
558 
564 - (signed int)intValue;
565 
571 - (signed long)longValue;
572 
578 - (unsigned char)unsignedCharValue;
579 
585 - (unsigned short)unsignedShortValue;
586 
592 - (unsigned int)unsignedIntValue;
593 
599 - (unsigned long)unsignedLongValue;
600 
606 - (int8_t)int8Value;
607 
613 - (int16_t)int16Value;
614 
620 - (int32_t)int32Value;
621 
627 - (int64_t)int64Value;
628 
634 - (uint8_t)uInt8Value;
635 
641 - (uint16_t)uInt16Value;
642 
648 - (uint32_t)uInt32Value;
649 
655 - (uint64_t)uInt64Value;
656 
662 - (size_t)sizeValue;
663 
669 - (ssize_t)sSizeValue;
670 
676 - (intmax_t)intMaxValue;
677 
683 - (uintmax_t)uIntMaxValue;
684 
690 - (ptrdiff_t)ptrDiffValue;
691 
697 - (intptr_t)intPtrValue;
698 
704 - (uintptr_t)uIntPtrValue;
705 
711 - (float)floatValue;
712 
718 - (double)doubleValue;
719 
726 - (OFNumber*)numberByAddingNumber: (OFNumber*)num;
727 
734 - (OFNumber*)numberBySubtractingNumber: (OFNumber*)num;
735 
742 - (OFNumber*)numberByMultiplyingWithNumber: (OFNumber*)num;
743 
750 - (OFNumber*)numberByDividingWithNumber: (OFNumber*)num;
751 
760 - (OFNumber*)numberByANDingWithNumber: (OFNumber*)num;
761 
770 - (OFNumber*)numberByORingWithNumber: (OFNumber*)num;
771 
780 - (OFNumber*)numberByXORingWithNumber: (OFNumber*)num;
781 
792 - (OFNumber*)numberByShiftingLeftWithNumber: (OFNumber*)num;
793 
804 - (OFNumber*)numberByShiftingRightWithNumber: (OFNumber*)num;
805 
811 - (OFNumber*)numberByIncreasing;
812 
818 - (OFNumber*)numberByDecreasing;
819 
827 - (OFNumber*)remainderOfDivisionWithNumber: (OFNumber*)num;
828 @end
829 
830 #ifndef NSINTEGER_DEFINED
831 /* Required for number literals to work */
832 @compatibility_alias NSNumber OFNumber;
833 #endif