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

Go to the source code of this file.

Macros

#define _Class   _Error
 

Functions

Class_Error (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static ErrorinitWithDomain (Error *self, String *domain, int code, String *message)
 
static _Bool isEqual (const Object *self, const Object *other)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Error

Definition at line 30 of file Error.c.

Function Documentation

◆ _Error()

Class * _Error ( void  )

Definition at line 166 of file Error.c.

166 {
167 static Class *clazz;
168 static Once once;
169
170 do_once(&once, {
171 clazz = _initialize(&(const ClassDef) {
172 .name = "Error",
173 .superclass = _Object(),
174 .instanceSize = sizeof(Error),
175 .interfaceOffset = offsetof(Error, interface),
176 .interfaceSize = sizeof(ErrorInterface),
178 });
179 });
180
181 return clazz;
182}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Error.c:151
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
Encapsulation for error conditions.
Definition: Error.h:41
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 37 of file Error.c.

37 {
38
39 Error *this = (Error *) self;
40 Error *that = $(alloc(Error), initWithDomain, this->domain, this->code, this->message);
41
42 return (Object *) that;
43}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Error * initWithDomain(Error *self, String *domain, int code, String *message)
Initializes an Error with the given details.
Definition: Error.c:128
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 48 of file Error.c.

48 {
49
50 Error *this = (Error *) self;
51
52 release(this->domain);
53 release(this->message);
54
55 super(Object, self, dealloc);
56}
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()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 61 of file Error.c.

61 {
62
63 Error *this = (Error *) self;
64
66
67 $(desc, appendFormat, "%ls: %d", this->domain->chars, this->code);
68
69 if (this->message) {
70 $(desc, appendFormat, ": %ls", this->message->chars);
71 }
72
73 return (String *) desc;
74}
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Mutable UTF-8 strings.
Definition: MutableString.h:40
void appendFormat(MutableString *self, const char *fmt,...)
Appends the specified formatted string.
Definition: MutableString.c:89
Immutable UTF-8 strings.
Definition: String.h:69

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 79 of file Error.c.

79 {
80
81 Error *this = (Error *) self;
82
83 int hash = HASH_SEED;
84
85 hash = HashForInteger(hash, this->code);
86 hash = HashForObject(hash, this->domain);
87 hash = HashForObject(hash, this->message);
88
89 return hash;
90}
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

◆ initialize()

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

Definition at line 151 of file Error.c.

151 {
152
153 ((ObjectInterface *) clazz->interface)->copy = copy;
154 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
155 ((ObjectInterface *) clazz->interface)->description = description;
156 ((ObjectInterface *) clazz->interface)->hash = hash;
157 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
158
159 ((ErrorInterface *) clazz->interface)->initWithDomain = initWithDomain;
160}
ident interface
The interface of the Class.
Definition: Class.h:105
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
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

◆ initWithDomain()

static Error * initWithDomain ( Error self,
String domain,
int  code,
String message 
)
static

Definition at line 128 of file Error.c.

128 {
129
130 assert(domain);
131
132 self = (Error *) super(Object, self, init);
133 if (self) {
134
135 self->domain = retain(domain);
136 self->code = code;
137
138 if (message) {
139 self->message = retain(message);
140 }
141 }
142
143 return self;
144}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
int code
The error code.
Definition: Error.h:57
String * message
The error message.
Definition: Error.h:67
String * domain
The error domain.
Definition: Error.h:62

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 95 of file Error.c.

95 {
96
97 if (super(Object, self, isEqual, other)) {
98 return true;
99 }
100
101 if (other && (self->clazz == other->clazz)) {
102
103 const Error *this = (Error *) self;
104 const Error *that = (Error *) other;
105
106 if (this->code == that->code) {
107
108 if ($((Object * ) this->domain, isEqual, (Object * ) that->domain)) {
109
110 if (this->message) {
111 return $((Object * ) this->message, isEqual, (Object * ) that->message);
112 }
113
114 return that->message == NULL;
115 }
116 }
117 }
118
119 return false;
120}
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51