Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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 Number *initWithValue(Number *self, const double value) {
130
131 self = (Number *) super(Object, self, init);
132 if (self) {
133 self->value = value;
134 }
135
136 return self;
137}
138
143static int intValue(const Number *self) {
144 return (int) self->value;
145}
146
151static long longValue(const Number *self) {
152 return (long) 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
static Array * init(Array *self)
Definition Array.c:420
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
int HashForDecimal(int hash, const double decimal)
Accumulates the hash value of decimal into hash.
Definition Hash.c:60
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
static Number * numberWithValue(double value)
Definition Number.c:159
static char charValue(const Number *self)
Definition Number.c:88
static double doubleValue(const Number *self)
Definition Number.c:113
static bool isEqual(const Object *self, const Object *other)
Definition Number.c:57
static Order compareTo(const Number *self, const Number *other)
Definition Number.c:96
static short shortValue(const Number *self)
Definition Number.c:167
static String * description(const Object *self)
Definition Number.c:37
static Number * initWithValue(Number *self, const double value)
Definition Number.c:129
static long longValue(const Number *self)
Definition Number.c:151
static bool boolValue(const Number *self)
Definition Number.c:80
static float floatValue(const Number *self)
Definition Number.c:121
static void initialize(Class *clazz)
Definition Number.c:176
Class * _Number(void)
Definition Number.c:198
static int intValue(const Number *self)
Definition Number.c:143
static int hash(const Object *self)
Definition Number.c:47
A wrapper for placing numeric primitives into collections, etc.
Class * _Object(void)
Definition Object.c:136
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
A wrapper for placing numeric primitives into collections, etc.
Definition Number.h:41
short shortValue(const Number *self)
Definition Number.c:167
double value
The backing value.
Definition Number.h:57
int intValue(const Number *self)
Definition Number.c:143
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:55
int hash(const Object *self)
Definition Array.c:129
UTF-8 strings.
Definition String.h:69