Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Number.c File Reference
#include <assert.h>
#include "Hash.h"
#include "Number.h"
#include "String.h"

Go to the source code of this file.

Macros

#define _Class   _Number
 

Functions

Class_Number (void)
 
static bool boolValue (const Number *self)
 
static char charValue (const Number *self)
 
static Order compareTo (const Number *self, const Number *other)
 
static Stringdescription (const Object *self)
 
static double doubleValue (const Number *self)
 
static float floatValue (const Number *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static NumberinitWithValue (Number *self, const double value)
 
static int intValue (const Number *self)
 
static bool isEqual (const Object *self, const Object *other)
 
static long longValue (const Number *self)
 
static NumbernumberWithValue (double value)
 
static short shortValue (const Number *self)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Number

Definition at line 30 of file Number.c.

Function Documentation

◆ _Number()

Class * _Number ( void  )

Definition at line 198 of file Number.c.

198 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
static void initialize(Class *clazz)
Definition Number.c:176
Class * _Object(void)
Definition Object.c:136
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
A wrapper for placing numeric primitives into collections, etc.
Definition Number.h:41

◆ boolValue()

static bool boolValue ( const Number self)
static

Definition at line 80 of file Number.c.

80 {
81 return self->value ? true : false;
82}
double value
The backing value.
Definition Number.h:57

◆ charValue()

static char charValue ( const Number self)
static

Definition at line 88 of file Number.c.

88 {
89 return (char) self->value;
90}

◆ compareTo()

static Order compareTo ( const Number self,
const Number other 
)
static

Definition at line 96 of file Number.c.

96 {
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}
@ OrderSame
Definition Types.h:72
@ OrderDescending
Definition Types.h:73
@ OrderAscending
Definition Types.h:71

◆ description()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 37 of file Number.c.

37 {
38
39 Number *this = (Number *) self;
40
41 return $(alloc(String), initWithFormat, "%.2f", this->value);
42}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
UTF-8 strings.
Definition String.h:69

◆ doubleValue()

static double doubleValue ( const Number self)
static

Definition at line 113 of file Number.c.

113 {
114 return self->value;
115}

◆ floatValue()

static float floatValue ( const Number self)
static

Definition at line 121 of file Number.c.

121 {
122 return (float) self->value;
123}

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 47 of file Number.c.

47 {
48
49 Number *this = (Number *) self;
50
51 return HashForDecimal(HASH_SEED, this->value);
52}
int HashForDecimal(int hash, const double decimal)
Accumulates the hash value of decimal into hash.
Definition Hash.c:60
#define HASH_SEED
The hash seed value.
Definition Hash.h:37

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 176 of file Number.c.

176 {
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}
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 int intValue(const Number *self)
Definition Number.c:143
static int hash(const Object *self)
Definition Number.c:47
ident interface
The interface of the Class.
Definition Class.h:105
short shortValue(const Number *self)
Definition Number.c:167
int intValue(const Number *self)
Definition Number.c:143
int hash(const Object *self)
Definition Array.c:129

◆ initWithValue()

static Number * initWithValue ( Number self,
const double  value 
)
static

Definition at line 129 of file Number.c.

129 {
130
131 self = (Number *) super(Object, self, init);
132 if (self) {
133 self->value = value;
134 }
135
136 return self;
137}
static Array * init(Array *self)
Definition Array.c:420
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ intValue()

static int intValue ( const Number self)
static

Definition at line 143 of file Number.c.

143 {
144 return (int) self->value;
145}

◆ isEqual()

static bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 57 of file Number.c.

57 {
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}
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55

◆ longValue()

static long longValue ( const Number self)
static

Definition at line 151 of file Number.c.

151 {
152 return (long) self->value;
153}

◆ numberWithValue()

static Number * numberWithValue ( double  value)
static

Definition at line 159 of file Number.c.

159 {
160 return $(alloc(Number), initWithValue, value);
161}

◆ shortValue()

static short shortValue ( const Number self)
static

Definition at line 167 of file Number.c.

167 {
168 return (short) self->value;
169}