Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
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 IndexPath IndexSet JSONPath JSONSerialization Lock Log Null Number NumberFormatter Operation OperationQueue Regexp Resource Set String StringReader Thread URL URLRequest URLResponse URLSession URLSessionConfiguration URLSessionTask Value Vector

Properties

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

Methods

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

Protected Attributes

ObjectInterface * interface
 The interface. More...
 

Property Details

◆ clazz

Class* Object::clazz

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

Definition at line 51 of file Object.h.

◆ interface

ObjectInterface* Object::interface
protected

The interface.

Definition at line 57 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}
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
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
ObjectInterface * interface
The interface.
Definition: Object.h:57

◆ copy()

Object * copy ( const Object self)

Creates a shallow copy of this Object.

Parameters
selfThe Object.
Returns
The copy.

Definition at line 40 of file Array.c.

40 {
41
42 const Array *this = (Array *) self;
43
44 return (Object *) $(alloc(Array), initWithArray, this);
45}
static Array * initWithArray(Array *self, const Array *array)
Definition: Array.c:288
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Immutable arrays.
Definition: Array.h:56

◆ dealloc()

void dealloc ( Object self)

Frees all resources held by this Object.

Parameters
selfThe Object.

Definition at line 50 of file Array.c.

50 {
51
52 Array *this = (Array *) self;
53
54 for (size_t i = 0; i < this->count; i++) {
55 release(this->elements[i]);
56 }
57
58 free(this->elements);
59
60 super(Object, self, dealloc);
61}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

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

Definition at line 66 of file Array.c.

66 {
67
68 String *components = $((Array *) self, componentsJoinedByCharacters, ", ");
69
70 String *desc = $(alloc(String), initWithFormat, "[%s]", components->chars ?: "");
71
72 release(components);
73
74 return desc;
75}
static String * componentsJoinedByCharacters(const Array *self, const char *chars)
Definition: Array.c:176
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Definition: DateFormatter.c:76
Immutable 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 80 of file Array.c.

80 {
81
82 Array *this = (Array *) self;
83
84 int hash = HashForInteger(HASH_SEED, this->count);
85
86 for (size_t i = 0; i < this->count; i++) {
87 hash = HashForObject(hash, this->elements[i]);
88 }
89
90 return hash;
91}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition: Hash.c:66
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
int hash(const Object *self)
Definition: Array.c:80

◆ 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 96 of file Array.c.

96 {
97
98 if (super(Object, self, isEqual, other)) {
99 return true;
100 }
101
102 if (other && $(other, isKindOfClass, _Array())) {
103
104 const Array *this = (Array *) self;
105 const Array *that = (Array *) other;
106
107 if (this->count == that->count) {
108
109 for (size_t i = 0; i < this->count; i++) {
110
111 const Object *thisObject = this->elements[i];
112 const Object *thatObject = that->elements[i];
113
114 if ($(thisObject, isEqual, thatObject) == false) {
115 return false;
116 }
117 }
118
119 return true;
120 }
121 }
122
123 return false;
124}
Class * _Array(void)
Definition: Array.c:470
size_t count
The count of elements.
Definition: Array.h:72
_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

◆ 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: