This repository was archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflecs_meta.c
3154 lines (2663 loc) · 81.4 KB
/
flecs_meta.c
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef FLECS_META_IMPL
#include "flecs_meta.h"
#endif
#ifndef FLECS_META_PARSER_H
#define FLECS_META_PARSER_H
#define ECS_META_IDENTIFIER_LENGTH (256)
#define ecs_meta_error(ctx, ptr, ...)\
ecs_parser_error((ctx)->name, (ctx)->decl, ptr - (ctx)->decl, __VA_ARGS__)
typedef char ecs_meta_token_t[ECS_META_IDENTIFIER_LENGTH];
typedef struct ecs_meta_parse_ctx_t {
const char *name;
const char *decl;
} ecs_meta_parse_ctx_t;
typedef struct ecs_meta_type_t {
ecs_meta_token_t type;
ecs_meta_token_t params;
bool is_const;
bool is_ptr;
} ecs_meta_type_t;
typedef struct ecs_meta_member_t {
ecs_meta_type_t type;
ecs_meta_token_t name;
int64_t count;
bool is_partial;
} ecs_meta_member_t;
typedef struct ecs_meta_constant_t {
ecs_meta_token_t name;
int64_t value;
bool is_value_set;
} ecs_meta_constant_t;
typedef struct ecs_meta_params_t {
ecs_meta_type_t key_type;
ecs_meta_type_t type;
int64_t count;
bool is_key_value;
bool is_fixed_size;
} ecs_meta_params_t;
const char* ecs_meta_parse_constant(
const char *ptr,
ecs_meta_constant_t *token_out,
ecs_meta_parse_ctx_t *ctx);
const char* ecs_meta_parse_member(
const char *ptr,
ecs_meta_member_t *token_out,
ecs_meta_parse_ctx_t *ctx);
void ecs_meta_parse_params(
const char *ptr,
ecs_meta_params_t *token_out,
ecs_meta_parse_ctx_t *ctx);
#endif
#ifndef FLECS_META_SERIALIZER_H
#define FLECS_META_SERIALIZER_H
void EcsAddStruct(
ecs_iter_t *it);
void EcsSetPrimitive(
ecs_iter_t *it);
void EcsSetEnum(
ecs_iter_t *it);
void EcsSetBitmask(
ecs_iter_t *it);
void EcsSetStruct(
ecs_iter_t *it);
void EcsSetArray(
ecs_iter_t *it);
void EcsSetVector(
ecs_iter_t *it);
void EcsSetMap(
ecs_iter_t *it);
#endif
static
ecs_vector_t* serialize_type(
ecs_world_t *world,
ecs_entity_t entity,
ecs_vector_t *ops,
int32_t offset);
static
ecs_size_t ecs_get_primitive_size(
ecs_primitive_kind_t kind)
{
switch(kind) {
case EcsBool: return sizeof(bool);
case EcsChar: return sizeof(char);
case EcsByte: return sizeof(char);
case EcsU8: return sizeof(uint8_t);
case EcsU16: return sizeof(uint16_t);
case EcsU32: return sizeof(uint32_t);
case EcsU64: return sizeof(uint64_t);
case EcsI8: return sizeof(int8_t);
case EcsI16: return sizeof(int16_t);
case EcsI32: return sizeof(int32_t);
case EcsI64: return sizeof(int64_t);
case EcsF32: return sizeof(float);
case EcsF64: return sizeof(double);
case EcsIPtr: return sizeof(intptr_t);
case EcsUPtr: return sizeof(uintptr_t);
case EcsString: return sizeof(char*);
case EcsEntity: return sizeof(ecs_entity_t);
default:
ecs_abort(ECS_INTERNAL_ERROR, NULL);
}
}
static
int16_t ecs_get_primitive_alignment(
ecs_primitive_kind_t kind)
{
switch(kind) {
case EcsBool: return ECS_ALIGNOF(bool);
case EcsChar: return ECS_ALIGNOF(char);
case EcsByte: return ECS_ALIGNOF(char);
case EcsU8: return ECS_ALIGNOF(uint8_t);
case EcsU16: return ECS_ALIGNOF(uint16_t);
case EcsU32: return ECS_ALIGNOF(uint32_t);
case EcsU64: return ECS_ALIGNOF(uint64_t);
case EcsI8: return ECS_ALIGNOF(int8_t);
case EcsI16: return ECS_ALIGNOF(int16_t);
case EcsI32: return ECS_ALIGNOF(int32_t);
case EcsI64: return ECS_ALIGNOF(int64_t);
case EcsF32: return ECS_ALIGNOF(float);
case EcsF64: return ECS_ALIGNOF(double);
case EcsIPtr: return ECS_ALIGNOF(intptr_t);
case EcsUPtr: return ECS_ALIGNOF(uintptr_t);
case EcsString: return ECS_ALIGNOF(char*);
case EcsEntity: return ECS_ALIGNOF(ecs_entity_t);
default:
ecs_abort(ECS_INTERNAL_ERROR, NULL);
}
}
static
ecs_vector_t* serialize_primitive(
ecs_world_t *world,
ecs_entity_t entity,
const EcsPrimitive *type,
ecs_vector_t *ops)
{
(void)world;
(void)entity;
ecs_type_op_t *op;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = ecs_get_primitive_size(type->kind),
.alignment = ecs_get_primitive_alignment(type->kind)
};
}
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpPrimitive,
.size = ecs_get_primitive_size(type->kind),
.alignment = ecs_get_primitive_alignment(type->kind),
.count = 1,
.is.primitive = type->kind
};
return ops;
}
static
ecs_vector_t* serialize_enum(
ecs_world_t *world,
ecs_entity_t entity,
const EcsEnum *type,
ecs_vector_t *ops)
{
(void)type;
ecs_type_op_t *op;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = sizeof(int32_t),
.alignment = ECS_ALIGNOF(int32_t)
};
}
op = ecs_vector_add(&ops, ecs_type_op_t);
ecs_ref_t ref = {0};
ecs_get_ref(world, &ref, entity, EcsEnum);
*op = (ecs_type_op_t) {
.kind = EcsOpEnum,
.size = sizeof(int32_t),
.alignment = ECS_ALIGNOF(int32_t),
.count = 1,
.is.constant = ref
};
return ops;
}
static
ecs_vector_t* serialize_bitmask(
ecs_world_t *world,
ecs_entity_t entity,
const EcsBitmask *type,
ecs_vector_t *ops)
{
(void)type;
ecs_type_op_t *op;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = sizeof(int32_t),
.alignment = ECS_ALIGNOF(int32_t)
};
}
op = ecs_vector_add(&ops, ecs_type_op_t);
ecs_ref_t ref = {0};
ecs_get_ref(world, &ref, entity, EcsBitmask);
*op = (ecs_type_op_t) {
.kind = EcsOpBitmask,
.size = sizeof(int32_t),
.alignment = ECS_ALIGNOF(int32_t),
.count = 1,
.is.constant = ref
};
return ops;
}
static
ecs_vector_t* serialize_struct(
ecs_world_t *world,
ecs_entity_t entity,
const EcsStruct *type,
ecs_vector_t *ops,
int32_t offset)
{
ecs_type_op_t *op_header = NULL;
if (!ops) {
op_header = ecs_vector_add(&ops, ecs_type_op_t);
}
int32_t push_op = ecs_vector_count(ops);
ecs_type_op_t *op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpPush
};
ecs_size_t size = 0;
int16_t alignment = 0;
EcsMember *members = ecs_vector_first(type->members, EcsMember);
int32_t i, count = ecs_vector_count(type->members);
for (i = 0; i < count; i ++) {
/* Add type operations of member to struct ops */
int32_t prev_count = ecs_vector_count(ops);
ops = serialize_type(world, members[i].type, ops, offset + size);
#ifndef NDEBUG
int32_t op_count = ecs_vector_count(ops);
/* At least one op should be added */
ecs_assert(prev_count != op_count, ECS_INTERNAL_ERROR, NULL);
ecs_assert(ops != NULL, ECS_INTERNAL_ERROR, NULL);
#endif
op = ecs_vector_get(ops, ecs_type_op_t, prev_count);
op->name = members[i].name;
const EcsMetaType *meta_type = ecs_get(world, members[i].type, EcsMetaType);
ecs_size_t member_size = meta_type->size * (meta_type->kind == EcsStructType ? 1 : op->count);
int16_t member_alignment = meta_type->alignment;
ecs_assert(member_size != 0, ECS_INTERNAL_ERROR, op->name);
ecs_assert(member_alignment != 0, ECS_INTERNAL_ERROR, op->name);
size = ECS_ALIGN(size, member_alignment);
op->offset = offset + size;
size += member_size;
if (member_alignment > alignment) {
alignment = member_alignment;
}
}
/* Align struct size to struct alignment */
size = ECS_ALIGN(size, alignment);
/* Size and alignment are ordinarily determined by ECS_STRUCT and should be
* the same as the values computed here. However, there are two exceptions.
* The first exception is when an application defines a type by populating
* the EcsStruct component directly and does not provide size and alignment
* values for EcsMetaType. The second scenario is when the type definition
* contains an ECS_PRIVATE, in which case the type may contain
* members that are not described.
*
* In the first case the computed values should be set in EcsMetaType. In the
* second case the values from EcsMetaType should be assigned to the type
* operation. */
bool is_added;
EcsMetaType *base_type = ecs_get_mut(world, entity, EcsMetaType, &is_added);
ecs_assert(base_type != NULL, ECS_INTERNAL_ERROR, NULL);
if (!is_added) {
if (!type->is_partial) {
/* EcsMetaType existed already, and this is not a partial type. This
* means that computed size and alignment should match exactly. */
if (base_type->size) {
ecs_assert(base_type->size == size, ECS_INTERNAL_ERROR, NULL);
}
if (base_type->alignment) {
ecs_assert(
base_type->alignment == alignment, ECS_INTERNAL_ERROR, NULL);
}
} else {
/* EcsMetaType exists, and this is a partial type. In this case the
* computed values only apply to the members described in EcsStruct
* but not to the type as a whole. Use the values from EcsMetaType. Note
* that it is not allowed to have a partial type for which no size
* and alignment are specified in EcsMetaType. */
ecs_assert(base_type->size != 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(base_type->alignment != 0, ECS_INVALID_PARAMETER, NULL);
size = base_type->size;
alignment = base_type->alignment;
}
} else {
/* If EcsMetaType was not set yet, initialize descriptor, alias to NULL
* since it won't be used here */
base_type->descriptor = NULL;
base_type->alias = NULL;
}
base_type->kind = EcsStructType;
base_type->size = size;
base_type->alignment = alignment;
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpPop,
};
if (op_header) {
op_header = ecs_vector_first(ops, ecs_type_op_t);
*op_header = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = size,
.alignment = alignment
};
}
ecs_type_op_t *op_push = ecs_vector_get(ops, ecs_type_op_t, push_op);
ecs_assert(op_push->kind == EcsOpPush, ECS_INTERNAL_ERROR, NULL);
op_push->size = size;
op_push->alignment = alignment;
op_push->count = count;
return ops;
}
static
ecs_vector_t* serialize_array(
ecs_world_t *world,
ecs_entity_t entity,
const EcsArray *type,
ecs_vector_t *ops)
{
(void)entity;
ecs_type_op_t *op_header = NULL;
if (!ops) {
op_header = ecs_vector_add(&ops, ecs_type_op_t);
}
const EcsMetaType *element_type = ecs_get(world, type->element_type, EcsMetaType);
ecs_assert(element_type != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_ref_t ref = {0};
ecs_get_ref(world, &ref, type->element_type, EcsMetaTypeSerializer);
ecs_type_op_t *op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t){
.kind = EcsOpArray,
.count = type->count,
.size = element_type->size,
.alignment = element_type->alignment,
.is.collection = ref
};
if (op_header) {
op_header = ecs_vector_first(ops, ecs_type_op_t);
*op_header = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = op->size,
.alignment = op->alignment
};
}
EcsMetaType *meta = ecs_get_mut(world, entity, EcsMetaType, NULL);
if(!meta->size || !meta->alignment) {
meta->size = type->count * element_type->size;
meta->alignment = element_type->alignment;
}
return ops;
}
static
ecs_vector_t* serialize_vector(
ecs_world_t *world,
ecs_entity_t entity,
const EcsVector *type,
ecs_vector_t *ops)
{
(void)entity;
ecs_type_op_t *op = NULL;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = sizeof(ecs_vector_t*),
.alignment = ECS_ALIGNOF(ecs_vector_t*)
};
}
const EcsMetaType *element_type = ecs_get(world, type->element_type, EcsMetaType);
ecs_assert(element_type != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_ref_t ref = {0};
ecs_get_ref(world, &ref, type->element_type, EcsMetaTypeSerializer);
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t){
.kind = EcsOpVector,
.count = 1,
.size = element_type->size,
.alignment = element_type->alignment,
.is.collection = ref
};
return ops;
}
static
ecs_vector_t* serialize_map(
ecs_world_t *world,
ecs_entity_t entity,
const EcsMap *type,
ecs_vector_t *ops)
{
ecs_type_op_t *op = NULL;
if (!ops) {
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t) {
.kind = EcsOpHeader,
.size = sizeof(ecs_map_t*),
.alignment = ECS_ALIGNOF(ecs_map_t*)
};
}
const EcsMetaTypeSerializer *key_cache = ecs_get(world, type->key_type, EcsMetaTypeSerializer);
ecs_assert(key_cache != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(key_cache->ops != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_assert(ecs_vector_count(key_cache->ops) != 0, ECS_INTERNAL_ERROR, NULL);
/* Make sure first op is the header */
ecs_type_op_t *key_op = ecs_vector_first(key_cache->ops, ecs_type_op_t);
ecs_assert(key_op->kind == EcsOpHeader, ECS_INTERNAL_ERROR, NULL);
if (ecs_vector_count(key_cache->ops) != 2) {
const EcsMetaType *ptr = ecs_get(world, entity, EcsMetaType);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_meta_parse_ctx_t ctx = {
.name = ecs_get_name(world, entity),
.decl = ptr->descriptor
};
ecs_meta_error( &ctx, ctx.decl,
"invalid key type '%s' for map", ecs_get_name(world, type->key_type));
}
key_op = ecs_vector_get(key_cache->ops, ecs_type_op_t, 1);
ecs_assert(key_op != NULL, ECS_INTERNAL_ERROR, NULL);
if (key_op->count != 1) {
const EcsMetaType *ptr = ecs_get(world, entity, EcsMetaType);
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_meta_parse_ctx_t ctx = {
.name = ecs_get_name(world, entity),
.decl = ptr->descriptor
};
ecs_meta_error( &ctx, ctx.decl, "array type invalid for key type");
}
ecs_ref_t key_ref = {0};
ecs_get_ref(world, &key_ref, type->key_type, EcsMetaTypeSerializer);
ecs_ref_t element_ref = {0};
ecs_get_ref(world, &element_ref, type->element_type, EcsMetaTypeSerializer);
op = ecs_vector_add(&ops, ecs_type_op_t);
*op = (ecs_type_op_t){
.kind = EcsOpMap,
.count = 1,
.size = sizeof(ecs_map_t*),
.alignment = ECS_ALIGNOF(ecs_map_t*),
.is.map = {
.key = key_ref,
.element = element_ref
}
};
return ops;
}
static
ecs_vector_t* serialize_type(
ecs_world_t *world,
ecs_entity_t entity,
ecs_vector_t *ops,
int32_t offset)
{
const EcsMetaType *type = ecs_get(world, entity, EcsMetaType);
ecs_assert(type != NULL, ECS_INVALID_PARAMETER, NULL);
switch(type->kind) {
case EcsPrimitiveType: {
const EcsPrimitive *t = ecs_get(world, entity, EcsPrimitive);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_primitive(world, entity, t, ops);
}
case EcsEnumType: {
const EcsEnum *t = ecs_get(world, entity, EcsEnum);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_enum(world, entity, t, ops);
}
case EcsBitmaskType: {
const EcsBitmask *t = ecs_get(world, entity, EcsBitmask);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_bitmask(world, entity, t, ops);
}
case EcsStructType: {
const EcsStruct *t = ecs_get(world, entity, EcsStruct);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_struct(world, entity, t, ops, offset);
}
case EcsArrayType: {
const EcsArray *t = ecs_get(world, entity, EcsArray);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_array(world, entity, t, ops);
}
case EcsVectorType: {
const EcsVector *t = ecs_get(world, entity, EcsVector);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_vector(world, entity, t, ops);
}
case EcsMapType: {
const EcsMap *t = ecs_get(world, entity, EcsMap);
ecs_assert(t != NULL, ECS_INTERNAL_ERROR, NULL);
return serialize_map(world, entity, t, ops);
}
default:
break;
}
return NULL;
}
void EcsSetPrimitive(ecs_iter_t *it) {
EcsPrimitive *type = ecs_term(it, EcsPrimitive, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
/* Size and alignment for primitive types can only be set after we know
* what kind of primitive type it is. Set values in case they haven't
* been set already */
bool is_added;
EcsMetaType *base_type = ecs_get_mut(world, e, EcsMetaType, &is_added);
ecs_assert(base_type != NULL, ECS_INTERNAL_ERROR, NULL);
base_type->size = ecs_get_primitive_size(type[i].kind);
base_type->alignment = ecs_get_primitive_alignment(type[i].kind);
ecs_set(world, e, EcsMetaTypeSerializer, {
serialize_primitive(
world, e, &type[i], NULL)
});
}
}
void EcsSetEnum(ecs_iter_t *it) {
EcsEnum *type = ecs_term(it, EcsEnum, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_enum(world, e, &type[i], NULL)
});
}
}
void EcsSetBitmask(ecs_iter_t *it) {
EcsBitmask *type = ecs_term(it, EcsBitmask, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_bitmask(world, e, &type[i], NULL)
});
}
}
void EcsSetStruct(ecs_iter_t *it) {
EcsStruct *type = ecs_term(it, EcsStruct, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_struct(world, e, &type[i], NULL, 0)
});
}
}
void EcsSetArray(ecs_iter_t *it) {
EcsArray *type = ecs_term(it, EcsArray, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_array(world, e, &type[i], NULL)
});
}
}
void EcsSetVector(ecs_iter_t *it) {
EcsVector *type = ecs_term(it, EcsVector, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_vector(world, e, &type[i], NULL)
});
}
}
void EcsSetMap(ecs_iter_t *it) {
EcsMap *type = ecs_term(it, EcsMap, 1);
ecs_world_t *world = it->world;
int i;
for (i = 0; i < it->count; i ++) {
ecs_entity_t e = it->entities[i];
ecs_set(it->world, e, EcsMetaTypeSerializer, {
serialize_map(world, e, &type[i], NULL)
});
}
}
char* ecs_chresc(
char *out,
char in,
char delimiter)
{
char *bptr = out;
switch(in) {
case '\a':
*bptr++ = '\\';
*bptr = 'a';
break;
case '\b':
*bptr++ = '\\';
*bptr = 'b';
break;
case '\f':
*bptr++ = '\\';
*bptr = 'f';
break;
case '\n':
*bptr++ = '\\';
*bptr = 'n';
break;
case '\r':
*bptr++ = '\\';
*bptr = 'r';
break;
case '\t':
*bptr++ = '\\';
*bptr = 't';
break;
case '\v':
*bptr++ = '\\';
*bptr = 'v';
break;
case '\\':
*bptr++ = '\\';
*bptr = '\\';
break;
default:
if (in == delimiter) {
*bptr++ = '\\';
*bptr = delimiter;
} else {
*bptr = in;
}
break;
}
*(++bptr) = '\0';
return bptr;
}
const char* ecs_chrparse(
const char *in,
char *out)
{
const char *result = in + 1;
char ch;
if (in[0] == '\\') {
result ++;
switch(in[1]) {
case 'a':
ch = '\a';
break;
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case 'v':
ch = '\v';
break;
case '\\':
ch = '\\';
break;
case '"':
ch = '"';
break;
case '0':
ch = '\0';
break;
case ' ':
ch = ' ';
break;
case '$':
ch = '$';
break;
default:
goto error;
}
} else {
ch = in[0];
}
if (out) {
*out = ch;
}
return result;
error:
return NULL;
}
ecs_size_t ecs_stresc(
char *out,
ecs_size_t n,
char delimiter,
const char *in)
{
const char *ptr = in;
char ch, *bptr = out, buff[3];
ecs_size_t written = 0;
while ((ch = *ptr++)) {
if ((written += (ecs_size_t)(ecs_chresc(buff, ch, delimiter) - buff)) <= n) {
*bptr++ = buff[0];
if ((ch = buff[1])) {
*bptr = ch;
bptr++;
}
}
}
if (bptr) {
while (written < n) {
*bptr = '\0';
bptr++;
written++;
}
}
return written;
}
/* Simple serializer to turn values into strings. Use this code as a template
* for when implementing a new serializer. */
static
int str_ser_type(
ecs_world_t *world,
ecs_vector_t *ser,
const void *base,
ecs_strbuf_t *str);
static
int str_ser_type_op(
ecs_world_t *world,
ecs_type_op_t *op,
const void *base,
ecs_strbuf_t *str);
/* Serialize a primitive value */
static
void str_ser_primitive(
ecs_world_t *world,
ecs_type_op_t *op,
const void *base,
ecs_strbuf_t *str)
{
const char *bool_str[] = { "false", "true" };
switch(op->is.primitive) {
case EcsBool:
ecs_strbuf_appendstr(str, bool_str[(int)*(bool*)base]);
break;
case EcsChar: {
char chbuf[3];
ecs_chresc(chbuf, *(char*)base, '\'');
ecs_strbuf_appendstrn(str, "'", 1);
ecs_strbuf_appendstr(str, chbuf);
ecs_strbuf_appendstrn(str, "'", 1);
break;
}
case EcsByte:
ecs_strbuf_append(str, "0x%x", *(uint8_t*)base);
break;
case EcsU8:
ecs_strbuf_append(str, "%u", *(uint8_t*)base);
break;
case EcsU16:
ecs_strbuf_append(str, "%u", *(uint16_t*)base);
break;
case EcsU32:
ecs_strbuf_append(str, "%u", *(uint32_t*)base);
break;
case EcsU64:
ecs_strbuf_append(str, "%llu", *(uint64_t*)base);
break;
case EcsI8:
ecs_strbuf_append(str, "%d", *(int8_t*)base);
break;
case EcsI16:
ecs_strbuf_append(str, "%d", *(int16_t*)base);
break;
case EcsI32:
ecs_strbuf_append(str, "%d", *(int32_t*)base);
break;
case EcsI64:
ecs_strbuf_append(str, "%lld", *(int64_t*)base);
break;
case EcsF32:
ecs_strbuf_append(str, "%f", (double)*(float*)base);
break;
case EcsF64:
ecs_strbuf_append(str, "%f", *(double*)base);
break;
case EcsIPtr:
ecs_strbuf_append(str, "%i", *(intptr_t*)base);
break;
case EcsUPtr:
ecs_strbuf_append(str, "%u", *(uintptr_t*)base);
break;
case EcsString: {
char *value = *(char**)base;
if (value) {
ecs_size_t length = ecs_stresc(NULL, 0, '"', value);
if (length == ecs_os_strlen(value)) {
ecs_strbuf_appendstrn(str, "\"", 1);
ecs_strbuf_appendstr(str, value);
ecs_strbuf_appendstrn(str, "\"", 1);
} else {
char *out = ecs_os_malloc(length + 3);
ecs_stresc(out + 1, length, '"', value);
out[0] = '"';
out[length + 1] = '"';
out[length + 2] = '\0';
ecs_strbuf_appendstr_zerocpy(str, out);
}
} else {
ecs_strbuf_appendstr(str, "nullptr");
}
break;
}
case EcsEntity: {
ecs_entity_t e = *(ecs_entity_t*)base;
const char *name;
if (e && (name = ecs_get_name(world, e))) {
ecs_strbuf_appendstr(str, name);
} else {
ecs_strbuf_append(str, "%u", e);
}
break;
}
}
}
/* Serialize enumeration */
static
int str_ser_enum(
ecs_world_t *world,
ecs_type_op_t *op,
const void *base,
ecs_strbuf_t *str)
{
const EcsEnum *enum_type = ecs_get_ref_w_id(world, &op->is.constant, 0, 0);
ecs_assert(enum_type != NULL, ECS_INVALID_PARAMETER, NULL);
int32_t value = *(int32_t*)base;
/* Enumeration constants are stored in a map that is keyed on the
* enumeration value. */
char **constant = ecs_map_get(enum_type->constants, char*, value);
if (!constant) {
return -1;
}