ObjFW
OFNumber.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
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 "objfw-defs.h"
25 
26 #ifdef OF_HAVE_SYS_TYPES_H
27 # include <sys/types.h>
28 #endif
29 
30 #import "OFObject.h"
31 #import "OFSerialization.h"
32 #import "OFJSONRepresentation.h"
33 #import "OFMessagePackRepresentation.h"
34 
35 OF_ASSUME_NONNULL_BEGIN
36 
42 typedef enum {
69  OF_NUMBER_TYPE_SIGNED = 0x10,
72  OF_NUMBER_TYPE_SIGNED,
75  OF_NUMBER_TYPE_SIGNED,
78  OF_NUMBER_TYPE_SIGNED,
81  OF_NUMBER_TYPE_SIGNED,
84  OF_NUMBER_TYPE_SIGNED,
87  OF_NUMBER_TYPE_SIGNED,
90  OF_NUMBER_TYPE_SIGNED,
93  OF_NUMBER_TYPE_SIGNED,
96  OF_NUMBER_TYPE_SIGNED,
99  OF_NUMBER_TYPE_SIGNED,
102  OF_NUMBER_TYPE_SIGNED,
104  OF_NUMBER_TYPE_PTRDIFF = 0x0E | OF_NUMBER_TYPE_SIGNED,
106  OF_NUMBER_TYPE_INTPTR = 0x0F | OF_NUMBER_TYPE_SIGNED,
112 
120 {
121  union of_number_value {
122  bool bool_;
123  signed char sChar;
124  signed short sShort;
125  signed int sInt;
126  signed long sLong;
127  signed long long sLongLong;
128  unsigned char uChar;
129  unsigned short uShort;
130  unsigned int uInt;
131  unsigned long uLong;
132  unsigned long long uLongLong;
133  int8_t int8;
134  int16_t int16;
135  int32_t int32;
136  int64_t int64;
137  uint8_t uInt8;
138  uint16_t uInt16;
139  uint32_t uInt32;
140  uint64_t uInt64;
141  size_t size;
142  ssize_t sSize;
143  intmax_t intMax;
144  uintmax_t uIntMax;
145  ptrdiff_t ptrDiff;
146  intptr_t intPtr;
147  uintptr_t uIntPtr;
148  float float_;
149  double double_;
150  } _value;
151  of_number_type_t _type;
152 }
153 
157 @property (readonly, nonatomic) of_number_type_t type;
165 + (instancetype)numberWithBool: (bool)bool_;
166 
173 + (instancetype)numberWithChar: (signed char)schar;
174 
181 + (instancetype)numberWithShort: (signed short)sshort;
182 
189 + (instancetype)numberWithInt: (signed int)sint;
190 
197 + (instancetype)numberWithLong: (signed long)slong;
198 
205 + (instancetype)numberWithLongLong: (signed long long)slonglong;
206 
213 + (instancetype)numberWithUnsignedChar: (unsigned char)uchar;
214 
221 + (instancetype)numberWithUnsignedShort: (unsigned short)ushort;
222 
229 + (instancetype)numberWithUnsignedInt: (unsigned int)uint;
230 
237 + (instancetype)numberWithUnsignedLong: (unsigned long)ulong;
238 
245 + (instancetype)numberWithUnsignedLongLong: (unsigned long long)ulonglong;
246 
253 + (instancetype)numberWithInt8: (int8_t)int8;
254 
261 + (instancetype)numberWithInt16: (int16_t)int16;
262 
269 + (instancetype)numberWithInt32: (int32_t)int32;
270 
277 + (instancetype)numberWithInt64: (int64_t)int64;
278 
285 + (instancetype)numberWithUInt8: (uint8_t)uint8;
286 
293 + (instancetype)numberWithUInt16: (uint16_t)uint16;
294 
301 + (instancetype)numberWithUInt32: (uint32_t)uint32;
302 
309 + (instancetype)numberWithUInt64: (uint64_t)uint64;
310 
317 + (instancetype)numberWithSize: (size_t)size;
318 
325 + (instancetype)numberWithSSize: (ssize_t)ssize;
326 
333 + (instancetype)numberWithIntMax: (intmax_t)intmax;
334 
341 + (instancetype)numberWithUIntMax: (uintmax_t)uintmax;
342 
349 + (instancetype)numberWithPtrDiff: (ptrdiff_t)ptrdiff;
350 
357 + (instancetype)numberWithIntPtr: (intptr_t)intptr;
358 
365 + (instancetype)numberWithUIntPtr: (uintptr_t)uintptr;
366 
373 + (instancetype)numberWithFloat: (float)float_;
374 
381 + (instancetype)numberWithDouble: (double)double_;
382 
383 - init OF_UNAVAILABLE;
384 
391 - initWithBool: (bool)bool_;
392 
400 - initWithChar: (signed char)schar;
401 
409 - initWithShort: (signed short)sshort;
410 
418 - initWithInt: (signed int)sint;
419 
427 - initWithLong: (signed long)slong;
428 
436 - initWithLongLong: (signed long long)slonglong;
437 
445 - initWithUnsignedChar: (unsigned char)uchar;
446 
454 - initWithUnsignedShort: (unsigned short)ushort;
455 
463 - initWithUnsignedInt: (unsigned int)uint;
464 
472 - initWithUnsignedLong: (unsigned long)ulong;
473 
481 - initWithUnsignedLongLong: (unsigned long long)ulonglong;
482 
489 - initWithInt8: (int8_t)int8;
490 
497 - initWithInt16: (int16_t)int16;
498 
505 - initWithInt32: (int32_t)int32;
506 
513 - initWithInt64: (int64_t)int64;
514 
521 - initWithUInt8: (uint8_t)uint8;
522 
529 - initWithUInt16: (uint16_t)uint16;
530 
537 - initWithUInt32: (uint32_t)uint32;
538 
545 - initWithUInt64: (uint64_t)uint64;
546 
553 - initWithSize: (size_t)size;
554 
561 - initWithSSize: (ssize_t)ssize;
562 
569 - initWithIntMax: (intmax_t)intmax;
570 
578 - initWithUIntMax: (uintmax_t)uintmax;
579 
587 - initWithPtrDiff: (ptrdiff_t)ptrdiff;
588 
595 - initWithIntPtr: (intptr_t)intptr;
596 
604 - initWithUIntPtr: (uintptr_t)uintptr;
605 
612 - initWithFloat: (float)float_;
613 
620 - initWithDouble: (double)double_;
621 
627 - (bool)boolValue;
628 
634 - (signed char)charValue;
635 
641 - (signed short)shortValue;
642 
648 - (signed int)intValue;
649 
655 - (signed long)longValue;
656 
662 - (signed long long)longLongValue;
663 
669 - (unsigned char)unsignedCharValue;
670 
676 - (unsigned short)unsignedShortValue;
677 
683 - (unsigned int)unsignedIntValue;
684 
690 - (unsigned long)unsignedLongValue;
691 
697 - (unsigned long long)unsignedLongLongValue;
698 
704 - (int8_t)int8Value;
705 
711 - (int16_t)int16Value;
712 
718 - (int32_t)int32Value;
719 
725 - (int64_t)int64Value;
726 
732 - (uint8_t)uInt8Value;
733 
739 - (uint16_t)uInt16Value;
740 
746 - (uint32_t)uInt32Value;
747 
753 - (uint64_t)uInt64Value;
754 
760 - (size_t)sizeValue;
761 
767 - (ssize_t)sSizeValue;
768 
774 - (intmax_t)intMaxValue;
775 
781 - (uintmax_t)uIntMaxValue;
782 
788 - (ptrdiff_t)ptrDiffValue;
789 
795 - (intptr_t)intPtrValue;
796 
802 - (uintptr_t)uIntPtrValue;
803 
809 - (float)floatValue;
810 
816 - (double)doubleValue;
817 @end
818 
819 OF_ASSUME_NONNULL_END
820 
821 #if !defined(NSINTEGER_DEFINED) && !__has_feature(modules)
822 /* Required for number literals to work */
823 @compatibility_alias NSNumber OFNumber;
824 #endif
Definition: OFNumber.h:92
of_number_type_t
The C type of a number stored in an OFNumber.
Definition: OFNumber.h:42
signed char charValue()
Returns the OFNumber as a signed char.
Definition: OFNumber.m:599
Definition: OFNumber.h:104
Definition: OFNumber.h:80
unsigned char unsignedCharValue()
Returns the OFNumber as an unsigned char.
Definition: OFNumber.m:624
Definition: OFNumber.h:68
Definition: OFNumber.h:48
ptrdiff_t ptrDiffValue()
Returns the OFNumber as a ptrdiff_t.
Definition: OFNumber.m:709
uintmax_t uIntMaxValue()
Returns the OFNumber as a uintmax_t.
Definition: OFNumber.m:704
signed long long longLongValue()
Returns the OFNumber as a signed long long.
Definition: OFNumber.m:619
Definition: OFNumber.h:62
Definition: OFNumber.h:77
Definition: OFNumber.h:58
Definition: OFNumber.h:50
uint32_t uInt32Value()
Returns the OFNumber as a uint32_t.
Definition: OFNumber.m:679
float floatValue()
Returns the OFNumber as a float.
Definition: OFNumber.m:724
Definition: OFNumber.h:54
intptr_t intPtrValue()
Returns the OFNumber as an intptr_t.
Definition: OFNumber.m:714
uint8_t uInt8Value()
Returns the OFNumber as a uint8_t.
Definition: OFNumber.m:669
A protocol implemented by classes that support encoding to a JSON representation. ...
Definition: OFJSONRepresentation.h:38
Definition: OFNumber.h:52
signed int intValue()
Returns the OFNumber as a signed int.
Definition: OFNumber.m:609
of_number_type_t type
Definition: OFNumber.h:158
Definition: OFNumber.h:101
Definition: OFNumber.h:44
id init()
Initializes an already allocated object.
Definition: OFObject.m:488
Definition: OFNumber.h:86
unsigned long long unsignedLongLongValue()
Returns the OFNumber as an unsigned long long.
Definition: OFNumber.m:644
A protocol implemented by classes that support encoding to a MessagePack representation.
Definition: OFMessagePackRepresentation.h:30
unsigned int unsignedIntValue()
Returns the OFNumber as an unsigned int.
Definition: OFNumber.m:634
int16_t int16Value()
Returns the OFNumber as an int16_t.
Definition: OFNumber.m:654
int8_t int8Value()
Returns the OFNumber as an int8_t.
Definition: OFNumber.m:649
Provides a way to store a number in an object.
Definition: OFNumber.h:118
Definition: OFNumber.h:71
double doubleValue()
Returns the OFNumber as a double.
Definition: OFNumber.m:729
A protocol for the creation of copies.
Definition: OFObject.h:914
intmax_t intMaxValue()
Returns the OFNumber as an intmax_t.
Definition: OFNumber.m:699
int64_t int64Value()
Returns the OFNumber as an int64_t.
Definition: OFNumber.m:664
Definition: OFNumber.h:110
uintptr_t uIntPtrValue()
Returns the OFNumber as a uintptr_t.
Definition: OFNumber.m:719
Definition: OFNumber.h:64
A protocol for comparing objects.
unsigned long unsignedLongValue()
Returns the OFNumber as an unsigned long.
Definition: OFNumber.m:639
Definition: OFNumber.h:66
The root class for all other classes inside ObjFW.
Definition: OFObject.h:379
signed short shortValue()
Returns the OFNumber as a signed short.
Definition: OFNumber.m:604
Definition: OFNumber.h:83
Definition: OFNumber.h:106
uint16_t uInt16Value()
Returns the OFNumber as a uint16_t.
Definition: OFNumber.m:674
Definition: OFNumber.h:108
A protocol for serializing objects.
Definition: OFSerialization.h:30
Definition: OFNumber.h:60
signed long longValue()
Returns the OFNumber as a signed long.
Definition: OFNumber.m:614
Definition: OFNumber.h:46
size_t sizeValue()
Returns the OFNumber as a size_t.
Definition: OFNumber.m:689
int32_t int32Value()
Returns the OFNumber as an int32_t.
Definition: OFNumber.m:659
Definition: OFNumber.h:56
unsigned short unsignedShortValue()
Returns the OFNumber as an unsigned short.
Definition: OFNumber.m:629
Definition: OFNumber.h:74
uint64_t uInt64Value()
Returns the OFNumber as a uint64_t.
Definition: OFNumber.m:684
Definition: OFNumber.h:89
Definition: OFNumber.h:98
Definition: OFNumber.h:95
ssize_t sSizeValue()
Returns the OFNumber as an ssize_t.
Definition: OFNumber.m:694
bool boolValue()
Returns the OFNumber as a bool.
Definition: OFNumber.m:594