Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Object.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 "Object.h"
29#include "String.h"
30
31#define _Class _Object
32
33#pragma mark - Object
34
39static Object *copy(const Object *self) {
40
41 ident obj = calloc(1, self->clazz->def.instanceSize);
42 assert(obj);
43
44 Object *object = memcpy(obj, self, self->clazz->def.instanceSize);
45 object->referenceCount = 1;
46
47 return object;
48}
49
54static void dealloc(Object *self) {
55
56 free(self);
57}
58
63static String *description(const Object *self) {
64
65 return $(alloc(String), initWithFormat, "%s@%p", self->clazz->def.name, self);
66}
67
72static int hash(const Object *self) {
73
74 uintptr_t addr = (uintptr_t) self;
75
76 return (int) ((13 * addr) ^ (addr >> 15));
77}
78
83static Object *init(Object *self) {
84
85 return self;
86}
87
92static _Bool isEqual(const Object *self, const Object *other) {
93
94 return self == other;
95}
96
101static _Bool isKindOfClass(const Object *self, const Class *clazz) {
102
103 assert(clazz);
104
105 const Class *c = self->clazz;
106 while (c) {
107 if (memcmp(c, clazz, sizeof(*c)) == 0) {
108 return true;
109 }
110 c = c->def.superclass;
111 }
112
113 return false;
114}
115
116#pragma mark - Class lifecycle
117
121static void initialize(Class *clazz) {
122
123 ((ObjectInterface *) clazz->interface)->copy = copy;
124 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
125 ((ObjectInterface *) clazz->interface)->description = description;
126 ((ObjectInterface *) clazz->interface)->hash = hash;
127 ((ObjectInterface *) clazz->interface)->init = init;
128 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
129 ((ObjectInterface *) clazz->interface)->isKindOfClass = isKindOfClass;
130}
131
137 static Class *clazz;
138 static Once once;
139
140 do_once(&once, {
141 clazz = _initialize(&(const ClassDef) {
142 .name = "Object",
143 .instanceSize = sizeof(Object),
144 .interfaceOffset = offsetof(Object, interface),
145 .interfaceSize = sizeof(ObjectInterface),
147 });
148 });
149
150 return clazz;
151}
152
153#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static void initialize(Class *clazz)
Definition: Object.c:121
Object is the root Class of The Objectively Class hierarchy.
Immutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
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
size_t instanceSize
The instance size (required).
Definition: Class.h:69
Class * superclass
The superclass (required). e.g. _Object().
Definition: Class.h:89
const char * name
The Class name (required).
Definition: Class.h:84
The runtime representation of a Class.
Definition: Class.h:95
ClassDef def
The Class definition.
Definition: Class.h:100
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
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:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101
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
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Immutable UTF-8 strings.
Definition: String.h:69