-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommon.h
More file actions
299 lines (251 loc) · 7.46 KB
/
Common.h
File metadata and controls
299 lines (251 loc) · 7.46 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#pragma once
#include <vector>
#include <map>
#include <assert.h>
#include "Location.h"
namespace LSystem
{
class LSystem;
typedef int Token;
typedef std::vector<Token> Tokens;
struct ArgumentType
{
typedef enum
{
Unspecified,
Int,
Float,
Bool,
Vec2,
Vec3,
Vec4,
MaxArgumentTypes,
} Type;
template <typename T> static Type GetType();
};
template <> inline ArgumentType::Type ArgumentType::GetType<bool>() { return Bool; }
template <> inline ArgumentType::Type ArgumentType::GetType<int>() { return Int; }
template <> inline ArgumentType::Type ArgumentType::GetType<float>() { return Float; }
template <> inline ArgumentType::Type ArgumentType::GetType<glm::vec2>() { return Vec2; }
template <> inline ArgumentType::Type ArgumentType::GetType<glm::vec3>() { return Vec3; }
template <> inline ArgumentType::Type ArgumentType::GetType<glm::vec4>() { return Vec4; }
typedef std::vector<ArgumentType::Type> ArgumentTypes;
// A formal argument: source code location + name + type
struct FormalArgument
{
FormalArgument(const Location& aLocation, Token aToken, ArgumentType::Type aType)
: mLocation(aLocation), mToken(aToken), mType(aType)
{}
Location mLocation;
Token mToken;
ArgumentType::Type mType;
};
typedef std::vector<FormalArgument> FormalArguments;
typedef std::map<Token, FormalArgument> FormalArgumentMap;
class CodeStorage
{
public:
template <typename T>
void Push(T aValue)
{
const int address = (int) mData.size();
mData.resize(address + sizeof(T));
reinterpret_cast<T&>(mData[address]) = aValue;
}
template <typename T>
const T& Get(int address) const
{
ASSERT(address <= (int) mData.size() - (int) sizeof(T));
ASSERT(address >= 0);
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
const T& operator[](int address) const
{
ASSERT(address < mData.size() - sizeof(T));
ASSERT(address >= 0);
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
T& operator[](int address)
{
ASSERT(address < mData.size() - sizeof(T));
ASSERT(address >= 0);
return reinterpret_cast<const T&>(mData[address]);
}
inline bool IsEmpty() const { return mData.empty(); }
inline int Size() const { return (int) mData.size(); }
private:
std::vector<unsigned char> mData;
};
class ArgumentValueList
{
public:
template <typename T>
void Push(T aValue)
{
const Entry e((int) mData.size(), ArgumentType::GetType<T>());
mData.resize(e.mAddress + sizeof(T));
reinterpret_cast<T&>(mData[e.mAddress]) = aValue;
mIndex.push_back(e);
}
template <typename T>
T Pop()
{
ASSERT(!mIndex.empty());
ASSERT(mIndex[mIndex.size() - 1].mType == ArgumentType::GetType<T>());
const int s = (int)(mData.size() - sizeof(T));
T r = reinterpret_cast<T&>(mData[s]);
mData.resize(s);
mIndex.pop_back();
return r;
}
template <typename T>
const T& Get(int index) const
{
ASSERT(index < (int) mIndex.size());
ASSERT(mIndex[index].mType == ArgumentType::GetType<T>());
const int address = mIndex[index].mAddress;
ASSERT(address <= (int) mData.size() - (int) sizeof(T));
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
const T& operator[](int index) const
{
ASSERT(index < mIndex.size());
ASSERT(mIndex[index].mType == ArgumentType::GetType<T>());
const int address = mIndex[index].mAddress;
ASSERT(address <= mData.size() - sizeof(T));
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
const T& Top() const
{
const int address = mData.size() - sizeof(T);
ASSERT(address >= 0);
ASSERT(mIndex[mIndex.size() - 1].mType == ArgumentType::GetType<T>());
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
T& Top()
{
const int address = (int)(mData.size() - sizeof(T));
ASSERT(address >= 0);
ASSERT(mIndex[mIndex.size() - 1].mType == ArgumentType::GetType<T>());
return reinterpret_cast<T&>(mData[address]);
}
template <typename T>
const T& Top(int index) const
{
ASSERT(index < 0);
const int address = (int)(mData.size() + index * sizeof(T));
ASSERT(address >= 0);
ASSERT(mIndex[mIndex.size() + index].mType == ArgumentType::GetType<T>());
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
T& Top(int index)
{
ASSERT(index < 0);
const int address = (int)(mData.size() + index * sizeof(T));
ASSERT(address >= 0);
ASSERT(mIndex[mIndex.size() + index].mType == ArgumentType::GetType<T>());
return reinterpret_cast<T&>(mData[address]);
}
template <typename T>
T& operator[](int index)
{
ASSERT(index < (int) mIndex.size());
const int address = mIndex[index].mAddress;
ASSERT(address <= (int) mData.size() - (int) sizeof(T));
return reinterpret_cast<T&>(mData[address]);
}
inline ArgumentType::Type GetType(int index) const
{
ASSERT(index < (int) mIndex.size());
return mIndex[index].mType;
}
inline bool IsEmpty() const { return mData.empty(); }
inline int ElementCount() const { return (int) mIndex.size(); }
private:
struct Entry
{
int mAddress;
ArgumentType::Type mType;
Entry(int aAddress, ArgumentType::Type aType) : mAddress(aAddress), mType(aType) {}
};
std::vector<unsigned char> mData;
std::vector<Entry> mIndex;
};
// A special storage for argument values: stores argument values in a tightly packed storage,
// regardless of the types of particular arguments
class ArgumentValueMap
{
public:
struct ArgumentDesc
{
int mAddress;
ArgumentType::Type mType;
};
typedef std::map<Token, ArgumentDesc>::const_iterator const_iterator;
typedef std::map<Token, ArgumentDesc>::iterator iterator;
ArgumentValueMap() : mSize(0) {}
iterator begin() { return mIndex.begin(); }
iterator end() { return mIndex.end(); }
const_iterator begin() const { return mIndex.begin(); }
const_iterator end() const { return mIndex.end(); }
template <typename T>
void Set(const Token aName, const T aValue)
{
IndexMap::const_iterator it = mIndex.find(aName);
assert(it == mIndex.end());
ArgumentDesc desc;
desc.mAddress = (int) mData.size();
desc.mType = ArgumentType::GetType<T>();
mData.resize(desc.mAddress + sizeof(T));
T& r = reinterpret_cast<T&>(mData[desc.mAddress]);
r = aValue;
++mSize;
mIndex.insert(std::make_pair(aName, desc));
}
template <typename T>
const T& Top() const
{
const int address = mData.size() - sizeof(T);
ASSERT(address >= 0);
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
T& Top()
{
const int address = mData.size() - sizeof(T);
ASSERT(address >= 0);
return reinterpret_cast<const T&>(mData[address]);
}
template <typename T>
const T& Get(Token aName) const
{
IndexMap::const_iterator it = mIndex.find(aName);
ASSERT(it != mIndex.end());
return reinterpret_cast<const T&>(mData[it->second.mAddress]);
}
template <typename T>
const T& Get(const ArgumentDesc& aArgument) const
{
ASSERT(aArgument.mAddress < mData.size());
return reinterpret_cast<const T&>(mData[aArgument.mAddress]);
}
ArgumentType::Type GetType(Token aName) const
{
IndexMap::const_iterator it = mIndex.find(aName);
ASSERT(it != mIndex.end());
return it->second.mType;
}
inline int Size() const { return mSize; }
private:
typedef std::map<Token, ArgumentDesc> IndexMap;
IndexMap mIndex;
std::vector<unsigned char> mData;
int mSize;
};
}