Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Class.h
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#pragma once
25
26#include <Objectively/Types.h>
27#include <Objectively/Once.h>
28
34typedef struct ClassDef ClassDef;
35typedef struct Class Class;
36
41struct ClassDef {
42
47 void (*destroy)(Class *clazz);
48
64 void (*initialize)(Class *clazz);
65
70
74 ptrdiff_t interfaceOffset;
75
80
84 const char *name;
85
90};
91
95struct Class {
96
101
106
111};
112
119
124
128OBJECTIVELY_EXPORT ident _cast(const Class *clazz, const ident obj);
129
133OBJECTIVELY_EXPORT Class *classForName(const char *name);
134
141
150
155
159#define OBJECTIVELY_MAGIC 0xdeadbeef
160
164#define isobject(obj) \
165 (obj && *((unsigned int *) obj) == OBJECTIVELY_MAGIC)
166
170#define instanceof(type, obj) \
171 (isobject(obj) && $((Object *) obj, isKindOfClass, _##type()))
172
176#define alloc(type) \
177 ((type *) _alloc(_##type()))
178
182#define cast(type, obj) \
183 ((type *) _cast(_##type(), (const ident) obj))
184
188#define classof(obj) \
189 ((Object *) obj)->clazz
190
194#define classnameof(obj) \
195 classof(obj)->def.name
196
200#define interfaceof(type, clazz) \
201 ((type##Interface *) (clazz)->interface)
202
206#define $(obj, method, ...) \
207 ({ \
208 typeof(obj) _obj = obj; \
209 _obj->interface->method(_obj, ## __VA_ARGS__); \
210 })
211
215#define $$(type, method, ...) \
216 ({ \
217 interfaceof(type, _##type())->method(__VA_ARGS__); \
218 })
219
223#define super(type, obj, method, ...) \
224 interfaceof(type, _Class()->def.superclass)->method(cast(type, obj), ## __VA_ARGS__)
#define obj
OBJECTIVELY_EXPORT ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
OBJECTIVELY_EXPORT size_t _pageSize
The page size, in bytes, of the target host.
Definition Class.h:154
OBJECTIVELY_EXPORT Class * _initialize(const ClassDef *clazz)
Initializes the given Class.
Definition Class.c:86
OBJECTIVELY_EXPORT ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
OBJECTIVELY_EXPORT ident _alloc(Class *clazz)
Instantiate a type through the given Class.
Definition Class.c:123
OBJECTIVELY_EXPORT Class * classForName(const char *name)
Definition Class.c:161
OBJECTIVELY_EXPORT ident _cast(const Class *clazz, const ident obj)
Perform a type-checking cast.
Definition Class.c:142
Helpers for at-most-once semantics.
Objectively base types.
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
#define OBJECTIVELY_EXPORT
Definition Types.h:36
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
size_t instanceSize
The instance size (required).
Definition Class.h:69
size_t interfaceSize
The interface size (required).
Definition Class.h:79
ptrdiff_t interfaceOffset
The interface offset (required).
Definition Class.h:74
Class * superclass
The superclass (required). e.g. _Object().
Definition Class.h:89
void(* destroy)(Class *clazz)
The Class destructor (optional). This method is run for initialized Classes when your application exi...
Definition Class.h:47
const char * name
The Class name (required).
Definition Class.h:84
void(* initialize)(Class *clazz)
The Class initializer (optional).
Definition Class.h:64
The runtime representation of a Class.
Definition Class.h:95
ClassDef def
The Class definition.
Definition Class.h:100
Class * next
Provides chaining of initialized Classes.
Definition Class.h:110
ident interface
The interface of the Class.
Definition Class.h:105