-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArg.hpp
483 lines (409 loc) · 13.3 KB
/
Arg.hpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#ifndef ___FCF__PARALLEL__ARG_HPP___
#define ___FCF__PARALLEL__ARG_HPP___
#include "macro.hpp"
#define FCF_PARALLEL_INCLUDE_LIBRARY fcfBasis
#define FCF_PARALLEL_INCLUDE_FILE nativeType.hpp
#include "include.hpp"
#include <string>
#include <set>
#include <memory>
#include "Details/TupleCall.hpp"
#define FCF_PARALLLEL_MAX_TYPE_LENGTH 64
#define FCF_PARALLEL_MAX_TYPE_COMPATIBLE 9
namespace fcf {
namespace Parallel {
enum ParallelSplit{
PS_NONE,
PS_FULL,
PS_PACKAGE,
};
class BaseArg;
struct ArgUpload{
inline ArgUpload(bool a_value)
: value(a_value){}
inline void operator()(BaseArg* a_parg);
bool value;
};
struct ArgSplit{
inline ArgSplit(ParallelSplit a_value)
: value(a_value){}
inline void operator()(BaseArg* a_parg);
ParallelSplit value;
};
struct ArgLength{
inline ArgLength(size_t a_value)
: value(a_value){}
inline void operator()(BaseArg* a_parg);
size_t value;
};
struct ArgSplitSize{
inline ArgSplitSize(size_t a_value)
: value(a_value){}
inline void operator()(BaseArg* a_parg);
size_t value;
};
struct ArgContainer{
inline ArgContainer(size_t a_value)
: value(a_value){}
inline void operator()(BaseArg* a_parg);
size_t value;
};
template <typename Ty>
class Arg;
namespace Details {
template <typename Ty>
struct StaticCompatible {
StaticCompatible() {
const char* origArgs[FCF_PARALLEL_MAX_TYPE_COMPATIBLE];
Arg<Ty>().types(origArgs, typesc);
for(size_t i = 0; i < typesc; ++i) {
size_t index = i*2;
size_t l = strlen(origArgs[i]);
if (l + 1 >= FCF_PARALLLEL_MAX_TYPE_LENGTH - 1) {
throw std::runtime_error(std::string() + "The size of the type name may not exceed " + std::to_string(FCF_PARALLLEL_MAX_TYPE_LENGTH-1));
}
std::strcpy(types[index], origArgs[i]);
types[index][l] = '*';
types[index][l+1] = 0;
index += 1;
l += 6;
if (l + 1 >= FCF_PARALLLEL_MAX_TYPE_LENGTH - 1) {
throw std::runtime_error(std::string() + "The size of the type name may not exceed " + std::to_string(FCF_PARALLLEL_MAX_TYPE_LENGTH-1));
}
std::strcpy(types[index], "const ");
std::strcpy(&types[index][6], origArgs[i]);
types[index][l] = '*';
types[index][l+1] = 0;
}
typesc *= 2;
}
size_t typesc;
char types[FCF_PARALLEL_MAX_TYPE_COMPATIBLE][FCF_PARALLLEL_MAX_TYPE_LENGTH];
};
}
class FCF_PARALLEL_DECL_EXPORT BaseArg {
public:
BaseArg();
virtual ~BaseArg();
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) = 0;
void* pointer;
size_t size;
size_t itemSize;
size_t length;
bool container;
ParallelSplit split;
size_t splitSize;
bool upload;
};
template <typename Ty>
class Arg : public BaseArg {
public:
typedef Ty value_type;
typedef typename Ty::value_type argument_item_type;
typedef typename Ty::value_type* argument_type;
Arg()
: pvalue(0)
{
}
Arg(const Arg& a_arg)
: BaseArg(a_arg) {
if (a_arg.data.get() && a_arg.pvalue == &(*a_arg.data->begin()) ) {
data = a_arg.data;
pvalue = &(*data->begin());
} else {
pvalue = a_arg.pvalue;
}
pointer = &pvalue;
}
template <typename... TParamPack>
Arg(Ty& a_value, bool a_refMode, TParamPack... a_paramPack) {
container = true;
size = sizeof(void*);
itemSize = sizeof(argument_item_type);
length = a_value.size();
if (a_refMode) {
pvalue = &(*a_value.begin());
} else {
data.reset(new Ty(a_value));
pvalue = &(*data->begin());
}
pointer = &pvalue;
typedef std::tuple<TParamPack ...> tuple_param_type;
tuple_param_type tupleParams{a_paramPack...};
Details::tupleCall(tupleParams, this);
}
virtual void types(const char* a_args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
static Details::StaticCompatible<argument_item_type> staticCompatible;
a_dstArgsCount = staticCompatible.typesc;
for(size_t i = 0; i < a_dstArgsCount; ++i){
a_args[i] = staticCompatible.types[i];
}
}
argument_type pvalue;
std::shared_ptr<Ty> data;
};
template <typename Ty>
class Arg<Ty*> : public BaseArg {
public:
typedef Ty* value_type;
typedef Ty* argument_type;
Arg(){
}
Arg(const Arg& a_arg)
: BaseArg(a_arg){
container = true;
value = a_arg.value;
pointer = &value;
}
template <typename... TParamPack>
Arg(Ty* a_value, bool a_refMode, TParamPack... a_paramPack) {
value = a_value;
pointer = &value;
itemSize = sizeof(Ty);
size = sizeof(void*);
length = 0;
typedef std::tuple<TParamPack ...> tuple_param_type;
tuple_param_type tupleParams{a_paramPack...};
Details::tupleCall(tupleParams, this);
}
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
static Details::StaticCompatible<Ty> staticCompatible;
a_dstArgsCount = staticCompatible.typesc;
for(size_t i = 0; i < a_dstArgsCount; ++i){
args[i] = staticCompatible.types[i];
}
}
Ty* value;
};
template <typename Ty>
class Arg<const Ty*> : public BaseArg {
public:
typedef Ty* value_type;
typedef Ty* argument_type;
Arg(){
}
Arg(const Arg& a_arg)
: BaseArg(a_arg){
container = true;
value = a_arg.value;
pointer = &value;
}
template <typename... TParamPack>
Arg(const Ty* a_value, bool a_refMode, TParamPack... a_paramPack) {
value = a_value;
pointer = &value;
itemSize = sizeof(Ty);
size = sizeof(void*);
length = 0;
typedef std::tuple<TParamPack ...> tuple_param_type;
tuple_param_type tupleParams{a_paramPack...};
Details::tupleCall(tupleParams, this);
}
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
static Details::StaticCompatible<Ty> staticCompatible;
a_dstArgsCount = staticCompatible.typesc;
for(size_t i = 0; i < a_dstArgsCount; ++i){
args[i] = staticCompatible.types[i];
}
}
const Ty* value;
};
template <typename Ty>
class SimpleArg : public BaseArg {
public:
typedef Ty value_type;
typedef Ty argument_type;
SimpleArg(){
}
SimpleArg(const SimpleArg& a_arg)
: BaseArg(a_arg)
, value(a_arg.value){
if (&a_arg.value == a_arg.pointer){
pointer = &value;
}
}
template <typename... TParamPack>
SimpleArg(Ty& a_value, bool a_refMode, TParamPack... a_paramPack) {
size = sizeof(Ty);
itemSize = 0;
length = 0;
if (a_refMode) {
pointer = &a_value;
value = a_value;
} else {
value = a_value;
pointer = &value;
}
typedef std::tuple<TParamPack ...> tuple_param_type;
tuple_param_type tupleParams{a_paramPack...};
Details::tupleCall(tupleParams, this);
}
Ty value;
};
template <>
class Arg<bool> : public SimpleArg<bool> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 1;
args[0] = "bool";
}
};
template <>
class Arg<char> : public SimpleArg<char> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "char";
args[1] = "unsigned char";
}
};
template <>
class Arg<unsigned char> : public SimpleArg<unsigned char> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "unsigned char";
args[1] = "char";
}
};
template <>
class Arg<short> : public SimpleArg<short> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "short";
args[1] = "unsigned short";
}
};
template <>
class Arg<unsigned short> : public SimpleArg<unsigned short> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "unsigned short";
args[1] = "short";
}
};
template <>
class Arg<int> : public SimpleArg<int> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "int";
args[1] = "unsigned int";
}
};
template <>
class Arg<unsigned int> : public SimpleArg<unsigned int> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "unsigned int";
args[1] = "int";
}
};
template <>
class Arg<fcf_int64> : public SimpleArg<fcf_int64> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "long long";
args[1] = "unsigned long long";
}
};
template <>
class Arg<fcf_uint64> : public SimpleArg<fcf_uint64> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 2;
args[0] = "unsigned long long";
args[1] = "long long";
}
};
template <>
class Arg<float> : public SimpleArg<float> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 1;
args[0] = "float";
}
};
template <>
class Arg<double> : public SimpleArg<double> {
public:
using SimpleArg::SimpleArg;
virtual void types(const char* args[FCF_PARALLEL_MAX_TYPE_COMPATIBLE], size_t& a_dstArgsCount) {
a_dstArgsCount = 1;
args[0] = "double";
}
};
template <typename Ty>
struct ArgResolver{
typedef Ty type;
typedef Arg<Ty> arg_type;
static inline Arg<Ty> resolve(const Ty& a_value){
return Arg<Ty>((Ty&)a_value, false);
}
};
template <typename Ty>
struct ArgResolver< Arg<Ty> > {
typedef Ty type;
typedef Arg<Ty> arg_type;
static inline Arg<Ty> resolve(const Arg<Ty>& a_value){
return Arg<Ty>(a_value);
}
};
template <typename Ty, typename... TParamPack>
Arg<Ty> refArg(Ty& a_value, TParamPack... a_paramPack){
Arg<Ty> a(a_value, true, a_paramPack...);
return a;
}
template <typename Ty, typename... TParamPack>
Arg<Ty> valArg(const Ty& a_value, TParamPack... a_paramPack){
Arg<Ty> a(a_value, false, a_paramPack...);
return a;
}
#ifdef FCF_PARALLEL_IMPLEMENTATION
BaseArg::~BaseArg() {
}
#endif
#ifdef FCF_PARALLEL_IMPLEMENTATION
BaseArg::BaseArg()
: pointer(0)
, size(0)
, itemSize(0)
, length(0)
, container(false)
, split(PS_NONE)
, splitSize(1)
, upload(false) {
}
#endif
void ArgUpload::operator()(BaseArg* a_parg){
a_parg->upload = value;
}
void ArgSplit::operator()(BaseArg* a_parg){
a_parg->split = value;
}
void ArgLength::operator()(BaseArg* a_parg){
a_parg->length = value;
}
void ArgSplitSize::operator()(BaseArg* a_parg){
a_parg->splitSize = value;
}
void ArgContainer::operator()(BaseArg* a_parg){
a_parg->container = value;
}
} // namespace Parallel
} // namesapce fcf
#endif // #ifndef ___FCF__PARALLEL__ARG_HPP___