Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
MutableData Struct Reference

#include <MutableData.h>

Overview

Mutable data buffers.

Definition at line 40 of file MutableData.h.

Inheritance diagram for MutableData:
Data Object

Properties

Data data
 The superclass. More...
 
- Properties inherited from Data
uint8_t * bytes
 The bytes. More...
 
DataDestructor destroy
 An optional destructor that, if set, is called on dealloc. More...
 
size_t length
 The length of bytes. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

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

Protected Attributes

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

Property Details

◆ data

MutableData * data ( void  )

The superclass.

Definition at line 45 of file MutableData.h.

◆ interface

MutableDataInterface* MutableData::interface
protected

The interface.

Definition at line 51 of file MutableData.h.

Method Details

◆ _MutableData()

Class * _MutableData ( void  )

The MutableData archetype.

Returns
The MutableData Class.

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
MutableDataInterface * interface
The interface.
Definition: MutableData.h:51
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ appendBytes()

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

Appends the given bytes to this Data.

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

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()

void appendData ( MutableData self,
const Data data 
)

Appends the given data to this Data.

Parameters
selfThe MutableData.
dataThe Data to append.

Definition at line 66 of file MutableData.c.

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

◆ data()

MutableData * data ( void  )

Returns a new MutableData.

Returns
The new MutableData, or NULL on error.

Definition at line 75 of file MutableData.c.

75 {
76
77 return $(alloc(MutableData), init);
78}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
MutableData * init(MutableData *self)
Initializes this Data with length 0.
Definition: MutableData.c:93

◆ dataWithCapacity()

MutableData * dataWithCapacity ( size_t  capacity)

Returns a new MutableData with the given capacity.

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

Definition at line 84 of file MutableData.c.

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

◆ init()

MutableData * init ( MutableData self)

Initializes this Data with length 0.

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

Definition at line 93 of file MutableData.c.

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

◆ initWithCapacity()

MutableData * initWithCapacity ( MutableData self,
size_t  capacity 
)

Initializes this Data with the given capacity.

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

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
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ initWithData()

MutableData * initWithData ( MutableData self,
const Data data 
)

Initializes this Data with the contents of data.

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

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}
void appendData(MutableData *self, const Data *data)
Appends the given data to this Data.
Definition: MutableData.c:66

◆ setLength()

void setLength ( MutableData self,
size_t  length 
)

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

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

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

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