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

#include <Data.h>

Overview

Immutable data buffers.

Definition at line 50 of file Data.h.

Inheritance diagram for Data:
Object MutableData

Properties

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_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

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

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 283 of file Data.c.

283 {
284 static Class *clazz;
285 static Once once;
286
287 do_once(&once, {
288 clazz = _initialize(&(const ClassDef) {
289 .name = "Data",
290 .superclass = _Object(),
291 .instanceSize = sizeof(Data),
292 .interfaceOffset = offsetof(Data, interface),
293 .interfaceSize = sizeof(DataInterface),
295 });
296 });
297
298 return clazz;
299}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Data.c:260
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
Immutable 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:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ 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 109 of file Data.c.

109 {
110
111 return $(alloc(Data), initWithBytes, bytes, length);
112}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
size_t length
The length of bytes.
Definition: Data.h:76
uint8_t * bytes
The bytes.
Definition: Data.h:66
Data * initWithBytes(Data *self, const uint8_t *bytes, size_t length)
Initializes this Data by copying length of bytes.
Definition: Data.c:145

◆ 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 118 of file Data.c.

118 {
119
120 return $(alloc(Data), initWithConstMemory, mem, length);
121}
Data * initWithConstMemory(Data *self, const ident mem, size_t length)
Initializes this Data with the given const memory.
Definition: Data.c:159

◆ 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 127 of file Data.c.

127 {
128
129 return $(alloc(Data), initWithContentsOfFile, path);
130}
Data * initWithContentsOfFile(Data *self, const char *path)
Initializes this Data with the contents of the file at path.
Definition: Data.c:174

◆ 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 136 of file Data.c.

136 {
137
138 return $(alloc(Data), initWithMemory, mem, length);
139}
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Definition: Data.c:209

◆ 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 145 of file Data.c.

145 {
146
147 ident mem = malloc(length);
148 assert(mem);
149
150 memcpy(mem, bytes, length);
151
152 return $(self, initWithMemory, mem, length);
153}
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49

◆ 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 159 of file Data.c.

159 {
160
161 self = (Data *) super(Object, self, init);
162 if (self) {
163 self->bytes = mem;
164 self->length = length;
165 }
166
167 return self;
168}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ 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 174 of file Data.c.

174 {
175
176 assert(path);
177
178 FILE *file = fopen(path, "rb");
179 if (file) {
180 ident mem = NULL;
181
182 int err = fseek(file, 0, SEEK_END);
183 assert(err == 0);
184
185 const size_t length = ftell(file);
186 if (length) {
187
188 mem = malloc(length);
189 assert(mem);
190
191 err = fseek(file, 0, SEEK_SET);
192 assert(err == 0);
193
194 const size_t read = fread(mem, length, 1, file);
195 assert(read == 1);
196 }
197
198 fclose(file);
199 return $(self, initWithMemory, mem, length);
200 }
201
202 return release(self);
203}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196

◆ 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 209 of file Data.c.

209 {
210
211 self = $(self, initWithConstMemory, mem, length);
212 if (self) {
213 self->destroy = free;
214 }
215
216 return self;
217}
DataDestructor destroy
An optional destructor that, if set, is called on dealloc.
Definition: Data.h:71

◆ mutableCopy()

MutableData * mutableCopy ( const Data self)
Parameters
selfThe Data.
Returns
A MutableData with the contents of this Data.

Definition at line 223 of file Data.c.

223 {
224
225 return $(alloc(MutableData), initWithData, self);
226}
static MutableData * initWithData(MutableData *self, const Data *data)
Definition: MutableData.c:124
Mutable data buffers.
Definition: MutableData.h:40

◆ 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 232 of file Data.c.

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

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