Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
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:91
static void initialize(Class *clazz)
Definition: Number.c:176
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
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ 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:159
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
Immutable 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:58
#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}
ident interface
The interface of the Class.
Definition: Class.h:105
Order compareTo(const Date *self, const Date *other)
Compares this Date to another.
Definition: Date.c:74
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
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
int intValue(const Number *self)
Definition: Number.c:151
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

◆ initWithValue()

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

Definition at line 137 of file Number.c.

137 {
138
139 self = (Number *) super(Object, self, init);
140 if (self) {
141 self->value = value;
142 }
143
144 return self;
145}
#define super(type, obj, method,...)
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
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 151 of file Number.c.

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

◆ 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:51

◆ longValue()

static long longValue ( const Number self)
static

Definition at line 129 of file Number.c.

129 {
130 return (long) self->value;
131}

◆ 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}