Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Data Struct Reference

#include <Data.h>

Overview

Data buffers.

Definition at line 50 of file Data.h.

Inheritance diagram for Data:
Object

Properties

uint8_t * bytes
 The bytes.
 
DataDestructor destroy
 An optional destructor that, if set, is called on dealloc.
 
size_t length
 The length of bytes.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_Data (void)
 The Data archetype.
 
void appendBytes (Data *self, const uint8_t *bytes, size_t length)
 Appends the given bytes to this Data.
 
void appendData (Data *self, const Data *data)
 Appends the given data to this Data.
 
Datadata (void)
 Returns a new Data.
 
DatadataWithBytes (const uint8_t *bytes, size_t length)
 Returns a new Data by copying length of bytes.
 
DatadataWithCapacity (size_t capacity)
 Returns a new Data with the given capacity.
 
DatadataWithConstMemory (const ident mem, size_t length)
 Returns a new Data, backed by the given const memory.
 
DatadataWithContentsOfFile (const char *path)
 Returns a new Data with the contents of the file at path.
 
DatadataWithMemory (ident mem, size_t length)
 Returns a new Data, taking ownership of the specified memory.
 
Datainit (Data *self)
 Initializes this Data with length 0.
 
DatainitWithBytes (Data *self, const uint8_t *bytes, size_t length)
 Initializes this Data by copying length of bytes.
 
DatainitWithCapacity (Data *self, size_t capacity)
 Initializes this Data with the given capacity.
 
DatainitWithConstMemory (Data *self, const ident mem, size_t length)
 Initializes this Data with the given const memory.
 
DatainitWithContentsOfFile (Data *self, const char *path)
 Initializes this Data with the contents of the file at path.
 
DatainitWithData (Data *self, const Data *data)
 Initializes this Data with the contents of data.
 
DatainitWithMemory (Data *self, ident mem, size_t length)
 Initializes this Data, taking ownership of the specified memory.
 
void setLength (Data *self, size_t length)
 Sets the length of this Data, truncating or expanding it.
 
bool writeToFile (const Data *self, const char *path)
 Writes this Data to path.
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

DataInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ bytes

uint8_t* Data::bytes

The bytes.

Definition at line 66 of file Data.h.

◆ destroy

DataDestructor Data::destroy

An optional destructor that, if set, is called on dealloc.

Definition at line 71 of file Data.h.

◆ interface

DataInterface* Data::interface
protected

The interface.

Definition at line 61 of file Data.h.

◆ length

size_t Data::length

The length of bytes.

Definition at line 76 of file Data.h.

◆ object

Object Data::object

The superclass.

Definition at line 55 of file Data.h.

Method Details

◆ _Data()

Class * _Data ( void  )

The Data archetype.

Returns
The Data Class.

Definition at line 419 of file Data.c.

419 {
420 static Class *clazz;
421 static Once once;
422
423 do_once(&once, {
424 clazz = _initialize(&(const ClassDef) {
425 .name = "Data",
426 .superclass = _Object(),
427 .instanceSize = sizeof(Data),
428 .interfaceOffset = offsetof(Data, interface),
429 .interfaceSize = sizeof(DataInterface),
431 });
432 });
433
434 return clazz;
435}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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
Data buffers.
Definition Data.h:50
DataInterface * interface
The interface.
Definition Data.h:61
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136

◆ appendBytes()

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

Appends the given bytes to this Data.

Parameters
selfThe Data.
bytesThe bytes to append.
lengthThe length of bytes to append.
Remarks
Data are grown in blocks as bytes are appended. This provides a significant performance gain when frequently appending small chunks of bytes.

Definition at line 262 of file Data.c.

262 {
263
264 const size_t oldLength = self->length;
265
266 $(self, setLength, self->length + length);
267
268 if (length) {
269 memcpy(self->bytes + oldLength, bytes, length);
270 }
271}
size_t length
The length of bytes.
Definition Data.h:76
uint8_t * bytes
The bytes.
Definition Data.h:66
void setLength(Data *self, size_t length)
Sets the length of this Data, truncating or expanding it.
Definition Data.c:349

◆ appendData()

void appendData ( Data self,
const Data data 
)

Appends the given data to this Data.

Parameters
selfThe Data.
dataThe Data to append.

Definition at line 277 of file Data.c.

277 {
278
279 $(self, appendBytes, data->bytes, data->length);
280}
void appendBytes(Data *self, const uint8_t *bytes, size_t length)
Appends the given bytes to this Data.
Definition Data.c:262
Data * data(void)
Returns a new Data.
Definition Data.c:286

