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

#include <Resource.h>

Overview

Resources provide an abstraction for file and stream resources.

Definition at line 53 of file Resource.h.

Inheritance diagram for Resource:
Object

Properties

Datadata
 The resource data. More...
 
char * name
 The resource name. 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_Resource (void)
 The Resource archetype. More...
 
void addResourcePath (const char *path)
 Adds the specified Resource path. More...
 
void addResourceProvider (ResourceProvider provider)
 Adds the specified ResourceProvider. More...
 
ResourceinitWithBytes (Resource *self, const uint8_t *bytes, size_t length, const char *name)
 Initializes this Resource with the specified bytes. More...
 
ResourceinitWithData (Resource *self, Data *data)
 
ResourceinitWithData (Resource *self, Data *data, const char *name)
 Initializes this Resource with the specified Data. More...
 
ResourceinitWithName (Resource *self, const char *name)
 Initializes this Resource with the specified name. More...
 
void removeResourcePath (const char *path)
 Removes the specified Resource path. More...
 
void removeResourceProvider (ResourceProvider provider)
 Removes the specified ResourceProvider. More...
 
ResourceresourceWithName (const char *name)
 Returns a new Resource with the specified name. 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

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

Property Details

◆ data

Data* Resource::data ( void  )

The resource data.

Definition at line 69 of file Resource.h.

◆ interface

ResourceInterface* Resource::interface
protected

The interface.

Definition at line 64 of file Resource.h.

◆ name

char* Resource::name

The resource name.

Definition at line 74 of file Resource.h.

◆ object

Object Resource::object

The superclass.

Definition at line 58 of file Resource.h.

Method Details

◆ _Resource()

Class * _Resource ( void  )

The Resource archetype.

Returns
The Resource Class.

Definition at line 241 of file Resource.c.

241 {
242 static Class *clazz;
243 static Once once;
244
245 do_once(&once, {
246 clazz = _initialize(&(const ClassDef) {
247 .name = "Resource",
248 .superclass = _Object(),
249 .instanceSize = sizeof(Resource),
250 .interfaceOffset = offsetof(Resource, interface),
251 .interfaceSize = sizeof(ResourceInterface),
253 .destroy = destroy,
254 });
255 });
256
257 return clazz;
258}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void destroy(Class *clazz)
Definition: Resource.c:232
static void initialize(Class *clazz)
Definition: Resource.c:193
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 * 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
Resources provide an abstraction for file and stream resources.
Definition: Resource.h:53
ResourceInterface * interface
The interface.
Definition: Resource.h:64

◆ addResourcePath()

void addResourcePath ( const char *  path)

Adds the specified Resource path.

Parameters
pathThe resource path to add.
Remarks
Resource paths may be relative to the working directory, or absolute.

Definition at line 61 of file Resource.c.

61 {
62
63 assert(path);
64 String *string = str(path);
65
66 $(_resourcePaths, addObject, string);
67 release(string);
68}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static void addObject(MutableArray *self, const ident obj)
Definition: MutableArray.c:99
static MutableArray * _resourcePaths
Definition: Resource.c:36
String * str(const char *fmt,...)
Definition: String.c:739
Immutable UTF-8 strings.
Definition: String.h:69

◆ addResourceProvider()

void addResourceProvider ( ResourceProvider  provider)

Adds the specified ResourceProvider.

Parameters
providerThe ResoureProvider to add.

Definition at line 74 of file Resource.c.

74 {
75
76 assert(provider);
77 Value *value = $(alloc(Value), initWithValue, provider);
78
80 release(value);
81}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static Number * initWithValue(Number *self, const double value)
Definition: Number.c:137
static MutableArray * _resourceProviders
Definition: Resource.c:37
Values provide Object encapsulation for C types.
Definition: Value.h:45

◆ initWithBytes()

Resource * initWithBytes ( Resource self,
const uint8_t *  bytes,
size_t  length,
const char *  name 
)

Initializes this Resource with the specified bytes.

Parameters
selfThe Resource.
bytesThe bytes.
lengthThe length of bytes.
nameThe resource name.
Returns
The initialized Resource, or NULL on error.

Definition at line 87 of file Resource.c.

