NW4F G3d
Loading...
Searching...
No Matches
g3d_ResCommon.h
Go to the documentation of this file.
1#ifndef NW_G3D_RES_RESCOMMON_H_
2#define NW_G3D_RES_RESCOMMON_H_
3
4#include <nw/g3d/g3d_config.h>
5#include <nw/g3d/ut/g3d_Inlines.h>
6#include <nw/g3d/res/g3d_ResDefs.h>
7#include <cstring>
8
9namespace nw { namespace g3d { namespace res {
10
11#define NW_G3D_VALIDITY_ASSERT
12 NW_G3D_ASSERTMSG(this, "%s::%s: Object not valid.", GetClassName(), __FUNCTION__)
13
14#define NW_G3D_RES_COMMON_ALIGN(class_name, alignment) public
15 :
16 typedef class_name##Data DataType;
17 DataType* ptr() { return this; }
18 const DataType* ptr() const { return this; }
19 DataType& ref() { NW_G3D_VALIDITY_ASSERT; return *ptr(); }
20 const DataType& ref() const { NW_G3D_VALIDITY_ASSERT; return *ptr(); }
21 static class_name* ResCast(DataType* ptr)
22 { NW_G3D_ASSERT_ADDR_ALIGNMENT(ptr, alignment); return static_cast<class_name*>(ptr); }
23 static const class_name* ResCast(const DataType* ptr)
24 { NW_G3D_ASSERT_ADDR_ALIGNMENT(ptr, alignment); return static_cast<const class_name*>(ptr); }
25 static const char* GetClassName() { return #class_name; } private
26 :
28
29
30#define NW_G3D_RES_COMMON(class_name) NW_G3D_RES_COMMON_ALIGN(class_name, 4)
31
33{
34 typedef u32 LengthType;
36 char str[sizeof(LengthType)];
37};
38
39class ResName : private ResNameData
40{
42
43public:
44 using ResNameData::LengthType;
45
46 size_t GetLength() const { return static_cast<size_t>(ref().len); }
47
48 const char* GetName() const { return &ref().str[0]; }
49
50 bool Equals(const char* str, size_t len) const
51 {
52 return (GetLength() == len) && (std::strcmp(GetName(), str) == 0);
53 }
54
55 bool Equals(const ResName* rhs) const
56 {
57 return (ptr() == rhs->ptr()) || Equals(rhs->GetName(), rhs->GetLength());
58 }
59};
60
61class Offset
62{
63private:
65
66public:
67 operator s32() const { return offset; }
68
69 Offset& operator=(s32 ofs) { offset = ofs; return *this; }
70
71 void set_ptr(const void* ptr)
72 {
73 offset = (ptr == NULL) ? 0 :
74 static_cast<s32>(reinterpret_cast<intptr_t>(ptr) - reinterpret_cast<intptr_t>(this));
75 }
76
77 void* to_ptr() { return to_ptr<void>(); }
78
79 const void* to_ptr() const { return to_ptr<void>(); }
80
81 template<typename T>
82 T* to_ptr() { return (offset == 0) ? NULL : AddOffset<T>(this, static_cast<size_t>(offset)); }
83
84 template<typename T>
85 const T* to_ptr() const { return (offset == 0) ? NULL : AddOffset<T>(this, static_cast<size_t>(offset)); }
86
87 Offset* to_table_ptr() { return static_cast<Offset*>(to_ptr()); }
88
89 const Offset* to_table_ptr() const { return static_cast<const Offset*>(to_ptr()); }
90};
91
93{
94public:
96
97 const char* to_ptr() const { return offset.to_ptr<char>(); }
98
99 void set_ptr(const char* ptr) { offset.set_ptr(ptr); }
100
101 const ResName* GetResName() const
102 {
103 return offset == 0 ? NULL : ResName::ResCast(
104 AddOffset<ResNameData>(this, static_cast<size_t>(offset) - sizeof(ResName::LengthType)));
105 }
106};
107
109{
110public:
111 union
112 {
115 void* ptr;
116#endif
117 };
118
119 void* to_ptr() { return to_ptr<void>(); }
120
121 const void* to_ptr() const { return to_ptr<void>(); }
122
123 template <typename T>
125 {
127 return static_cast<T*>(ptr);
128#else
129 return NULL;
130#endif
131 }
132
133 template <typename T>
134 const T* to_ptr() const
135 {
137 return static_cast<const T*>(ptr);
138#else
139 return NULL;
140#endif
141 }
142
143 BinPtr& set_ptr(void* ptr)
144 {
146 this->ptr = ptr;
147#else
148 NW_G3D_ASSERT(ptr == NULL);
149 NW_G3D_UNUSED(ptr);
150 this->addr = 0;
151#endif
152 return *this;
153 }
154};
155
157{
158 enum
159 {
161 };
162
163 union
164 {
167 };
168 union
169 {
172 };
176};
177
179{
180 union
181 {
184 };
185};
186
192
194{
196
197public:
198 size_t GetSize() const { return ref().size; }
199
200 void* GetData() { return ref().offset.to_ptr(); }
201
202 const void* GetData() const { return ref().offset.to_ptr(); }
203};
204
205} } } // namespace nw::g3d::res
206
207#endif // NW_G3D_RES_RESCOMMON_H_
Definition g3d_ResCommon.h:109
const T * to_ptr() const
Definition g3d_ResCommon.h:134
BinPtr & set_ptr(void *ptr)
Definition g3d_ResCommon.h:143
u32 addr
Definition g3d_ResCommon.h:113
const void * to_ptr() const
Definition g3d_ResCommon.h:121
void * ptr
Definition g3d_ResCommon.h:115
T * to_ptr()
Definition g3d_ResCommon.h:124
void * to_ptr()
Definition g3d_ResCommon.h:119
Definition g3d_ResCommon.h:93
const char * to_ptr() const
Definition g3d_ResCommon.h:97
const ResName * GetResName() const
Definition g3d_ResCommon.h:101
void set_ptr(const char *ptr)
Definition g3d_ResCommon.h:99
Offset offset
Definition g3d_ResCommon.h:95
Definition g3d_ResCommon.h:62
s32 offset
Definition g3d_ResCommon.h:64
Definition g3d_ResCommon.h:194
const void * GetData() const
Definition g3d_ResCommon.h:202
size_t GetSize() const
Definition g3d_ResCommon.h:198
void * GetData()
Definition g3d_ResCommon.h:200
Definition g3d_ResCommon.h:40
bool Equals(const char *str, size_t len) const
Definition g3d_ResCommon.h:50
size_t GetLength() const
Definition g3d_ResCommon.h:46
bool Equals(const ResName *rhs) const
Definition g3d_ResCommon.h:55
const char * GetName() const
Definition g3d_ResCommon.h:48
#define NW_G3D_RES_COMMON_ALIGN(class_name, alignment)
Definition g3d_ResCommon.h:14
#define NW_G3D_VALIDITY_ASSERT
Definition g3d_ResCommon.h:11
#define NW_G3D_RES_COMMON(class_name)
Definition g3d_ResCommon.h:30
#define NW_G3D_ASSERTMSG(exp,...)
Definition g3d_assert.h:13
#define NW_G3D_ASSERT_ADDR_ALIGNMENT(addr, alignment)
Definition g3d_assert.h:36
#define NW_G3D_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition g3d_defs.h:81
#define NW_G3D_HOST_PTRSIZE
Definition g3d_defs.h:40
#define NW_G3D_TARGET_PTRSIZE
Definition g3d_defs.h:34
Definition g3d_defs.h:122
Definition g3d_GfxManage.cpp:10
Definition g3d_ResCommon.h:179
u8 signature[4]
Definition g3d_ResCommon.h:182
u32 sigWord
Definition g3d_ResCommon.h:183
Definition g3d_ResCommon.h:157
u16 headerSize
Definition g3d_ResCommon.h:174
u32 fileSize
Definition g3d_ResCommon.h:175
u8 signature[4]
Definition g3d_ResCommon.h:165
@ BYTE_ORDER_MARK
Definition g3d_ResCommon.h:160
u16 byteOrder
Definition g3d_ResCommon.h:173
u32 sigWord
Definition g3d_ResCommon.h:166
u32 verWord
Definition g3d_ResCommon.h:171
u8 version[4]
Definition g3d_ResCommon.h:170
Definition g3d_ResCommon.h:188
Offset offset
Definition g3d_ResCommon.h:189
u32 size
Definition g3d_ResCommon.h:190
Definition g3d_ResCommon.h:33
char str[sizeof(LengthType)]
Definition g3d_ResCommon.h:36
LengthType len
Definition g3d_ResCommon.h:35
u32 LengthType
Definition g3d_ResCommon.h:34