Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Value.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#include <stdlib.h>
26#include <string.h>
27
28#include "Hash.h"
29#include "Value.h"
30
31#define _Class _Value
32
33#pragma mark - Object
34
38static void dealloc(Object *self) {
39
40 Value *this = (Value *) self;
41
42 if (this->destructor) {
43 this->destructor(this->value);
44 }
45
46 super(Object, self, dealloc);
47}
48
52static int hash(const Object *self) {
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}
60
64static _Bool isEqual(const Object *self, const Object *other) {
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}
80
81#pragma mark - Value
82
87static Value *initWithBytes(Value *self, const uint8_t *bytes, size_t length) {
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}
102
107static Value *initWithValue(Value *self, ident value) {
108
109 self = (Value *) super(Object, self, init);
110 if (self) {
111 self->value = value;
112 }
113
114 return self;
115}
116
117#pragma mark - Class lifecycle
118
122static void initialize(Class *clazz) {
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}
131
136Class *_Value(void) {
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}
153
154#undef _Class
155
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define super(type, obj, method,...)
Utilities for calculating hash values.
Immutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
static void initialize(Class *clazz)
Definition: Value.c:122
Values provide Object encapsulation for C types.
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
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
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101
_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
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Values provide Object encapsulation for C types.
Definition: Value.h:45
ValueDestructor destructor
An optional destructor that, if set, is called on dealloc.
Definition: Value.h:66
Class * _Value(void)
The Value archetype.
Definition: Value.c:136
ident value
The backing value.
Definition: Value.h:61