sead
Loading...
Searching...
No Matches
aglResCommon.h
Go to the documentation of this file.
1#pragma once
2
3#include <basis/seadAssert.h>
4#include <basis/seadTypes.h>
5
6namespace agl {
7
8void ModifyEndianU32(bool is_le, void* p_data, size_t size);
9
10template <typename _DataType>
12{
13public:
14 typedef _DataType DataType;
15
16public:
18 : mpData(nullptr)
19 {
20 }
21
22 ResCommon(const void* data)
23 : mpData(static_cast<const DataType*>(data))
24 {
25 }
26
27 bool isValid() const
28 {
29 return mpData != nullptr;
30 }
31
32 void verify() const
33 {
34 if (ref().mSigWord != DataType::cSignature)
35 {
36 const char* signature = ptr()->mSignature;
37 SEAD_ASSERT_MSG(false, "Wrong binary. [%c%c%c%c].",
38 signature[0], signature[1],
39 signature[2], signature[3]);
40 }
41
42 if (ref().mVersion != DataType::cVersion)
43 {
44 SEAD_ASSERT_MSG(false, "Version error.current:%d binary:%d",
45 DataType::cVersion,
46 ref().mVersion);
47 }
48 }
49
51 {
52 return const_cast<DataType*>(mpData);
53 }
54
55 const DataType* ptr() const
56 {
57 return mpData;
58 }
59
61 {
63 return *ptr();
64 }
65
66 const DataType& ref() const
67 {
69 return *ptr();
70 }
71
72private:
74};
75
76#define AGL_RES_COMMON(class_name)
77 public:
78 class_name()
79 : ResCommon<typename class_name::DataType>()
80 {
81 }
82
83 class_name(const void* data)
84 : ResCommon<typename class_name::DataType>(data)
85 {
86 }
87
88#define AGL_RES_FILE_HEADER()
89 public:
90 bool modifyEndian() const
91 {
92 return ref().mEndian & DataType::cEndianCheckBit;
93 }
94
95 bool isEndianResolved() const
96 {
97 return !modifyEndian();
98 }
99
100 void setEndianResolved()
101 {
102 ref().mEndian = 1 - ref().mEndian;
103 }
104
105template <typename DataType>
107{
110 // DataType mData[];
111
112 typedef DataType ElemType;
113};
114
115template <typename Type>
116class ResArray : public ResCommon< ResArrayData<typename Type::DataType> >
117{
119
120public:
121 typedef Type ElemType;
122 typedef typename Type::DataType ElemDataType;
123 typedef typename ResArray<Type>::DataType DataType;
125
126public:
128 {
129 public:
130 iterator(s32 index, ElemDataType* elem)
131 : mIndex(index)
132 , mElem(elem)
133 {
134 }
135
136 friend bool operator==(const iterator& lhs, const iterator& rhs)
137 {
138 return lhs.mIndex == rhs.mIndex;
139 }
140
141 friend bool operator!=(const iterator& lhs, const iterator& rhs)
142 {
143 return lhs.mIndex != rhs.mIndex;
144 }
145
147 {
148 ++mIndex;
149 mElem = (ElemDataType*)((uintptr_t)mElem + Type(mElem).ref().mSize);
150 return *this;
151 }
152
153 ElemDataType& operator*() const { return *mElem; }
154 ElemDataType* operator->() const { return mElem; }
155 s32 getIndex() const { return mIndex; }
156
157 private:
160 };
161
163 {
164 public:
165 constIterator(s32 index, const ElemDataType* elem)
166 : mIndex(index)
167 , mElem(elem)
168 {
169 }
170
171 friend bool operator==(const constIterator& lhs, const constIterator& rhs)
172 {
173 return lhs.mIndex == rhs.mIndex;
174 }
175
176 friend bool operator!=(const constIterator& lhs, const constIterator& rhs)
177 {
178 return lhs.mIndex != rhs.mIndex;
179 }
180
182 {
183 ++mIndex;
184 mElem = (const ElemDataType*)((uintptr_t)mElem + Type(mElem).ref().mSize);
185 return *this;
186 }
187
188 const ElemDataType& operator*() const { return *mElem; }
189 const ElemDataType* operator->() const { return mElem; }
190 s32 getIndex() const { return mIndex; }
191
192 private:
195 };
196
197public:
198 iterator begin() { return iterator(0, (ElemDataType*)(Base::ptr() + 1)); }
199 constIterator begin() const { return constIterator(0, (const ElemDataType*)(Base::ptr() + 1)); }
200 constIterator constBegin() const { return constIterator(0, (const ElemDataType*)(Base::ptr() + 1)); }
201
202 iterator end() { return iterator(getNum(), nullptr); }
203 constIterator end() const { return constIterator(getNum(), nullptr); }
204 constIterator constEnd() const { return constIterator(getNum(), nullptr); }
205
206public:
207 u32 getNum() const
208 {
209 return Base::ref().mNum;
210 }
211
212 ElemType get(s32 n) const
213 {
214 SEAD_ASSERT(0 <= n && n <= static_cast< int >( this->getNum() ));
215
217 constIterator itr_end = constIterator(n, nullptr);
218
219 while (itr != itr_end)
220 ++itr;
221
222 return &(*itr);
223 }
224
225 void modifyEndianArray(bool is_le)
226 {
227 ModifyEndianU32(is_le, Base::ptr(), sizeof(DataType));
228
229 for (iterator it = begin(), it_end = end(); it != it_end; ++it)
230 ModifyEndianU32(is_le, &(*it), sizeof(ElemDataType));
231 }
232};
233
234#define AGL_RES_ARRAY(class_name)
235 public:
236 class_name()
237 : ResArray<class_name::ElemType>()
238 {
239 }
240
241 class_name(const void* data)
242 : ResArray<class_name::ElemType>(data)
243 {
244 }
245
246}
#define AGL_RES_COMMON(class_name)
Definition aglResCommon.h:76
Definition aglResCommon.h:163
s32 mIndex
Definition aglResCommon.h:193
const ElemDataType * operator->() const
Definition aglResCommon.h:189
s32 getIndex() const
Definition aglResCommon.h:190
const ElemDataType & operator*() const
Definition aglResCommon.h:188
constIterator & operator++()
Definition aglResCommon.h:181
friend bool operator!=(const constIterator &lhs, const constIterator &rhs)
Definition aglResCommon.h:176
constIterator(s32 index, const ElemDataType *elem)
Definition aglResCommon.h:165
const ElemDataType * mElem
Definition aglResCommon.h:194
friend bool operator==(const constIterator &lhs, const constIterator &rhs)
Definition aglResCommon.h:171
Definition aglResCommon.h:128
iterator & operator++()
Definition aglResCommon.h:146
iterator(s32 index, ElemDataType *elem)
Definition aglResCommon.h:130
ElemDataType * operator->() const
Definition aglResCommon.h:154
friend bool operator==(const iterator &lhs, const iterator &rhs)
Definition aglResCommon.h:136
s32 mIndex
Definition aglResCommon.h:158
friend bool operator!=(const iterator &lhs, const iterator &rhs)
Definition aglResCommon.h:141
s32 getIndex() const
Definition aglResCommon.h:155
ElemDataType & operator*() const
Definition aglResCommon.h:153
ElemDataType * mElem
Definition aglResCommon.h:159
Definition aglResCommon.h:117
u32 getNum() const
Definition aglResCommon.h:207
constIterator constEnd() const
Definition aglResCommon.h:204
constIterator begin() const
Definition aglResCommon.h:199
constIterator end() const
Definition aglResCommon.h:203
iterator begin()
Definition aglResCommon.h:198
iterator end()
Definition aglResCommon.h:202
constIterator constBegin() const
Definition aglResCommon.h:200
ResCommon< DataType > Base
Definition aglResCommon.h:124
Type::DataType ElemDataType
Definition aglResCommon.h:122
Type ElemType
Definition aglResCommon.h:121
void modifyEndianArray(bool is_le)
Definition aglResCommon.h:225
ResArray< Type >::DataType DataType
Definition aglResCommon.h:123
ElemType get(s32 n) const
Definition aglResCommon.h:212
Definition aglResCommon.h:12
const DataType * mpData
Definition aglResCommon.h:73
DataType * ptr()
Definition aglResCommon.h:50
ResCommon()
Definition aglResCommon.h:17
ResCommon(const void *data)
Definition aglResCommon.h:22
DataType & ref()
Definition aglResCommon.h:60
bool isValid() const
Definition aglResCommon.h:27
const DataType & ref() const
Definition aglResCommon.h:66
const DataType * ptr() const
Definition aglResCommon.h:55
_DataType DataType
Definition aglResCommon.h:14
void verify() const
Definition aglResCommon.h:32
Definition aglDisplayList.cpp:5
void ModifyEndianU32(bool is_le, void *p_data, size_t size)
Definition aglResCommon.cpp:7
#define SEAD_ASSERT(condition)
Definition seadAssert.h:24
#define SEAD_ASSERT_MSG(condition, format,...)
Definition seadAssert.h:33
Definition aglResCommon.h:107
u32 mSize
Definition aglResCommon.h:108
DataType ElemType
Definition aglResCommon.h:112
u32 mNum
Definition aglResCommon.h:109