Skip to content

Commit 8449655

Browse files
committed
Added offset for template methods
1 parent 5c593cf commit 8449655

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

LibSource/Resource.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@ bool Resource::setData(void* src, uint32_t length, bool copy){
2222
}
2323

2424
template<typename Array, typename Element>
25-
const Array Resource::asArray() const {
25+
const Array Resource::asArray(uint32_t offset, uint32_t max_size) const {
2626
// Data is expected to be aligned
27-
return Array((Element*)getData(), size / sizeof(Element));
27+
if (max_size > size - offset)
28+
max_size = size - offset;
29+
return Array((Element*)(getData() + offset), max_size / sizeof(Element));
2830
}
2931

30-
template const FloatArray Resource::asArray<FloatArray, float>() const;
32+
template const FloatArray Resource::asArray<FloatArray, float>(uint32_t offset, uint32_t max_size) const;
3133

3234
template<typename Array, typename Element>
33-
Array Resource::asArray() {
35+
Array Resource::asArray(uint32_t offset, uint32_t max_size) {
3436
// Data is expected to be aligned
35-
return Array((Element*)getData(), size / sizeof(Element));
37+
if (max_size > size - offset)
38+
max_size = size - offset;
39+
return Array((Element*)(getData() + offset), max_size / sizeof(Element));
3640
}
3741

38-
template FloatArray Resource::asArray<FloatArray, float>();
42+
template FloatArray Resource::asArray<FloatArray, float>(uint32_t offset, uint32_t max_size);
3943

4044
void Resource::destroy(Resource resource) {
4145
if (resource.isAllocated()){

LibSource/Resource.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,31 @@ class Resource {
5151
/**
5252
* Get pointer to data. This may be NULL if no buffer is assigned yet.
5353
* This is the **const** version
54+
*
55+
* @param offset: offset in bytes
5456
*/
55-
const uint8_t* getData() const {
56-
return const_cast<const uint8_t*>(data);
57+
const uint8_t* getData(uint32_t offset = 0) const {
58+
return const_cast<const uint8_t*>(data + offset);
5759
}
5860

5961
/**
6062
* Template method for converting data to an object of particular class
63+
*
64+
* @param offset: offset in bytes
6165
*/
6266
template<typename Payload>
63-
const Payload& getData() const {
64-
return *(reinterpret_cast<const Payload*>(data));
67+
const Payload& getData(uint32_t offset = 0) const {
68+
return *(reinterpret_cast<const Payload*>(data + offset));
6569
}
6670

6771
/**
6872
* Template method for converting data to an object. This is the **const** version
73+
*
74+
* @param offset: offset in bytes
6975
*/
7076
template<typename Payload>
71-
Payload& getData() {
72-
return *(reinterpret_cast<Payload*>(data));
77+
Payload& getData(uint32_t offset = 0) {
78+
return *(reinterpret_cast<Payload*>(data + offset));
7379
}
7480

7581
/**
@@ -140,15 +146,21 @@ class Resource {
140146

141147
/**
142148
* Array conversion
149+
*
150+
* @param offset: offset in bytes
151+
* @param max_size maximum size, actual size can be smaller depending on object size
143152
*/
144153
template<typename Array, typename Element>
145-
const Array asArray() const;
154+
const Array asArray(uint32_t offset = 0, uint32_t max_size = 0xFFFFFFFF) const;
146155

147156
/**
148157
* Array conversion. This is the **const** version
158+
*
159+
* @param offset: offset in bytes
160+
* @param max_size maximum size, actual size can be smaller depending on object size
149161
*/
150162
template<typename Array, typename Element>
151-
Array asArray();
163+
Array asArray(uint32_t offset = 0, uint32_t max_size = 0xFFFFFFFF);
152164

153165
/**
154166
* Create a new resource with allocated buffer

0 commit comments

Comments
 (0)