Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
129
133OBJECTIVELY_EXPORT Class *classForName(const char *name);
134
141
150
155
159#define alloc(type) \
160 ((type *) _alloc(_##type()))
161
165#define cast(type, obj) \
166 ((type *) _cast(_##type(), (const ident) obj))
167
171#define classof(obj) \
172 ((Object *) obj)->clazz
173
177#define classnameof(obj) \
178 classof(obj)->def.name
179
183#define interfaceof(type, clazz) \
184 ((type##Interface *) (clazz)->interface)
185
189#define $(obj, method, ...) \
190 ({ \
191 typeof(obj) _obj = obj; \
192 _obj->interface->method(_obj, ## __VA_ARGS__); \
193 })
194
198#define $$(type, method, ...) \
199 ({ \
200 interfaceof(type, _##type())->method(__VA_ARGS__); \
201 })
202
206#define super(type, obj, method, ...) \
207 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:211
OBJECTIVELY_EXPORT ident _cast(Class *clazz, const ident obj)
Perform a type-checking cast.
Definition: Class.c:146
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:91
OBJECTIVELY_EXPORT ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
OBJECTIVELY_EXPORT ident _alloc(Class *clazz)
Instantiate a type through the given Class.
Definition: Class.c:128
OBJECTIVELY_EXPORT Class * classForName(const char *name)
Definition: Class.c:165
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