Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Error.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
26#include "Error.h"
27#include "Hash.h"
28#include "String.h"
29
30#define _Class _Error
31
32#pragma mark - Object
33
37static Object *copy(const Object *self) {
38
39 Error *this = (Error *) self;
40 Error *that = $(alloc(Error), initWithDomain, this->domain, this->code, this->message);
41
42 return (Object *) that;
43}
44
48static void dealloc(Object *self) {
49
50 Error *this = (Error *) self;
51
52 release(this->domain);
53 release(this->message);
54
55 super(Object, self, dealloc);
56}
57
61static String *description(const Object *self) {
62
63 Error *this = (Error *) self;
64
65 String *desc = $(alloc(String), init);
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}
75
79static int hash(const Object *self) {
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}
91
95static bool isEqual(const Object *self, const Object *other) {
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}
121
122#pragma mark - Error
123
128static Error *initWithDomain(Error *self, String *domain, int code, String *message) {
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}
145
146#pragma mark - Class lifecycle
147
151static void initialize(Class *clazz) {
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}
161
166Class *_Error(void) {
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}
183
184#undef _Class
static Array * init(Array *self)
Definition Array.c:420
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
Class * _Error(void)
Definition Error.c:166
static Error * initWithDomain(Error *self, String *domain, int code, String *message)
Definition Error.c:128
static bool isEqual(const Object *self, const Object *other)
Definition Error.c:95
static String * description(const Object *self)
Definition Error.c:61
static void dealloc(Object *self)
Definition Error.c:48
static Object * copy(const Object *self)
Definition Error.c:37
static void initialize(Class *clazz)
Definition Error.c:151
static int hash(const Object *self)
Definition Error.c:79
Encapsulation for error conditions.
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
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
Class * _Object(void)
Definition Object.c:136
static void appendFormat(String *self, const char *fmt,...)
Definition String.c:541
UTF-8 strings.
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
ident interface
The interface of the Class.
Definition Class.h:105
Encapsulation for error conditions.
Definition Error.h:41
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
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
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
UTF-8 strings.
Definition String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85