Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Pointer.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#include <stdlib.h>
26#include <string.h>
27
28#include "Hash.h"
29#include "Pointer.h"
30
31#define _Class _Pointer
32
33#pragma mark - Object
34
38static void dealloc(Object *self) {
39
40 Pointer *this = (Pointer *) self;
41
42 if (this->destroy) {
43 this->destroy(this->pointer);
44 }
45
46 super(Object, self, dealloc);
47}
48
52static int hash(const Object *self) {
53
54 const Pointer *this = (Pointer *) self;
55
56 uintptr_t addr = (uintptr_t) this->pointer;
57
58 return (int) ((13 * addr) ^ (addr >> 15));
59}
60
64static bool isEqual(const Object *self, const Object *other) {
65
66 if (super(Object, self, isEqual, other)) {
67 return true;
68 }
69
70 if (other && $(other, isKindOfClass, _Pointer())) {
71
72 const Pointer *this = (Pointer *) self;
73 const Pointer *that = (Pointer *) other;
74
75 return this->pointer == that->pointer;
76 }
77
78 return false;
79}
80
81#pragma mark - Pointer
82
87static Pointer *initWithBytes(Pointer *self, const uint8_t *bytes, size_t length) {
88
89 self = (Pointer *) super(Object, self, init);
90 if (self) {
91 if (bytes) {
92 self->pointer = calloc(1, length);
93 assert(self->pointer);
94
95 memcpy(self->pointer, bytes, length);
96 self->destroy = free;
97 }
98 }
99
100 return self;
101}
102
108
109 self = (Pointer *) super(Object, self, init);
110 if (self) {
111 self->pointer = pointer;
112 self->destroy = destroy;
113 }
114
115 return self;
116}
117
118#pragma mark - Class lifecycle
119
123static void initialize(Class *clazz) {
124
125 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
126 ((ObjectInterface *) clazz->interface)->hash = hash;
127 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
128
129 ((PointerInterface *) clazz->interface)->initWithBytes = initWithBytes;
130 ((PointerInterface *) clazz->interface)->initWithPointer = initWithPointer;
131}
132
138 static Class *clazz;
139 static Once once;
140
141 do_once(&once, {
142 clazz = _initialize(&(const ClassDef) {
143 .name = "Pointer",
144 .superclass = _Object(),
145 .instanceSize = sizeof(Pointer),
146 .interfaceOffset = offsetof(Pointer, interface),
147 .interfaceSize = sizeof(PointerInterface),
149 });
150 });
151
152 return clazz;
153}
154
155#undef _Class
156
158 return $(alloc(Pointer), initWithPointer, pointer, destroy);
159}
static Array * init(Array *self)
Definition Array.c:420
static void destroy(Class *clazz)
Definition Boole.c:102
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
Utilities for calculating hash values.
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Object(void)
Definition Object.c:136
static Pointer * initWithBytes(Pointer *self, const uint8_t *bytes, size_t length)
Definition Pointer.c:87
static bool isEqual(const Object *self, const Object *other)
Definition Pointer.c:64
static void dealloc(Object *self)
Definition Pointer.c:38
Pointer * ptr(ident pointer, Consumer destroy)
Definition Pointer.c:157
static Pointer * initWithPointer(Pointer *self, ident pointer, Consumer destroy)
Definition Pointer.c:107
static void initialize(Class *clazz)
Definition Pointer.c:123
Class * _Pointer(void)
Definition Pointer.c:137
static int hash(const Object *self)
Definition Pointer.c:52
Pointers provide Object encapsulation for raw C pointers.
UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
void(* Consumer)(ident data)
The Consumer function type.
Definition Types.h:88
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
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
Pointers provide Object encapsulation for raw C pointers.
Definition Pointer.h:40
Pointer * initWithPointer(Pointer *self, ident pointer, Consumer destroy)
Initializes this Pointer.
Definition Pointer.c:107
ident pointer
The backing pointer.
Definition Pointer.h:61
Consumer destroy
An optional destructor that, if set, is called on dealloc.
Definition Pointer.h:56