87 {
88
89 Data *data = $$(Data, dataWithBytes, bytes, length);
90 self = $(self, initWithData, data, name);
91
93 return self;
94}
static Data * dataWithBytes(const uint8_t *bytes, size_t length)
Definition: Data.c:109
Immutable data buffers.
Definition: Data.h:50
Resource * initWithData(Resource *self, Data *data)
Data * data
The resource data.
Definition: Resource.h:69
char * name
The resource name.
Definition: Resource.h:74

◆ initWithData() [1/2]

Resource * initWithData ( Resource self,
Data data 
)

◆ initWithData() [2/2]

Resource * initWithData ( Resource self,
Data data,
const char *  name 
)

Initializes this Resource with the specified Data.

Parameters
selfThe Resource.
dataThe Data.
nameThe resource name.
Returns
The initialized Resource, or NULL on error.

Definition at line 100 of file Resource.c.

100 {
101
102 self = (Resource *) super(Object, self, init);
103 if (self) {
104 assert(data);
105 self->data = retain(data);
106
107 assert(name);
108 self->name = strdup(name);
109 }
110
111 return self;
112}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#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

◆ initWithName()

Resource * initWithName ( Resource self,
const char *  name 
)

Initializes this Resource with the specified name.

Parameters
selfThe Resource.
nameThe resource name.
Returns
The initialized Resource, or NULL on error.

The configured resource paths are searched, in order, for a file by the given name.

Definition at line 118 of file Resource.c.

118 {
119
120 Data *data = NULL;
121
122 const Array *resourceProviders = (Array *) _resourceProviders;
123 for (size_t i = 0; i < resourceProviders->count && data == NULL; i++) {
124
125 const Value *value = $(resourceProviders, objectAtIndex, i);
126 data = ((ResourceProvider) (value->value))(name);
127 }
128
129 const Array *resourcePaths = (Array *) _resourcePaths;
130 for (size_t i = 0; i < resourcePaths->count && data == NULL; i++) {
131
132 const String *resourcePath = $(resourcePaths, objectAtIndex, i);
133 String *path = str("%s%s%s", resourcePath->chars, PATH_SEPAR, name);
134
135 struct stat s;
136 if (stat(path->chars, &s) == 0 && S_ISREG(s.st_mode)) {
138 }
139
140 release(path);
141 }
142
143 if (data) {
144 self = $(self, initWithData, data, name);
145 } else {
146 self = release(self);
147 }
148
149 release(data);
150
151 return self;
152}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
static Data * dataWithContentsOfFile(const char *path)
Definition: Data.c:127
Data *(* ResourceProvider)(const char *name)
Applications may specify a provider function for loading via file system abstractions.
Definition: Resource.h:47
#define PATH_SEPAR
Definition: Resource.h:33
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Array.h:72
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
ident value
The backing value.
Definition: Value.h:61

◆ removeResourcePath()

void removeResourcePath ( const char *  path)

Removes the specified Resource path.

Parameters
pathThe resource path to remove.

Definition at line 158 of file Resource.c.

158 {
159
160 String *string = $$(String, stringWithCharacters, path);
161
162 $(_resourcePaths, removeObject, string);
163
164 release(string);
165}
static void removeObject(MutableArray *self, const ident obj)
Definition: MutableArray.c:270
static String * stringWithCharacters(const char *chars)
Definition: String.c:487

◆ removeResourceProvider()

void removeResourceProvider ( ResourceProvider  provider)

Removes the specified ResourceProvider.

Parameters
providerThe ResourceProvider to remove.

Definition at line 171 of file Resource.c.

171 {
172
173 Value *value = $(alloc(Value), initWithValue, provider);
174
176
177 release(value);
178}

◆ resourceWithName()

Resource * resourceWithName ( const char *  name)

Returns a new Resource with the specified name.

Parameters
nameThe resource name.
Returns
The new Resource, or NULL on error.

Definition at line 184 of file Resource.c.

184 {
185 return $(alloc(Resource), initWithName, name);
186}
Resource * initWithName(Resource *self, const char *name)
Initializes this Resource with the specified name.
Definition: Resource.c:118

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