/
CSharpTemplates.h
411 lines (357 loc) · 9.48 KB
/
CSharpTemplates.h
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
#include "../Tests.h"
#include "AnotherUnit.h"
#include <string>
class DLL_API T1
{
public:
T1();
T1(int f);
~T1();
int getField() const;
private:
int field;
};
class DLL_API T2
{
};
template <typename T>
class DLL_API IndependentFields : public T1
{
public:
IndependentFields();
IndependentFields(const IndependentFields<T>& other);
IndependentFields(const T& t);
IndependentFields(int i);
~IndependentFields();
int getIndependent();
const T* returnTakeDependentPointer(const T* p);
T getDependent(const T& t);
static T staticDependent(const T& t);
template <typename AdditionalDependentType>
void usesAdditionalDependentType(AdditionalDependentType additionalDependentType);
static const int independentConst;
private:
int independent;
};
template <typename T>
const int IndependentFields<T>::independentConst = 15;
template <typename T>
IndependentFields<T>::IndependentFields() : independent(1)
{
}
template <typename T>
IndependentFields<T>::IndependentFields(const IndependentFields<T>& other)
{
independent = other.independent;
}
template <typename T>
IndependentFields<T>::IndependentFields(const T& t) : independent(1)
{
}
template <typename T>
IndependentFields<T>::IndependentFields(int i)
{
independent = i;
}
template <typename T>
IndependentFields<T>::~IndependentFields()
{
}
template <typename T>
const T* IndependentFields<T>::returnTakeDependentPointer(const T* p)
{
return p;
}
template <typename T>
T IndependentFields<T>::getDependent(const T& t)
{
return t;
}
template <typename T>
T IndependentFields<T>::staticDependent(const T& t)
{
return t;
}
template <typename T>
int IndependentFields<T>::getIndependent()
{
return independent;
}
template <typename X>
class DerivedChangesTypeName : public IndependentFields<X>
{
};
template <typename T>
class DLL_API DependentValueFieldForArray
{
private:
T field;
};
template <typename T>
class DLL_API DependentValueFields
{
public:
class Nested
{
};
DependentValueFields();
~DependentValueFields();
DependentValueFields& returnInjectedClass();
DependentValueFields returnValue();
T getDependentValue();
void setDependentValue(const T& value);
private:
T field;
union {
int unionField;
};
};
template <typename T>
DependentValueFields<T>::DependentValueFields()
{
}
template <typename T>
DependentValueFields<T>::~DependentValueFields()
{
}
template <typename T>
T DependentValueFields<T>::getDependentValue()
{
return field;
}
template <typename T>
void DependentValueFields<T>::setDependentValue(const T& value)
{
field = value;
}
template <typename T>
DependentValueFields<T>& DependentValueFields<T>::returnInjectedClass()
{
return *this;
}
template <typename T>
DependentValueFields<T> DependentValueFields<T>::returnValue()
{
return *this;
}
template <typename T>
class DLL_API DependentPointerFields
{
private:
T* field;
};
template <typename K, typename V>
class TwoTemplateArgs
{
private:
K key;
V value;
};
template <typename T, typename D = IndependentFields<T>>
class HasDefaultTemplateArgument
{
public:
HasDefaultTemplateArgument();
~HasDefaultTemplateArgument();
T property();
void setProperty(const T& t);
static T staticProperty();
static void setStaticProperty(const T& t);
bool operator==(const HasDefaultTemplateArgument& other);
private:
T field;
static T staticField;
};
template <>
class HasDefaultTemplateArgument<bool, bool>
{
public:
HasDefaultTemplateArgument();
~HasDefaultTemplateArgument();
bool property();
void setProperty(const bool& t);
static bool staticProperty();
static void setStaticProperty(const bool& t);
private:
bool field;
static bool staticField;
};
template <typename T>
class TemplateWithIndexer
{
public:
TemplateWithIndexer();
T& operator[](int i);
T& operator[](const char* string);
private:
T t[1];
};
template <typename T>
TemplateWithIndexer<T>::TemplateWithIndexer()
{
}
template <typename T>
T& TemplateWithIndexer<T>::operator[](int i)
{
return t[0];
}
template <typename T>
T& TemplateWithIndexer<T>::operator[](const char* string)
{
return t[0];
}
template <typename T, typename D>
HasDefaultTemplateArgument<T, D>::HasDefaultTemplateArgument()
{
}
template <typename T, typename D>
HasDefaultTemplateArgument<T, D>::~HasDefaultTemplateArgument()
{
}
template <typename T, typename D>
T HasDefaultTemplateArgument<T, D>::property()
{
return field;
}
template <typename T, typename D>
void HasDefaultTemplateArgument<T, D>::setProperty(const T& t)
{
field = t;
}
template <typename T, typename D>
T HasDefaultTemplateArgument<T, D>::staticProperty()
{
return staticField;
}
template <typename T, typename D>
void HasDefaultTemplateArgument<T, D>::setStaticProperty(const T& t)
{
staticField = t;
}
template <typename T, typename D>
bool HasDefaultTemplateArgument<T, D>::operator==(const HasDefaultTemplateArgument& other)
{
return field == other.field;
}
template <typename T, typename D>
T HasDefaultTemplateArgument<T, D>::staticField;
template <typename T>
class VirtualTemplate
{
public:
VirtualTemplate();
virtual ~VirtualTemplate();
virtual int function();
};
template <typename T>
VirtualTemplate<T>::VirtualTemplate()
{
}
template <typename T>
VirtualTemplate<T>::~VirtualTemplate()
{
}
template <typename T>
int VirtualTemplate<T>::function()
{
return 5;
}
class DLL_API HasVirtualTemplate
{
public:
HasVirtualTemplate();
~HasVirtualTemplate();
VirtualTemplate<int> getVCopy();
void setV(VirtualTemplate<int>* value);
int function();
private:
VirtualTemplate<int>* v;
HasDefaultTemplateArgument<bool, bool> explicitSpecialization;
};
template <typename T>
class TemplateInAnotherUnit;
class DLL_API TemplateSpecializer
{
public:
TemplateSpecializer();
template <typename T>
class NestedTemplate
{
};
IndependentFields<bool> getIndependentFields();
private:
IndependentFields<int> independentFields;
DependentValueFields<bool> dependentValueFields;
DependentPointerFields<int> dependentPointerFields;
HasDefaultTemplateArgument<int> hasDefaultTemplateArgument;
DependentValueFields<T1> dependentPointerFieldsT1;
DependentValueFields<T2> dependentPointerFieldsT2;
TemplateInAnotherUnit<float> templateInAnotherUnit;
DependentValueFields<IndependentFields<int>> specializeWithSpecialization;
DependentValueFields<IndependentFields<bool>> specializeWithSameSpecialization;
NestedTemplate<int> nestedTemplate;
DependentValueFields<DependentValueFields<int*>> nestedDependentPointer1;
DependentValueFields<DependentValueFields<char*>> nestedDependentPointer2;
DependentValueFieldForArray<char[3]> dependentFieldArray;
void completeSpecializationInParameter(DependentValueFields<float> p1,
DependentValueFields<int*> p2,
DependentValueFields<float*> p3);
void completeSpecializationInParameter(TwoTemplateArgs<int*, int*> p1,
TwoTemplateArgs<int*, int> p2,
TwoTemplateArgs<int*, float> p3);
};
template <typename Key, typename T>
class PartiallySpecialized
{
};
template <typename Key>
class PartiallySpecialized<Key, int>
{
union
{
int i;
float f;
};
};
template<class T>
class HasResultType {
typedef char Yes;
typedef void *No;
template<typename U> static Yes test(int, const typename U::result_type * = 0);
template<typename U> static No test(double);
public:
enum { Value = (sizeof(test<T>(0)) == sizeof(Yes)) };
};
template <typename Functor, bool foo = HasResultType<Functor>::Value>
struct LazyResultType { typedef typename Functor::result_type Type; };
template <typename Functor>
struct LazyResultType<Functor, false> { typedef void Type; };
template <class InputSequence, class MapFunctor>
struct MapResultType
{
typedef typename LazyResultType<MapFunctor>::Type ResultType;
};
template <template <typename> class InputSequence, typename MapFunctor, typename T>
struct MapResultType<InputSequence<T>, MapFunctor>
{
typedef InputSequence<typename LazyResultType<MapFunctor>::Type> ResultType;
};
// we optimise specialisations so that only actually used ones are wrapped
void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool> _2,
IndependentFields<T1> _3, IndependentFields<std::string> _4,
DependentValueFields<int> _5,
VirtualTemplate<int> _6, VirtualTemplate<bool> _7,
HasDefaultTemplateArgument<int, int> _8, DerivedChangesTypeName<T1> _9,
TemplateWithIndexer<int> _10, TemplateWithIndexer<T1> _11, std::string s);
// force the symbols for the template instantiations because we do not have the auto-compilation for the generated C++ source
template class DLL_API IndependentFields<int>;
template class DLL_API IndependentFields<bool>;
template class DLL_API IndependentFields<T1>;
template class DLL_API IndependentFields<std::string>;
template class DLL_API DependentValueFields<int>;
template class DLL_API VirtualTemplate<int>;
template class DLL_API VirtualTemplate<bool>;
template class DLL_API HasDefaultTemplateArgument<int, int>;
template class DLL_API DerivedChangesTypeName<T1>;
template class DLL_API TemplateWithIndexer<int>;
template class DLL_API TemplateWithIndexer<T1>;