Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Pointer.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "Pointer.h"

Go to the source code of this file.

Macros

#define _Class   _Pointer
 

Functions

Class_Pointer (void)
 
static void dealloc (Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static PointerinitWithBytes (Pointer *self, const uint8_t *bytes, size_t length)
 
static PointerinitWithPointer (Pointer *self, ident pointer, Consumer destroy)
 
static bool isEqual (const Object *self, const Object *other)
 
Pointerptr (ident pointer, Consumer destroy)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Pointer

Definition at line 31 of file Pointer.c.

Function Documentation

◆ _Pointer()

Class * _Pointer ( void  )

Definition at line 137 of file Pointer.c.

137 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void initialize(Class *clazz)
Definition Pointer.c:123
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
Pointers provide Object encapsulation for raw C pointers.
Definition Pointer.h:40

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 38 of file Pointer.c.

38 {
39
40 Pointer *this = (Pointer *) self;
41
42 if (this->destroy) {
43 this->destroy(this->pointer);
44 }
45
46 super(Object, self, dealloc);
47}
static void destroy(Class *clazz)
Definition Boole.c:102
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition Pointer.c:38
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 52 of file Pointer.c.

52 {
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}

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 123 of file Pointer.c.

123 {
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}
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 Pointer * initWithPointer(Pointer *self, ident pointer, Consumer destroy)
Definition Pointer.c:107
static int hash(const Object *self)
Definition Pointer.c:52
ident interface
The interface of the Class.
Definition Class.h:105
Pointer * initWithPointer(Pointer *self, ident pointer, Consumer destroy)
Initializes this Pointer.
Definition Pointer.c:107

◆ initWithBytes()

static Pointer * initWithBytes ( Pointer self,
const uint8_t *  bytes,
size_t  length 
)
static

Definition at line 87 of file Pointer.c.

87 {
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}
static Array * init(Array *self)
Definition Array.c:420
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

◆ initWithPointer()

static Pointer * initWithPointer ( Pointer self,
ident  pointer,
Consumer  destroy 
)
static

Definition at line 107 of file Pointer.c.

107 {
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}

◆ isEqual()

static bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 64 of file Pointer.c.

64 {
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}
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Pointer(void)
Definition Pointer.c:137

◆ ptr()

Pointer * ptr ( ident  pointer,
Consumer  destroy 
)
related

Definition at line 157 of file Pointer.c.

157 {
158 return $(alloc(Pointer), initWithPointer, pointer, destroy);
159}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176