Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Value.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "Value.h"

Go to the source code of this file.

Macros

#define _Class   _Value
 

Functions

Class_Value (void)
 
static void dealloc (Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static ValueinitWithBytes (Value *self, const uint8_t *bytes, size_t length)
 
static ValueinitWithValue (Value *self, ident value)
 
static _Bool isEqual (const Object *self, const Object *other)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Value

Definition at line 31 of file Value.c.

Function Documentation

◆ _Value()

Class * _Value ( void  )

Definition at line 136 of file Value.c.

136 {
137 static Class *clazz;
138 static Once once;
139
140 do_once(&once, {
141 clazz = _initialize(&(const ClassDef) {
142 .name = "Value",
143 .superclass = _Object(),
144 .instanceSize = sizeof(Value),
145 .interfaceOffset = offsetof(Value, interface),
146 .interfaceSize = sizeof(ValueInterface),
148 });
149 });
150
151 return clazz;
152}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Value.c:122
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
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Values provide Object encapsulation for C types.
Definition: Value.h:45

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 38 of file Value.c.

38 {
39
40 Value *this = (Value *) self;
41
42 if (this->destructor) {
43 this->destructor(this->value);
44 }
45
46 super(Object, self, dealloc);
47}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ hash()

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

Definition at line 52 of file Value.c.

52 {
53
54 const Value *this = (Value *) self;
55
56 uintptr_t addr = (uintptr_t) this->value;
57
58 return (int) ((13 * addr) ^ (addr >> 15));
59}

◆ initialize()

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

Definition at line 122 of file Value.c.

122 {
123
124 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
125 ((ObjectInterface *) clazz->interface)->hash = hash;
126 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
127
128 ((ValueInterface *) clazz->interface)->initWithBytes = initWithBytes;
129 ((ValueInterface *) clazz->interface)->initWithValue = initWithValue;
130}
ident interface
The interface of the Class.
Definition: Class.h:105
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Definition: Data.c:145
Number * initWithValue(Number *self, double value)
Initializes this Number with the specified value.
Definition: Number.c:137
_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

◆ initWithBytes()

static Value * initWithBytes ( Value self,
const uint8_t *  bytes,
size_t  length 
)
static

Definition at line 87 of file Value.c.

87 {
88
89 self = (Value *) super(Object, self, init);
90 if (self) {
91 if (bytes) {
92 self->value = calloc(1, length);
93 assert(self->value);
94
95 memcpy(self->value, bytes, length);
96 self->destructor = free;
97 }
98 }
99
100 return self;
101}
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
ValueDestructor destructor
An optional destructor that, if set, is called on dealloc.
Definition: Value.h:66
ident value
The backing value.
Definition: Value.h:61

◆ initWithValue()

static Value * initWithValue ( Value self,
ident  value 
)
static

Definition at line 107 of file Value.c.

107 {
108
109 self = (Value *) super(Object, self, init);
110 if (self) {
111 self->value = value;
112 }
113
114 return self;
115}

◆ isEqual()

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

Definition at line 64 of file Value.c.

64 {
65
66 if (super(Object, self, isEqual, other)) {
67 return true;
68 }
69
70 if (other && $(other, isKindOfClass, _Value())) {
71
72 const Value *this = (Value *) self;
73 const Value *that = (Value *) other;
74
75 return this->value == that->value;
76 }
77
78 return false;
79}
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101
Class * _Value(void)
The Value archetype.
Definition: Value.c:136