Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
MutableData.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "MutableData.h"

Go to the source code of this file.

Macros

#define _Class   _MutableData
 

Functions

Class_MutableData (void)
 
static void appendBytes (MutableData *self, const uint8_t *bytes, size_t length)
 
static void appendData (MutableData *self, const Data *data)
 
static Objectcopy (const Object *self)
 
static MutableDatadata (void)
 
static MutableDatadataWithCapacity (size_t capacity)
 
static MutableDatainit (MutableData *self)
 
static void initialize (Class *clazz)
 
static MutableDatainitWithCapacity (MutableData *self, size_t capacity)
 
static MutableDatainitWithData (MutableData *self, const Data *data)
 
static void setLength (MutableData *self, size_t length)
 

Macro Definition Documentation

◆ _Class

#define _Class   _MutableData

Definition at line 30 of file MutableData.c.

Function Documentation

◆ _MutableData()

Class * _MutableData ( void  )

Definition at line 182 of file MutableData.c.

182 {
183 static Class *clazz;
184 static Once once;
185
186 do_once(&once, {
187 clazz = _initialize(&(const ClassDef) {
188 .name = "MutableData",
189 .superclass = _Data(),
190 .instanceSize = sizeof(MutableData),
191 .interfaceOffset = offsetof(MutableData, interface),
192 .interfaceSize = sizeof(MutableDataInterface),
194 });
195 });
196
197 return clazz;
198}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: MutableData.c:164
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
Class * _Data(void)
The Data archetype.
Definition: Data.c:283
Mutable data buffers.
Definition: MutableData.h:40

◆ appendBytes()

static void appendBytes ( MutableData self,
const uint8_t *  bytes,
size_t  length 
)
static

Definition at line 53 of file MutableData.c.

53 {
54
55 const size_t oldLength = self->data.length;
56
57 $(self, setLength, self->data.length + length);
58
59 memcpy(self->data.bytes + oldLength, bytes, length);
60}
size_t length
The length of bytes.
Definition: Data.h:76
uint8_t * bytes
The bytes.
Definition: Data.h:66
void setLength(MutableData *self, size_t length)
Sets the length of this Data, truncating or expanding it.
Definition: MutableData.c:138
Data data
The superclass.
Definition: MutableData.h:45

◆ appendData()

static void appendData ( MutableData self,
const Data data 
)
static

Definition at line 66 of file MutableData.c.

66 {
67
68 $(self, appendBytes, data->bytes, data->length);
69}
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
void appendBytes(MutableData *self, const uint8_t *bytes, size_t length)
Appends the given bytes to this Data.
Definition: MutableData.c:53

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 37 of file MutableData.c.

37 {
38
39 Data *this = (Data *) self;
40
41 MutableData *that = $(alloc(MutableData), init);
42 $(that, appendBytes, this->bytes, this->length);
43
44 return (Object *) that;
45}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Immutable data buffers.
Definition: Data.h:50
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ data()

static MutableData * data ( void  )
static

Definition at line 75 of file MutableData.c.

75 {
76
77 return $(alloc(MutableData), init);
78}

◆ dataWithCapacity()

static MutableData * dataWithCapacity ( size_t  capacity)
static

Definition at line 84 of file MutableData.c.

84 {
85
86 return $(alloc(MutableData), initWithCapacity, capacity);
87}
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195

◆ init()

static MutableData * init ( MutableData self)
static

Definition at line 93 of file MutableData.c.

93 {
94
95 return $(self, initWithCapacity, 0);
96}

◆ initialize()

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

Definition at line 164 of file MutableData.c.

164 {
165
166 ((ObjectInterface *) clazz->interface)->copy = copy;
167
168 ((MutableDataInterface *) clazz->interface)->appendBytes = appendBytes;
169 ((MutableDataInterface *) clazz->interface)->appendData = appendData;
170 ((MutableDataInterface *) clazz->interface)->data = data;
171 ((MutableDataInterface *) clazz->interface)->dataWithCapacity = dataWithCapacity;
172 ((MutableDataInterface *) clazz->interface)->init = init;
173 ((MutableDataInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
174 ((MutableDataInterface *) clazz->interface)->initWithData = initWithData;
175 ((MutableDataInterface *) clazz->interface)->setLength = setLength;
176}
ident interface
The interface of the Class.
Definition: Class.h:105
MutableData * dataWithCapacity(size_t capacity)
Returns a new MutableData with the given capacity.
Definition: MutableData.c:84
MutableData * initWithData(MutableData *self, const Data *data)
Initializes this Data with the contents of data.
Definition: MutableData.c:124
MutableData * init(MutableData *self)
Initializes this Data with length 0.
Definition: MutableData.c:93
void appendData(MutableData *self, const Data *data)
Appends the given data to this Data.
Definition: MutableData.c:66
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ initWithCapacity()

static MutableData * initWithCapacity ( MutableData self,
size_t  capacity 
)
static

Definition at line 102 of file MutableData.c.

102 {
103
104 self = (MutableData *) super(Object, self, init);
105 if (self) {
106
107 self->capacity = capacity;
108 if (self->capacity) {
109
110 self->data.bytes = calloc(capacity, sizeof(uint8_t));
111 assert(self->data.bytes);
112 }
113
114 self->data.destroy = free;
115 }
116
117 return self;
118}
#define super(type, obj, method,...)
DataDestructor destroy
An optional destructor that, if set, is called on dealloc.
Definition: Data.h:71

◆ initWithData()

static MutableData * initWithData ( MutableData self,
const Data data 
)
static

Definition at line 124 of file MutableData.c.

124 {
125
126 self = $(self, initWithCapacity, data->length);
127 if (self) {
128 $(self, appendData, data);
129 }
130
131 return self;
132}

◆ setLength()

static void setLength ( MutableData self,
size_t  length 
)
static

Definition at line 138 of file MutableData.c.

138 {
139
140 const size_t newCapacity = (length / _pageSize + 1) * _pageSize;
141 if (newCapacity > self->capacity) {
142
143 if (self->data.bytes == NULL) {
144 self->data.bytes = calloc(newCapacity, sizeof(uint8_t));
145 assert(self->data.bytes);
146 } else {
147 self->data.bytes = realloc(self->data.bytes, newCapacity);
148 assert(self->data.bytes);
149
150 memset(self->data.bytes + self->data.length, 0, length - self->data.length);
151 }
152
153 self->capacity = newCapacity;
154 }
155
156 self->data.length = length;
157}
size_t _pageSize
Definition: Class.c:39