◆ data()

Data * data ( void  )

Returns a new Data.

Returns
The new Data, or NULL on error.

Definition at line 286 of file Data.c.

286 {
287
288 return $(alloc(Data), init);
289}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
Data * init(Data *self)
Initializes this Data with length 0.
Definition Data.c:304

◆ dataWithBytes()

Data * dataWithBytes ( const uint8_t *  bytes,
size_t  length 
)

Returns a new Data by copying length of bytes.

Parameters
bytesThe bytes.
lengthThe length of bytes to copy.
Returns
The new Data, or NULL on error.

Definition at line 115 of file Data.c.

115 {
116
117 return $(alloc(Data), initWithBytes, bytes, length);
118}
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Definition Data.c:151

◆ dataWithCapacity()

Data * dataWithCapacity ( size_t  capacity)

Returns a new Data with the given capacity.

Parameters
capacityThe desired capacity in bytes.
Returns
The new Data, or NULL on error.

Definition at line 295 of file Data.c.

295 {
296
297 return $(alloc(Data), initWithCapacity, capacity);
298}
Data * initWithCapacity(Data *self, size_t capacity)
Initializes this Data with the given capacity.
Definition Data.c:313

◆ dataWithConstMemory()

Data * dataWithConstMemory ( const ident  mem,
size_t  length 
)

Returns a new Data, backed by the given const memory.

Parameters
memThe constant memory to back this Data.
lengthThe length of mem in bytes.
Returns
The new Data, or NULL on error.

Definition at line 124 of file Data.c.

124 {
125
126 return $(alloc(Data), initWithConstMemory, mem, length);
127}
Data * initWithConstMemory(Data *self, const ident mem, size_t length)
Initializes this Data with the given const memory.
Definition Data.c:168

◆ dataWithContentsOfFile()

Data * dataWithContentsOfFile ( const char *  path)

Returns a new Data with the contents of the file at path.

Parameters
pathThe path of the file to read into memory.
Returns
The new Data, or NULL on error.

Definition at line 133 of file Data.c.

133 {
134
135 return $(alloc(Data), initWithContentsOfFile, path);
136}
Data * initWithContentsOfFile(Data *self, const char *path)
Initializes this Data with the contents of the file at path.
Definition Data.c:184

◆ dataWithMemory()

Data * dataWithMemory ( ident  mem,
size_t  length 
)

Returns a new Data, taking ownership of the specified memory.

Parameters
memThe dynamically allocated memory to back this Data.
lengthThe length of mem in bytes.
Returns
The new Data, or NULL on error.

Definition at line 142 of file Data.c.

142 {
143
144 return $(alloc(Data), initWithMemory, mem, length);
145}
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Definition Data.c:219

◆ init()

Data * init ( Data self)

Initializes this Data with length 0.

Parameters
selfThe Data.
Returns
The initialized Data, or NULL on error.

Definition at line 304 of file Data.c.

304 {
305
306 return $(self, initWithCapacity, 0);
307}

◆ initWithBytes()

Data * initWithBytes ( Data self,
const uint8_t *  bytes,
size_t  length 
)

Initializes this Data by copying length of bytes.

Parameters
selfThe Data.
bytesThe bytes.
lengthThe length of bytes to copy.
Returns
The initialized Data, or NULL on error.

Definition at line 151 of file Data.c.

151 {
152
153 ident mem = NULL;
154 if (length) {
155 mem = malloc(length);
156 assert(mem);
157
158 memcpy(mem, bytes, length);
159 }
160
161 return $(self, initWithMemory, mem, length);
162}
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ initWithCapacity()

Data * initWithCapacity ( Data self,
size_t  capacity 
)

Initializes this Data with the given capacity.

Parameters
selfThe Data.
capacityThe capacity in bytes.
Returns
The initialized Data, or NULL on error.

Definition at line 313 of file Data.c.

313 {
314
315 self = (Data *) super(Object, self, init);
316 if (self) {
317
318 self->capacity = capacity;
319 if (self->capacity) {
320
321 self->bytes = calloc(capacity, sizeof(uint8_t));
322 assert(self->bytes);
323 }
324
325 self->destroy = free;
326 }
327
328 return self;
329}
#define super(type, obj, method,...)
DataDestructor destroy
An optional destructor that, if set, is called on dealloc.
Definition Data.h:71
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ initWithConstMemory()

Data * initWithConstMemory ( Data self,
const ident  mem,
size_t  length 
)

