-
Notifications
You must be signed in to change notification settings - Fork 4
/
missile.hpp
597 lines (492 loc) · 14.7 KB
/
missile.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#ifndef INCLUDED_TRACK_ASSIGNMENTS_TEST_MISSILE_HPP
#define INCLUDED_TRACK_ASSIGNMENTS_TEST_MISSILE_HPP
#include <string>
/*[[[cog
import cogflect as cf
fields = [
["name", "type", "tags"],
["MISSILE_TYPE", "std::string", None ],
["LOCATION", "double[3]", None ],
["VELOCITY", "double[3]", None ],
["ACCELERATION", "double[3]", None ]
]
cf.Enum("missile", fields)
cf.CppClass("missile", fields)
]]]*/
#ifndef INCLUDED_COGFLECT_COMMON_COMMON_FUNCTIONS
#define INCLUDED_COGFLECT_COMMON_COMMON_FUNCTIONS
namespace cogflect {
// This seemingly useless template is a workaround
// for an oddity of C++ syntax. You can declare an
// array like this:
//
// double foo[3];
//
// But not like this:
//
// double[3] foo;
//
// Which means that some of the type information is
// on the left and some is on the right when declaring
// arrays. Cogflect lets you specify an element type of
// "double[3]", which is made possible by this template.
// You can use it to declare the array like this:
//
// type_passthrough<double[3]>::type foo;
//
// Thus keeping all the type information on the left
// side.
template<typename T>
struct type_passthrough {
typedef T type;
};
}
#endif
#ifndef INCLUDED_COGFLECT_ENUM_COMMON_FUNCTIONS
#define INCLUDED_COGFLECT_ENUM_COMMON_FUNCTIONS
namespace cogflect {
struct store_index_action
{
unsigned index;
// We don't waste an instruction initializing
// index because the type system guarantees
// this switch will always succeed.
inline store_index_action()
{}
template<class T>
inline void action()
{
index = T::index;
}
inline void default_action()
{}
};
struct store_string_action
{
const char* str;
template<class T>
inline void action()
{
str = T::string();
}
inline void default_action()
{}
};
struct true_t {};
struct false_t {};
}
#endif
namespace missile {
class type;
class data;
template<unsigned i>
struct info_index;
template<>
struct info_index<0>
{
static const unsigned index = 0;
static const int value = 0;
inline static const char* string() { return "missile::MISSILE_TYPE"; }
typedef std::string type;
typedef missile::type enum_type;
typedef missile::data data_type;
static const unsigned long long name_hash = 18317973204608468020u;
};
typedef info_index<0> MISSILE_TYPE_INFO;
template<>
struct info_index<1>
{
static const unsigned index = 1;
static const int value = 1;
inline static const char* string() { return "missile::LOCATION"; }
typedef cogflect::type_passthrough< double[3] >::type type;
typedef missile::type enum_type;
typedef missile::data data_type;
static const unsigned long long name_hash = 4666628800144805268u;
};
typedef info_index<1> LOCATION_INFO;
template<>
struct info_index<2>
{
static const unsigned index = 2;
static const int value = 2;
inline static const char* string() { return "missile::VELOCITY"; }
typedef cogflect::type_passthrough< double[3] >::type type;
typedef missile::type enum_type;
typedef missile::data data_type;
static const unsigned long long name_hash = 1507608000310853010u;
};
typedef info_index<2> VELOCITY_INFO;
template<>
struct info_index<3>
{
static const unsigned index = 3;
static const int value = 3;
inline static const char* string() { return "missile::ACCELERATION"; }
typedef cogflect::type_passthrough< double[3] >::type type;
typedef missile::type enum_type;
typedef missile::data data_type;
static const unsigned long long name_hash = 17348947259799395834u;
};
typedef info_index<3> ACCELERATION_INFO;
// This is a constant rather than a function so that it
// can be used as a template parameter. In C++0x we can change
// it to be a function using the 'constexpr' keyword.
static const unsigned size = 4;
class type
{
private:
int val_;
explicit inline type(int val) : val_(val) {}
public:
inline type(type const& other) : val_(other.val_) {}
inline type& operator=(type other) { val_ = other.val_; }
template<class Info>
static inline type make_from_info()
{
return type(Info::value);
}
template<class Action>
static inline void value_switcher(int value, Action& action)
{
switch(value)
{
case MISSILE_TYPE_INFO::value:
action.template action< MISSILE_TYPE_INFO >();
break;
case LOCATION_INFO::value:
action.template action< LOCATION_INFO >();
break;
case VELOCITY_INFO::value:
action.template action< VELOCITY_INFO >();
break;
case ACCELERATION_INFO::value:
action.template action< ACCELERATION_INFO >();
break;
default:
action.default_action();
break;
}
}
template<class Action>
static inline void index_switcher(unsigned index, Action& action)
{
switch(index)
{
case MISSILE_TYPE_INFO::index:
action.template action< MISSILE_TYPE_INFO >();
break;
case LOCATION_INFO::index:
action.template action< LOCATION_INFO >();
break;
case VELOCITY_INFO::index:
action.template action< VELOCITY_INFO >();
break;
case ACCELERATION_INFO::index:
action.template action< ACCELERATION_INFO >();
break;
default:
action.default_action();
break;
}
}
template<class Action>
inline void switcher(Action& action) const
{
value_switcher(val_, action);
}
inline int value() const
{
return val_;
}
inline unsigned index() const
{
cogflect::store_index_action sw;
switcher(sw);
return sw.index;
}
inline const char* string() const
{
cogflect::store_string_action sw;
switcher(sw);
return sw.str;
}
template<unsigned long long>
struct info_with_hash
{
typedef cogflect::false_t type;
};
// This is a constant rather than a function so that it
// can be used as a template parameter. In C++0x we can change
// it to be a function using the 'constexpr' keyword.
static const unsigned size = 4;
};
template<>
struct type::info_with_hash<18317973204608468020u>
{
typedef MISSILE_TYPE_INFO type;
};
template<>
struct type::info_with_hash<4666628800144805268u>
{
typedef LOCATION_INFO type;
};
template<>
struct type::info_with_hash<1507608000310853010u>
{
typedef VELOCITY_INFO type;
};
template<>
struct type::info_with_hash<17348947259799395834u>
{
typedef ACCELERATION_INFO type;
};
namespace {
const type MISSILE_TYPE(type::make_from_info<MISSILE_TYPE_INFO>());
const type LOCATION(type::make_from_info<LOCATION_INFO>());
const type VELOCITY(type::make_from_info<VELOCITY_INFO>());
const type ACCELERATION(type::make_from_info<ACCELERATION_INFO>());
}
}
#ifndef INCLUDED_COGFLECT_CPPCLASS_COMMON_FUNCTIONS
#define INCLUDED_COGFLECT_CPPCLASS_COMMON_FUNCTIONS
namespace cogflect {
template<typename Visitor, typename DataType>
struct pass_member_action
{
inline pass_member_action(Visitor& v, DataType& d)
: visitor_(v),
data_(d)
{}
template<typename MemberType>
inline void action()
{
visitor_.
template process_member<MemberType>(
data_.template get_member<MemberType>());
}
inline void default_action()
{
visitor_.unknown_member();
}
Visitor& visitor_;
DataType& data_;
};
/*
TODO: Pull this stuff out and give access to a
get_same_member<T> template that given an info template
from one class gets you the same named T in the other
class.
*/
template<class SourceMemberType, class TargetMemberType>
struct shape_assign_helper
{
inline static void assign(typename SourceMemberType::type& v,
typename TargetMemberType::data_type& d)
{
v = d.template get_member<TargetMemberType>();
}
};
template<class SourceMemberType>
struct shape_assign_helper<SourceMemberType, cogflect::false_t>
{
// no-op when target doesn't have the same member
// use ellipses since no sensible type for second argument
inline static void assign(...)
{
}
};
template<class Source, class Target>
struct shape_assign
{
inline shape_assign(Target& target)
: target_(target)
{}
template<class MemberType>
void process_member(typename MemberType::type& v) const
{
// Check if target type has a member with the same name by checking if
// it has a member with the same name_hash as the current member type being
// iterated.
typedef typename Target::enum_type target_enum_type;
typedef typename target_enum_type::template info_with_hash<MemberType::name_hash>::type
potential_type;
shape_assign_helper<MemberType, potential_type>::assign(v, target_);
}
Target& target_;
};
}
#endif
namespace missile {
class data
{
public:
typedef missile::type enum_type;
// TODO: iterate public/protected/private independently?
// TODO: iterate over members with a tag? <-- public/protected/private tags
// TODO: tag to indicate reflection is available
template<typename T>
typename T::type& get_member();
template<typename T>
typename T::type const& get_member() const;
template<unsigned index>
typename missile::info_index<index>::type& get_indexed_member()
{
return get_member< info_index<index> >();
}
template<unsigned index>
typename missile::info_index<index>::type const& get_indexed_member() const
{
return get_member< info_index<index> >();
}
template<typename Processor>
inline void get_runtime_member(Processor& p, type value)
{
cogflect::pass_member_action<Processor, data> tmp(p, *this);
value.switcher(tmp);
}
template<typename Processor>
inline void get_runtime_member(Processor const& p, type value)
{
cogflect::pass_member_action<const Processor, data> tmp(p, *this);
value.switcher(tmp);
}
template<typename Processor>
inline void get_runtime_member(Processor& p, type value) const
{
cogflect::pass_member_action<Processor, const data> tmp(p, *this);
value.switcher(tmp);
}
template<typename Processor>
inline void get_runtime_member(Processor const& p, type value) const
{
cogflect::pass_member_action<const Processor, const data> tmp(p, *this);
value.switcher(tmp);
}
template<typename Processor>
inline void get_runtime_indexed_member(Processor& p, unsigned index)
{
cogflect::pass_member_action<Processor, data> tmp(p, *this);
type::index_switcher(index, tmp);
}
template<typename Processor>
inline void get_runtime_indexed_member(Processor& p, unsigned index) const
{
cogflect::pass_member_action<Processor, const data> tmp(p, *this);
type::index_switcher(index, tmp);
}
template<typename Processor>
inline void get_runtime_indexed_member(Processor const& p, unsigned index)
{
cogflect::pass_member_action<const Processor, data> tmp(p, *this);
type::index_switcher(index, tmp);
}
template<typename Processor>
inline void get_runtime_indexed_member(Processor const& p, unsigned index) const
{
cogflect::pass_member_action<const Processor, const data> tmp(p, *this);
type::index_switcher(index, tmp);
}
template<class VisitorT>
inline void for_all_members(VisitorT& visitor)
{
visitor.
template process_member<MISSILE_TYPE_INFO>(missile_type_);
visitor.
template process_member<LOCATION_INFO>(location_);
visitor.
template process_member<VELOCITY_INFO>(velocity_);
visitor.
template process_member<ACCELERATION_INFO>(acceleration_);
}
template<class VisitorT>
inline void for_all_members(VisitorT& visitor) const
{
visitor.
template process_member<MISSILE_TYPE_INFO>(missile_type_);
visitor.
template process_member<LOCATION_INFO>(location_);
visitor.
template process_member<VELOCITY_INFO>(velocity_);
visitor.
template process_member<ACCELERATION_INFO>(acceleration_);
}
template<class VisitorT>
inline void for_all_members(VisitorT const& visitor)
{
visitor.
template process_member<MISSILE_TYPE_INFO>(missile_type_);
visitor.
template process_member<LOCATION_INFO>(location_);
visitor.
template process_member<VELOCITY_INFO>(velocity_);
visitor.
template process_member<ACCELERATION_INFO>(acceleration_);
}
template<class VisitorT>
inline void for_all_members(VisitorT const& visitor) const
{
visitor.
template process_member<MISSILE_TYPE_INFO>(missile_type_);
visitor.
template process_member<LOCATION_INFO>(location_);
visitor.
template process_member<VELOCITY_INFO>(velocity_);
visitor.
template process_member<ACCELERATION_INFO>(acceleration_);
}
template<class TargetType>
inline void shape_assign(TargetType const& other)
{
cogflect::shape_assign<data, TargetType> visitor;
for_all_members(visitor);
}
private:
std::string missile_type_;
cogflect::type_passthrough< double[3] >::type location_;
cogflect::type_passthrough< double[3] >::type velocity_;
cogflect::type_passthrough< double[3] >::type acceleration_;
}; // class data
template<>
inline MISSILE_TYPE_INFO::type& data::get_member<MISSILE_TYPE_INFO>()
{
return missile_type_;
}
template<>
inline LOCATION_INFO::type& data::get_member<LOCATION_INFO>()
{
return location_;
}
template<>
inline VELOCITY_INFO::type& data::get_member<VELOCITY_INFO>()
{
return velocity_;
}
template<>
inline ACCELERATION_INFO::type& data::get_member<ACCELERATION_INFO>()
{
return acceleration_;
}
template<>
inline MISSILE_TYPE_INFO::type const& data::get_member<MISSILE_TYPE_INFO>() const
{
return missile_type_;
}
template<>
inline LOCATION_INFO::type const& data::get_member<LOCATION_INFO>() const
{
return location_;
}
template<>
inline VELOCITY_INFO::type const& data::get_member<VELOCITY_INFO>() const
{
return velocity_;
}
template<>
inline ACCELERATION_INFO::type const& data::get_member<ACCELERATION_INFO>() const
{
return acceleration_;
}
} // namespace missile
//[[[end]]]
#endif