Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Object Struct Reference

#include <Object.h>

Overview

Object is the root Class of The Objectively Class hierarchy.

Every Class descends from Object, and every instance can be cast to Object.

Definition at line 46 of file Object.h.

Inheritance diagram for Object:
Array Boole Data Date DateFormatter Dictionary Error HashTable IndexPath IndexSet JSONContext JSONPath List Lock Log Null Number NumberFormatter Operation OperationQueue Pointer PointerArray RESTClient Regexp Resource Set String StringReader Thread URL URLCache URLCachedResponse URLRequest URLResponse URLSession URLSessionConfiguration URLSessionTask Vector

Properties

Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

ObjectInterface * interface
 The interface.
 

Property Details

◆ clazz

Class* Object::clazz

Every instance of Object begins with a pointer to its Class.

Definition at line 55 of file Object.h.

◆ interface

ObjectInterface* Object::interface
protected

The interface.

Definition at line 61 of file Object.h.

◆ magic

unsigned int Object::magic

A header to allow introspection of Object types.

Definition at line 50 of file Object.h.

Method Details

◆ _Object()

Class * _Object ( void  )

The Object archetype.

Returns
The Object Class.

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}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
ObjectInterface * interface
The interface.
Definition Object.h:61

◆ copy()

Object * copy ( const Object self)

Creates a shallow copy of this Object.

Parameters
selfThe Object.
Returns
The copy.

Definition at line 84 of file Array.c.

84 {
85
86 const Array *this = (Array *) self;
87
88 Array *copy = $(alloc(Array), initWithCapacity, this->count);
89 assert(copy);
90
91 $(copy, addObjectsFromArray, this);
92
93 return (Object *) copy;
94}
static Array * initWithCapacity(Array *self, size_t capacity)
Definition Array.c:453
static void addObjectsFromArray(Array *self, const Array *array)
Definition Array.c:221
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
Arrays.
Definition Array.h:56
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84

◆ dealloc()

void dealloc ( Object self)

Frees all resources held by this Object.

Parameters
selfThe Object.

Definition at line 99 of file Array.c.

99 {
100
101 Array *this = (Array *) self;
102
103 for (size_t i = 0; i < this->count; i++) {
104 release(this->elements[i]);
105 }
106
107 free(this->elements);
108
109 super(Object, self, dealloc);
110}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99

◆ description()

String * description ( const Object self)
Parameters
selfThe Object.
Returns
A brief description of this Object.

Definition at line 115 of file Array.c.

115 {
116
117 String *components = $((Array *) self, componentsJoinedByCharacters, ", ");
118
119 String *desc = $(alloc(String), initWithFormat, "[%s]", components->chars ?: "");
120
121 release(components);
122
123 return desc;
124}
static String * componentsJoinedByCharacters(const Array *self, const char *chars)
Definition Array.c:295
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
UTF-8 strings.
Definition String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85

◆ hash()

int hash ( const Object self)
Parameters
selfThe Object.
Returns
An integer hash for use in hash tables, etc.

Definition at line 129 of file Array.c.

129 {
130
131 Array *this = (Array *) self;
132
133 int hash = HashForInteger(HASH_SEED, this->count);
134
135 for (size_t i = 0; i < this->count; i++) {
136 hash = HashForObject(hash, this->elements[i]);
137 }
138
139 return hash;
140}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition Hash.c:72
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
int hash(const Object *self)
Definition Array.c:129

◆ init()

Object * init ( Object self)

Initializes this Object.

Parameters
selfThe Object.
Returns
The initialized Object, or the unmodified pointer on error.

Definition at line 83 of file Object.c.

83 {
84
85 return self;
86}

◆ isEqual()

bool isEqual ( const Object self,
const Object other 
)

Tests equality of the other Object.

Parameters
selfThe Object.
otherThe Object to test for equality.
Returns
True if other is deemed equal, false otherwise.

Definition at line 145 of file Array.c.

145 {
146
147 if (super(Object, self, isEqual, other)) {
148 return true;
149 }
150
151 if (other && $(other, isKindOfClass, _Array())) {
152
153 const Array *this = (Array *) self;
154 const Array *that = (Array *) other;
155
156 if (this->count == that->count) {
157
158 for (size_t i = 0; i < this->count; i++) {
159
160 const Object *thisObject = this->elements[i];
161 const Object *thatObject = that->elements[i];
162
163 if ($(thisObject, isEqual, thatObject) == false) {
164 return false;
165 }
166 }
167
168 return true;
169 }
170 }
171
172 return false;
173}
Class * _Array(void)
Definition Array.c:760
size_t count
The count of elements.
Definition Array.h:72
bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition Array.c:145
bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition Object.c:101

◆ isKindOfClass()

bool isKindOfClass ( const Object self,
const Class clazz 
)

Tests for Class hierarchy membership.

Parameters
selfThe Object.
clazzThe Class to test for membership.
Returns
True if this instance belongs to Class' hierarchy, false otherwise.

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
ClassDef def
The Class definition.
Definition Class.h:100

The documentation for this struct was generated from the following files: