Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Number.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25
26#include "Hash.h"
27#include "Number.h"
28#include "String.h"
29
30#define _Class _Number
31
32#pragma mark - Object
33
37static String *description(const Object *self) {
38
39 Number *this = (Number *) self;
40
41 return $(alloc(String), initWithFormat, "%.2f", this->value);
42}
43
47static int hash(const Object *self) {
48
49 Number *this = (Number *) self;
50
51 return HashForDecimal(HASH_SEED, this->value);
52}
53
57static _Bool isEqual(const Object *self, const Object *other) {
58
59 if (super(Object, self, isEqual, other)) {
60 return true;
61 }
62
63 if (other && self->clazz == other->clazz) {
64
65 const Number *this = (Number *) self;
66 const Number *that = (Number *) other;
67
68 return $(this, compareTo, that) == OrderSame;
69 }
70
71 return false;
72}
73
74#pragma mark - Number
75
80static _Bool boolValue(const Number *self) {
81 return self->value ? true : false;
82}
83
88static char charValue(const Number *self) {
89 return (char) self->value;
90}
91
96static Order compareTo(const Number *self, const Number *other) {
97
98 if (other) {
99 if (self->value == other->value) {
100 return OrderSame;
101 }
102
103 return self->value < other->value ? OrderAscending : OrderDescending;
104 }
105
106 return OrderAscending;
107}
108
113static double doubleValue(const Number *self) {
114 return self->value;
115}
116
121static float floatValue(const Number *self) {
122 return (float) self->value;
123}
124
129static long longValue(const Number *self) {
130 return (long) self->value;
131}
132
137static Number *initWithValue(Number *self, const double value) {
138
139 self = (Number *) super(Object, self, init);
140 if (self) {
141 self->value = value;
142 }
143
144 return self;
145}
146
151static int intValue(const Number *self) {
152 return (int) self->value;
153}
154
159static Number *numberWithValue(double value) {
160 return $(alloc(Number), initWithValue, value);
161}
162
167static short shortValue(const Number *self) {
168 return (short) self->value;
169}
170
171#pragma mark - Class lifecycle
172
176static void initialize(Class *clazz) {
177
178 ((ObjectInterface *) clazz->interface)->description = description;
179 ((ObjectInterface *) clazz->interface)->hash = hash;
180 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
181
182 ((NumberInterface *) clazz->interface)->boolValue = boolValue;
183 ((NumberInterface *) clazz->interface)->charValue = charValue;
184 ((NumberInterface *) clazz->interface)->compareTo = compareTo;
185 ((NumberInterface *) clazz->interface)->doubleValue = doubleValue;
186 ((NumberInterface *) clazz->interface)->floatValue = floatValue;
187 ((NumberInterface *) clazz->interface)->longValue = longValue;
188 ((NumberInterface *) clazz->interface)->initWithValue = initWithValue;
189 ((NumberInterface *) clazz->interface)->intValue = intValue;
190 ((NumberInterface *) clazz->interface)->numberWithValue = numberWithValue;
191 ((NumberInterface *) clazz->interface)->shortValue = shortValue;
192}
193
199 static Class *clazz;
200 static Once once;
201
202 do_once(&once, {
203 clazz = _initialize(&(const ClassDef) {
204 .name = "Number",
205 .superclass = _Object(),
206 .instanceSize = sizeof(Number),
207 .interfaceOffset = offsetof(Number, interface),
208 .interfaceSize = sizeof(NumberInterface),
210 });
211 });
212
213 return clazz;
214}
215
216#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
int HashForDecimal(int hash, const double decimal)
Accumulates the hash value of decimal into hash.
Definition: Hash.c:58
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
static void initialize(Class *clazz)
Definition: Number.c:176
A wrapper for placing numeric primitives into collections, etc.
Immutable UTF-8 strings.
Order
Comparison constants.
Definition: Types.h:70
@ OrderSame
Definition: Types.h:72
@ OrderDescending
Definition: Types.h:73
@ OrderAscending
Definition: Types.h:71
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
Order compareTo(const Date *self, const Date *other)
Compares this Date to another.
Definition: Date.c:74
A wrapper for placing numeric primitives into collections, etc.
Definition: Number.h:41
short shortValue(const Number *self)
Definition: Number.c:167
float floatValue(const Number *self)
Definition: Number.c:121
Number * numberWithValue(double value)
Returns a new Number with the given value.
Definition: Number.c:159
double value
The backing value.
Definition: Number.h:57
Number * initWithValue(Number *self, double value)
Initializes this Number with the specified value.
Definition: Number.c:137
_Bool boolValue(const Number *self)
Definition: Number.c:80
char charValue(const Number *self)
Definition: Number.c:88
long longValue(const Number *self)
Definition: Number.c:129
double doubleValue(const Number *self)
Definition: Number.c:113
Class * _Number(void)
The Number archetype.
Definition: Number.c:198
int intValue(const Number *self)
Definition: Number.c:151
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
String * description(const Object *self)
Definition: Array.c:66
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96
int hash(const Object *self)
Definition: Array.c:80
Immutable UTF-8 strings.
Definition: String.h:69