-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreamble.hpp
More file actions
436 lines (359 loc) · 9.45 KB
/
preamble.hpp
File metadata and controls
436 lines (359 loc) · 9.45 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/***************************************************************************
Copyright 2015 Ufora Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
namespace CPPML {
class Kinds {
public:
class tuple {};
class alternative {};
};
//our version of 'remove_reference'
template<typename _Tp>
struct remove_reference { typedef _Tp type; };
template<typename _Tp>
struct remove_reference<_Tp&> { typedef _Tp type; };
template<typename _Tp>
struct remove_reference<_Tp&&> { typedef _Tp type; };
//our version of 'remove_constness'
template<typename _Tp>
struct remove_constness { typedef _Tp type; };
template<typename _Tp>
struct remove_constness<const _Tp> { typedef _Tp type; };
//our version of forward, which we need because we don't have 'std' included here
template<class S>
S&& forward(typename ::CPPML::remove_reference<S>::type& a) /* noexcept - remove when gcc stops choking on it*/
{
return static_cast<S&&>(a);
}
//our version of move, which we need because we don't ahve 'std' here
template<class T>
typename ::CPPML::remove_reference<T>::type&& move(T&& a) /* noexcept - remove when gcc stops choking on it*/
{
typedef typename ::CPPML::remove_reference<T>::type&& RvalRef;
return static_cast<RvalRef>(a);
}
class Null {};
class CircularMemoError { };
template<class self_type, class default_t>
class Refcount {
public:
typedef unsigned long refcount_type;
static void increment(refcount_type& ioR)
{
ioR++;
};
static bool decrement(refcount_type& ioR)
{
if (ioR == 1)
{
ioR--;
return true;
}
else
{
ioR--;
return false;
}
}
};
template<class self_type, class default_t>
class Memovalid {
public:
typedef char memovalid_type;
static bool valid(memovalid_type& ioR)
{
return ioR == 1;
};
class lock_type {
public:
lock_type(memovalid_type& in) : m(in)
{
valid = false;
if (in == 2)
throw CircularMemoError();
in = 2;
valid = true;
};
~lock_type()
{
m = (valid ? 1 : 0);
};
bool needsCompute(void)
{
return true;
};
void invalidate(void)
{
valid = false;
};
memovalid_type& m;
bool valid;
};
};
template<class tagged_union_type, class common_type, class data_type, class default_t = void>
class TaggedUnion;
//Base class for tagged unions. Holds a refcount and some common data.
template<class tagged_union_type, class in_common_type, class default_t = void>
class TaggedUnionBase {
public:
typedef typename tagged_union_type::tag_type tag_type;
typedef in_common_type common_type;
TaggedUnionBase(tag_type inTag, const common_type& in) :
tag(inTag),
common(in),
refcount(1)
{
}
typedef typename Refcount<tagged_union_type, void>::refcount_type refcount_type;
refcount_type refcount;
common_type common;
tag_type tag;
void incrementRefcount(void)
{
Refcount<tagged_union_type, default_t>::increment(refcount);
}
bool decrementRefcount(void)
{
return Refcount<tagged_union_type, default_t>::decrement(refcount);
}
unsigned long getRefcount(void) const
{
return refcount;
}
};
//Layout type for a specific tagged union that holds 'data_type' as the specific data
template<class tagged_union_type, class in_common_type, class in_data_type, class default_t>
class TaggedUnion : public TaggedUnionBase<tagged_union_type, in_common_type, default_t> {
public:
typedef typename tagged_union_type::tag_type tag_type;
typedef in_data_type data_type;
typedef in_common_type common_type;
template<class arg_common_type, class arg_data_type>
TaggedUnion(tag_type tag, arg_common_type&& inCommon, arg_data_type&& inData) :
TaggedUnionBase<tagged_union_type, common_type, default_t>(
tag,
::CPPML::move(inCommon)
),
data(::CPPML::move(inData))
{
}
data_type data;
};
//how we hold references to cppml base classes
template<class cppml_type, class default_t>
class TaggedUnionReference {
public:
typedef typename cppml_type::common_data_type common_data_type;
typedef ::CPPML::TaggedUnionBase<cppml_type, common_data_type>
tagged_union_base_type;
typedef TaggedUnionReference<cppml_type, default_t> self_type;
TaggedUnionReference(tagged_union_base_type* inPointedTo) : mPointedTo(inPointedTo)
{
}
TaggedUnionReference() : mPointedTo()
{
}
bool valid(void)
{
return mPointedTo;
}
tagged_union_base_type* mPointedTo;
typename cppml_type::tag_type getTag() const
{
return mPointedTo->tag;
}
common_data_type& getCommonData() const
{
return mPointedTo->common;
}
template<class subtype>
void destroyAs(subtype* deliberatelyNullPtr)
{
delete static_cast<
::CPPML::TaggedUnion<cppml_type, common_data_type, subtype>*
>(mPointedTo);
mPointedTo = 0;
}
template<class subtype>
subtype& getData(subtype* deliberatelyNullPtr)
{
return static_cast<
::CPPML::TaggedUnion<cppml_type, common_data_type, subtype>*
>(mPointedTo)->data;
}
template<class subtype>
const subtype& getData(subtype* deliberatelyNullPtr) const
{
return static_cast<
const ::CPPML::TaggedUnion<cppml_type, common_data_type, subtype>*
>(mPointedTo)->data;
}
template<class in_subtype, class in_common_type>
static self_type create(typename cppml_type::tag_type tag, in_common_type&& inData, in_subtype&& inSubtype)
{
typedef typename remove_reference<in_subtype>::type real_subtype;
return self_type(
new TaggedUnion<cppml_type, common_data_type, real_subtype, default_t>(
tag,
::CPPML::forward<in_common_type>(inData),
::CPPML::forward<in_subtype>(inSubtype)
)
);
}
void incrementRefcount() const
{
mPointedTo->incrementRefcount();
}
bool decrementRefcount() const
{
if (mPointedTo)
return mPointedTo->decrementRefcount();
return false;
}
unsigned long getRefcount() const
{
if (mPointedTo)
return mPointedTo->getRefcount();
return 0;
}
void swap(self_type& other)
{
tagged_union_base_type* temp = other.mPointedTo;
other.mPointedTo = mPointedTo;
mPointedTo = temp;
}
};
template<class T, class default_t>
class Validator {
public:
void operator()(T& inElement) const
{
}
};
template<class T>
void validate(T& inElement)
{
Validator<T, void>()(inElement);
}
class MatchError {
};
template<class tagged_union_type, class default_t>
class MatchErrorFactory {
public:
typedef MatchError result_type;
static MatchError matchError(const tagged_union_type& in)
{
return MatchError();
}
};
template<class T>
typename MatchErrorFactory<T, void>::result_type
matchError(const T& in)
{
return MatchErrorFactory<T, void>::matchError(in);
}
class Void {};
class BadUnionAccess {};
template<class tagged_union_type, class default_t>
class ThrowBadUnionAccess {
public:
static void throwBadUnionAccess(const tagged_union_type& in) { throw BadUnionAccess(); }
};
template<class T> void throwBadUnionAccess(const T& in) { ThrowBadUnionAccess<T, void>::throwBadUnionAccess(in); }
template<class T>
void destroyInPlace(T* t)
{
t->~T();
}
template<class TLeft, class TRight>
class Chain {
public:
typedef TLeft left_type;
typedef TRight right_type;
};
template<class tuple_type_in, class member_type_in, class accessor_in, const int member_index_in>
class TupleMember {
public:
typedef tuple_type_in tuple_type;
typedef member_type_in member_type;
typedef accessor_in accessor;
static const int member_index(void)
{
return member_index_in;
}
};
template<class alternative_type_in, class member_type_in, class accessor_in, const int member_index_in>
class AlternativeCommonMember {
public:
typedef alternative_type_in alternative_type;
typedef member_type_in member_type;
typedef accessor_in accessor;
static const int member_index(void) { return member_index_in; }
};
template<class alternative_type_in, class data_type_in, class accessor_in>
class Alternative {
public:
typedef alternative_type_in alternative_type;
typedef data_type_in data_type;
typedef accessor_in accessor;
};
template<class T>
class Refholder {
public:
Refholder(const T& in) : m(in)
{
}
const T& m;
};
template<class T>
class Valueholder {
public:
Valueholder(const T& in) : m(in)
{
}
T m;
};
template<class T>
Refholder<T> grabMatchValue(const T& in)
{
return Refholder<T>(in);
}
template<class T>
Valueholder<T> grabMatchValue(T&& in)
{
return Valueholder<T>(in);
}
template<class T>
class CommonDataReplacer1 {
public:
CommonDataReplacer1(const typename T::member_0_type& in, T& inToInitialize) :
m(in),
toInitialize(inToInitialize)
{
}
template<class inner_tag_type>
void operator()(const inner_tag_type& tag) const
{
toInitialize = T(m, tag);
}
private:
typename T::member_0_type m;
T& toInitialize;
};
template<class T>
T replaceCommonData(const T& in, const typename T::member_0_type& s)
{
T toInitialize;
in.visit(CommonDataReplacer1<T>(s, toInitialize));
return toInitialize;
}
}