1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef __RESOURCE_STORAGE_H__
#define __RESOURCE_STORAGE_H__
#include <cstddef>
#include <stdint.h>
#include "FloatArray.h"
class Resource {
public:
/**
* Check if data is available
*/
bool hasData() const {
return data != NULL;
}
/**
* Get pointer to data. This may be NULL if no buffer is assigned yet.
*/
void* getData() {
return data;
}
/**
* Get buffer size in bytes
*/
size_t getSize() const {
return size;
}
bool exists() const {
return size != 0;
}
bool isMutable() const {
return allocated;
}
/**
* Get resource name
*/
const char* getName() const {
return name;
}
/**
* Array conversion.
*
* @param offset: offset in bytes
* @param max_size maximum size, actual size can be smaller depending on object size
*/
template<typename Array, typename Element>
Array asArray(size_t offset = 0, size_t max_size = 0xFFFFFFFF);
FloatArray asFloatArray(size_t offset = 0, size_t max_size = 0xFFFFFFFF){
return asArray<FloatArray, float>(offset, max_size);
}
/**
* Get resource from storage.
* Returned object must be garbage collected with Resource::destroy()
*
* @param name resource name
* @return NULL if resource does not exist or can't be read.
*/
static Resource* open(const char* name);
/**
* Open resource and load data.
* Allocates extra memory to hold the resource if required.
* Returned object must be garbage collected with Resource::destroy()
*
* @param name resource name
* @return NULL if resource does not exist or can't be read.
*/
static Resource* load(const char* name);
/**
* Clean up used memory resources.
*/
static void destroy(Resource* resource);
/**
* Read data from resource into memory
*
* @param len maximum number of bytes to read
* @param offset index of first byte to read from
*
* @return number of bytes actually read
*/
size_t read(void* dest, size_t len, size_t offset=0);
Resource(): name(NULL), size(0), data(NULL), allocated(false) {}
~Resource(){}
protected:
Resource(const char* name, size_t size, void* data)
: name(name), size(size), data((uint8_t*)data), allocated(false) {}
const char* name;
size_t size;
uint8_t* data;
bool allocated;
};
#endif