Objectively
Object oriented framework for 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
120
127
132
136OBJECTIVELY_EXPORT ident _cast(const Class *clazz, const ident obj);
137
152
173
179OBJECTIVELY_EXPORT Class *classForName(const char *name);
180
187
196
201
205#define OBJECTIVELY_MAGIC 0xdeadbeef
206
210#define isobject(obj) \
211 (obj && *((unsigned int *) obj) == OBJECTIVELY_MAGIC)
212
216#define instanceof(type, obj) \
217 (isobject(obj) && $((Object *) obj, isKindOfClass, _##type()))
218
222#define alloc(type) \
223 ((type *) _alloc(_##type()))
224
228#define cast(type, obj) \
229 ((type *) _cast(_##type(), (const ident) obj))
230
234#define classof(obj) \
235 ((Object *) obj)->clazz
236
240#define classnameof(obj) \
241 classof(obj)->def.name
242
246#define interfaceof(type, clazz) \
247 ((type##Interface *) (clazz)->interface)
248
252#define $(obj, method, ...) \
253 ({ \
254 typeof(obj) _obj = obj; \
255 _obj->interface->method(_obj, ## __VA_ARGS__); \
256 })
257
259#define $$(type, method, ...) \
260 ({ \
261 interfaceof(type, _##type())->method(__VA_ARGS__); \
262 })
263
267#define super(type, obj, method, ...) \
268 interfaceof(type, _Class()->def.superclass)->method(cast(type, obj), ## __VA_ARGS__)
OBJECTIVELY_EXPORT void addClassImage(ident handle)
Registers an image that provides Classes, e.g. a plugin.
Definition Class.c:284
#define obj
OBJECTIVELY_EXPORT ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:350
OBJECTIVELY_EXPORT size_t _pageSize
The page size, in bytes, of the target host.
Definition Class.h:200
OBJECTIVELY_EXPORT Class * _initialize(const ClassDef *clazz)
Initializes the given Class.
Definition Class.c:129
OBJECTIVELY_EXPORT void removeClassImage(ident handle)
Unregisters an image, and every Class it declared.
Definition Class.c:251
OBJECTIVELY_EXPORT ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:335
OBJECTIVELY_EXPORT ident _alloc(Class *clazz)
Instantiate a type through the given Class.
Definition Class.c:170
OBJECTIVELY_EXPORT Class * classForName(const char *name)
Definition Class.c:292
OBJECTIVELY_EXPORT ident _cast(const Class *clazz, const ident obj)
Perform a type-checking cast.
Definition Class.c:189
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
ident image
The base address of the image that declared this Class.
Definition Class.h:118
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