Initializes this Data with the given const memory.

Parameters
selfThe Data.
memThe const memory to back this Data.
lengthThe length of mem in bytes.
Returns
The initialized Data, or NULL on error.

Definition at line 168 of file Data.c.

168 {
169
170 self = (Data *) super(Object, self, init);
171 if (self) {
172 self->bytes = mem;
173 self->length = length;
174 self->capacity = length;
175 }
176
177 return self;
178}

◆ initWithContentsOfFile()

Data * initWithContentsOfFile ( Data self,
const char *  path 
)

Initializes this Data with the contents of the file at path.

Parameters
selfThe Data.
pathThe path of the file to read into memory.
Returns
The initialized Data, or NULL on error.

Definition at line 184 of file Data.c.

184 {
185
186 assert(path);
187
188 FILE *file = fopen(path, "rb");
189 if (file) {
190 ident mem = NULL;
191
192 int err = fseek(file, 0, SEEK_END);
193 assert(err == 0);
194
195 const size_t length = ftell(file);
196 if (length) {
197
198 mem = malloc(length);
199 assert(mem);
200
201 err = fseek(file, 0, SEEK_SET);
202 assert(err == 0);
203
204 const size_t read = fread(mem, length, 1, file);
205 assert(read == 1);
206 }
207
208 fclose(file);
209 return $(self, initWithMemory, mem, length);
210 }
211
212 return release(self);
213}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195

◆ initWithData()

Data * initWithData ( Data self,
const Data data 
)

Initializes this Data with the contents of data.

Parameters
selfThe Data.
dataA Data.
Returns
The initialized Data, or NULL on error.

Definition at line 335 of file Data.c.

335 {
336
337 self = $(self, initWithCapacity, data->length);
338 if (self) {
339 $(self, appendData, data);
340 }
341
342 return self;
343}
void appendData(Data *self, const Data *data)
Appends the given data to this Data.
Definition Data.c:277

◆ initWithMemory()

Data * initWithMemory ( Data self,
ident  mem,
size_t  length 
)

Initializes this Data, taking ownership of the specified memory.

Parameters
selfThe Data.
memThe dynamically allocated memory to back this Data.
lengthThe length of mem in bytes.
Returns
The initialized Data, or NULL on error.

Definition at line 219 of file Data.c.

219 {
220
221 self = $(self, initWithConstMemory, mem, length);
222 if (self) {
223 self->destroy = free;
224 }
225
226 return self;
227}

◆ setLength()

void setLength ( Data self,
size_t  length 
)

Sets the length of this Data, truncating or expanding it.

Parameters
selfThe Data.
lengthThe new desired length.
Remarks
If the data is expanded, the newly allocated bytes are filled with zeros.

Definition at line 349 of file Data.c.

349 {
350
351 const size_t newCapacity = (length / _pageSize + 1) * _pageSize;
352 if (newCapacity > self->capacity) {
353
354 if (self->bytes == NULL) {
355 self->bytes = calloc(newCapacity, sizeof(uint8_t));
356 assert(self->bytes);
357 } else {
358 if (self->destroy != free) {
359 uint8_t *owned = malloc(newCapacity);
360 assert(owned);
361 memcpy(owned, self->bytes, self->length);
362 if (length > self->length) {
363 memset(owned + self->length, 0, length - self->length);
364 }
365 self->bytes = owned;
366 self->destroy = free;
367 } else {
368 self->bytes = realloc(self->bytes, newCapacity);
369 assert(self->bytes);
370 if (length > self->length) {
371 memset(self->bytes + self->length, 0, length - self->length);
372 }
373 }
374 }
375
376 self->capacity = newCapacity;
377 } else if (length > self->length) {
378 memset(self->bytes + self->length, 0, length - self->length);
379 }
380
381 self->length = length;
382}
size_t _pageSize
Definition Class.c:39

◆ writeToFile()

bool writeToFile ( const Data self,
const char *  path 
)

Writes this Data to path.

Parameters
selfThe Data.
pathThe path of the file to write.
Returns
true on success, false on error.

Definition at line 233 of file Data.c.

233 {
234
235 assert(path);
236
237 FILE *file = fopen(path, "w");
238 if (file) {
239
240 size_t count = 1;
241
242 if (self->length) {
243 count = fwrite(self->bytes, self->length, 1, file);
244 }
245
246 fclose(file);
247
248 if (count == 1) {
249 return true;
250 }
251 }
252
253 return false;
254}

The documentation for this struct was generated from the following files: