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

Go to the source code of this file.

Macros

#define _Class   _Object
 

Functions

Class_Object (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static int hash (const Object *self)
 
static Objectinit (Object *self)
 
static void initialize (Class *clazz)
 
static _Bool isEqual (const Object *self, const Object *other)
 
static _Bool isKindOfClass (const Object *self, const Class *clazz)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Object

Definition at line 31 of file Object.c.

Function Documentation

◆ _Object()

Class * _Object ( void  )

Definition at line 136 of file Object.c.

136 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Object.c:121
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
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ copy()

static Object * copy ( const Object self)
static

Definition at line 39 of file Object.c.

39 {
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}
#define obj
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
size_t instanceSize
The instance size (required).
Definition: Class.h:69
ClassDef def
The Class definition.
Definition: Class.h:100
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ dealloc()

static void dealloc ( Object self)
static

Definition at line 54 of file Object.c.

54 {
55
56 free(self);
57}

◆ description()

static String * description ( const Object self)
static

Definition at line 63 of file Object.c.

63 {
64
65 return $(alloc(String), initWithFormat, "%s@%p", self->clazz->def.name, self);
66}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
const char * name
The Class name (required).
Definition: Class.h:84
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

◆ hash()

static int hash ( const Object self)
static

Definition at line 72 of file Object.c.

72 {
73
74 uintptr_t addr = (uintptr_t) self;
75
76 return (int) ((13 * addr) ^ (addr >> 15));
77}

◆ init()

static Object * init ( Object self)
static

Definition at line 83 of file Object.c.

83 {
84
85 return self;
86}

◆ initialize()

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

Definition at line 121 of file Object.c.

121 {
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}
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
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

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static

Definition at line 92 of file Object.c.

92 {
93
94 return self == other;
95}

◆ isKindOfClass()

static _Bool isKindOfClass ( const Object self,
const Class clazz 
)
static

Definition at line 101 of file Object.c.

101 {
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}
Class * superclass
The superclass (required). e.g. _Object().
Definition: Class.h:89