ObjFW  Check-in [0ec87765f4]

Overview
Comment:Add support for BOOL to OFNumber.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0ec87765f41b5d34c30d226e3e14639a41d73242864a122fd93c86c3a4a012c8
User & Date: js on 2010-11-13 16:15:16
Other Links: manifest | tags
Context
2010-11-13
20:48
Add +[OFDataArray dataArrayWithContentsOfFile:]. check-in: 1c4c60ac36 user: js tags: trunk
16:15
Add support for BOOL to OFNumber. check-in: 0ec87765f4 user: js tags: trunk
2010-11-12
20:13
Add tests for blocks. check-in: d7c2d46b2b user: js tags: trunk
Changes

Modified src/OFNumber.h from [60fe00fa34] to [15976a5400].

10
11
12
13
14
15
16

17
18
19
20
21
22
23
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24







+







 */

#include <unistd.h>

#import "OFObject.h"

enum of_number_type {
	OF_NUMBER_BOOL,
	OF_NUMBER_CHAR,
	OF_NUMBER_SHORT,
	OF_NUMBER_INT,
	OF_NUMBER_LONG,
	OF_NUMBER_UCHAR,
	OF_NUMBER_USHORT,
	OF_NUMBER_UINT,
43
44
45
46
47
48
49

50
51
52
53
54
55
56
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58







+








/**
 * \brief Provides a way to store a number in an object.
 */
@interface OFNumber: OFObject <OFCopying>
{
	union {
		BOOL	       bool_;
		char	       char_;
		short	       short_;
		int	       int_;
		long	       long_;
		unsigned char  uchar;
		unsigned short ushort;
		unsigned int   uint;
72
73
74
75
76
77
78






79
80
81
82
83
84
85
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93







+
+
+
+
+
+







		uintptr_t      uintptr;
		float	       float_;
		double	       double_;
	} value;
	enum of_number_type type;
}

/**
 * \param bool_ A BOOL which the OFNumber should contain
 * \return A new autoreleased OFNumber
 */
+ numberWithBool: (BOOL)bool_;

/**
 * \param char_ A char which the OFNumber should contain
 * \return A new autoreleased OFNumber
 */
+ numberWithChar: (char)char_;

/**
222
223
224
225
226
227
228








229
230
231
232
233
234
235
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251







+
+
+
+
+
+
+
+








/**
 * \param double_ A double which the OFNumber should contain
 * \return A new autoreleased OFNumber
 */
+ numberWithDouble: (double)double_;

/**
 * Initializes an already allocated OFNumber with the specified BOOL.
 *
 * \param bool_ A BOOL which the OFNumber should contain
 * \return An initialized OFNumber
 */
- initWithBool: (BOOL)bool_;

/**
 * Initializes an already allocated OFNumber with the specified char.
 *
 * \param char_ A char which the OFNumber should contain
 * \return An initialized OFNumber
 */
- initWithChar: (char)char_;
428
429
430
431
432
433
434





435
436
437
438
439
440
441
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462







+
+
+
+
+








/**
 * \return An enum of type of_number_type indicating the type of contained
 *	   number of the OFNumber
 */
- (enum of_number_type)type;

/**
 * \return The OFNumber as a BOOL
 */
- (BOOL)boolValue;

/**
 * \return The OFNumber as a char
 */
- (char)charValue;

/**
 * \return The OFNumber as a short

Modified src/OFNumber.m from [7d94808be1] to [f20700cf69].

15
16
17
18
19
20
21


22
23
24
25
26
27
28
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+
+








#import "OFNumber.h"
#import "OFExceptions.h"
#import "macros.h"

#define RETURN_AS(t)							\
	switch (type) {							\
	case OF_NUMBER_BOOL:						\
		return (t)value.bool_;					\
	case OF_NUMBER_CHAR:						\
		return (t)value.char_;					\
	case OF_NUMBER_SHORT:						\
		return (t)value.short_;					\
	case OF_NUMBER_INT:						\
		return (t)value.int_;					\
	case OF_NUMBER_LONG:						\
70
71
72
73
74
75
76



77
78
79
80
81
82
83
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88







+
+
+







	case OF_NUMBER_DOUBLE:						\
		return (t)value.double_;				\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}
#define CALCULATE(o, n)							\
	switch (type) { 						\
	case OF_NUMBER_BOOL:						\
		return [OFNumber numberWithBool:			\
		    value.bool_ o [n boolValue]];			\
	case OF_NUMBER_CHAR:						\
		return [OFNumber numberWithChar:			\
		    value.char_ o [n charValue]];			\
	case OF_NUMBER_SHORT:						\
		return [OFNumber numberWithShort:			\
		    value.short_ o [n shortValue]];			\
	case OF_NUMBER_INT:						\
150
151
152
153
154
155
156



157
158
159
160
161
162
163
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171







+
+
+







		return [OFNumber numberWithDouble:			\
		    value.double_ o [n doubleValue]];			\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}
#define CALCULATE2(o, n)						\
	switch (type) { 						\
	case OF_NUMBER_BOOL:						\
		return [OFNumber numberWithBool:			\
		    value.bool_ o [n boolValue]];			\
	case OF_NUMBER_CHAR:						\
		return [OFNumber numberWithChar:			\
		    value.char_ o [n charValue]];			\
	case OF_NUMBER_SHORT:						\
		return [OFNumber numberWithShort:			\
		    value.short_ o [n shortValue]];			\
	case OF_NUMBER_INT:						\
228
229
230
231
232
233
234


235
236
237
238
239
240
241
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251







+
+







		@throw [OFNotImplementedException newWithClass: isa	\
						      selector: _cmd];	\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}
#define CALCULATE3(o)							\
	switch (type) {							\
	case OF_NUMBER_BOOL:						\
		return [OFNumber numberWithBool: value.bool_ o];	\
	case OF_NUMBER_CHAR:						\
		return [OFNumber numberWithChar: value.char_ o];	\
	case OF_NUMBER_SHORT:						\
		return [OFNumber numberWithShort: value.short_ o];	\
	case OF_NUMBER_INT:						\
		return [OFNumber numberWithInt: value.int_ o];		\
	case OF_NUMBER_LONG:						\
286
287
288
289
290
291
292





293
294
295
296
297
298
299
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314







+
+
+
+
+







	case OF_NUMBER_DOUBLE:						\
		return [OFNumber numberWithDouble: value.double_ o];	\
	default:							\
		@throw [OFInvalidFormatException newWithClass: isa];	\
	}

@implementation OFNumber
+ numberWithBool: (BOOL)bool_
{
	return [[[self alloc] initWithBool: bool_] autorelease];
}

+ numberWithChar: (char)char_
{
	return [[[self alloc] initWithChar: char_] autorelease];
}

+ numberWithShort: (short)short_
{
416
417
418
419
420
421
422










423
424
425
426
427
428
429
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454







+
+
+
+
+
+
+
+
+
+







}

- init
{
	@throw [OFNotImplementedException newWithClass: isa
					      selector: _cmd];
}

- initWithBool: (BOOL)bool_
{
	self = [super init];

	value.bool_ = bool_;
	type = OF_NUMBER_BOOL;

	return self;
}

- initWithChar: (char)char_
{
	self = [super init];

	value.char_ = char_;
	type = OF_NUMBER_CHAR;
671
672
673
674
675
676
677





678
679
680
681
682
683
684
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714







+
+
+
+
+







	return self;
}

- (enum of_number_type)type
{
	return type;
}

- (BOOL)boolValue
{
	RETURN_AS(BOOL)
}

- (char)charValue
{
	RETURN_AS(char)
}

- (short)shortValue