forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flecs.h
18578 lines (15655 loc) · 551 KB
/
flecs.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
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
// Comment out this line when using as DLL
#define flecs_STATIC
/**
* @file flecs.h
* @brief Flecs public API.
*
* This file contains the public API for Flecs.
*/
#ifndef FLECS_H
#define FLECS_H
/**
* @defgroup options API toggles & constants
* @{
*/
/* Customizable precision for floating point operations (such as time ops) */
#ifndef FLECS_FLOAT
#define FLECS_FLOAT float
#endif
/* FLECS_LEGACY should be defined when building for C89 */
// #define FLECS_LEGACY
/* FLECS_NO_DEPRECATED_WARNINGS disables deprecated warnings */
#define FLECS_NO_DEPRECATED_WARNINGS
/* FLECS_SANITIZE enables expensive checks that can detect issues early */
#ifndef NDEBUG
#define FLECS_SANITIZE
#endif
/* FLECS_SOFT_ASSERT disables aborting for recoverable errors */
// #define FLECS_SOFT_ASSERT
/* FLECS_KEEP_ASSERT keeps asserts in release mode. */
// #define FLECS_KEEP_ASSERT
/* FLECS_CUSTOM_BUILD should be defined when manually selecting addons */
// #define FLECS_CUSTOM_BUILD
/* If this is a regular, non-custom build, include all addons. */
#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /* C API convenience macro's, always enabled */
#define FLECS_CPP /* C++ API */
#define FLECS_MODULE /* Module support */
#define FLECS_PARSER /* String parser for queries */
#define FLECS_PLECS /* ECS data definition format */
#define FLECS_RULES /* Constraint solver for advanced queries */
#define FLECS_SNAPSHOT /* Snapshot & restore ECS data */
#define FLECS_STATS /* Keep track of runtime statistics */
#define FLECS_SYSTEM /* System support */
#define FLECS_PIPELINE /* Pipeline support */
#define FLECS_TIMER /* Timer support */
#define FLECS_META /* Reflection support */
#define FLECS_META_C /* Utilities for populating reflection data */
#define FLECS_EXPR /* Parsing strings to/from component values */
#define FLECS_JSON /* Parsing JSON to/from component values */
#define FLECS_DOC /* Document entities & components */
#define FLECS_COREDOC /* Documentation for core entities & components */
#define FLECS_LOG /* When enabled ECS provides more detailed logs */
#define FLECS_APP /* Application addon */
#define FLECS_OS_API_IMPL /* Default implementation for OS API */
#define FLECS_HTTP /* Tiny HTTP server for connecting to remote UI */
#define FLECS_REST /* REST API for querying application data */
#endif // ifndef FLECS_CUSTOM_BUILD
/** @} */
/**
* @file api_defines.h
* @brief Supporting defines for the public API.
*
* This file contains constants / macro's that are typically not used by an
* application but support the public API, and therefore must be exposed. This
* header should not be included by itself.
*/
#ifndef FLECS_API_DEFINES_H
#define FLECS_API_DEFINES_H
/* Standard library dependencies */
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
/* Non-standard but required. If not provided by platform, add manually. */
#include <stdint.h>
/* Contains macro's for importing / exporting symbols */
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */
#ifndef FLECS_BAKE_CONFIG_H
#define FLECS_BAKE_CONFIG_H
/* Headers of public dependencies */
/* No dependencies */
/* Convenience macro for exporting symbols */
#ifndef flecs_STATIC
#if flecs_EXPORTS && (defined(_MSC_VER) || defined(__MINGW32__))
#define FLECS_API __declspec(dllexport)
#elif flecs_EXPORTS
#define FLECS_API __attribute__((__visibility__("default")))
#elif defined _MSC_VER
#define FLECS_API __declspec(dllimport)
#else
#define FLECS_API
#endif
#else
#define FLECS_API
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __BAKE_LEGACY__
#define FLECS_LEGACY
#endif
/* Some symbols are only exported when building in debug build, to enable
* whitebox testing of internal datastructures */
#ifndef NDEBUG
#define FLECS_DBG_API FLECS_API
#else
#define FLECS_DBG_API
#endif
////////////////////////////////////////////////////////////////////////////////
//// Language support defines
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY
#include <stdbool.h>
#endif
/* The API uses the native bool type in C++, or a custom one in C */
#if !defined(__cplusplus) && !defined(__bool_true_false_are_defined)
#undef bool
#undef true
#undef false
typedef char bool;
#define false 0
#define true !false
#endif
typedef uint32_t ecs_flags32_t;
typedef uint64_t ecs_flags64_t;
/* Keep unsigned integers out of the codebase as they do more harm than good */
typedef int32_t ecs_size_t;
#define ECS_SIZEOF(T) ECS_CAST(ecs_size_t, sizeof(T))
/* Use alignof in C++, or a trick in C. */
#ifdef __cplusplus
#define ECS_ALIGNOF(T) static_cast<int64_t>(alignof(T))
#elif defined(_MSC_VER)
#define ECS_ALIGNOF(T) (int64_t)__alignof(T)
#elif defined(__GNUC__)
#define ECS_ALIGNOF(T) (int64_t)__alignof__(T)
#else
#define ECS_ALIGNOF(T) ((int64_t)&((struct { char c; T d; } *)0)->d)
#endif
#if defined(__GNUC__)
#define ECS_UNUSED __attribute__((unused))
#else
#define ECS_UNUSED
#endif
#ifndef FLECS_NO_DEPRECATED_WARNINGS
#if defined(__GNUC__)
#define ECS_DEPRECATED(msg) __attribute__((deprecated(msg)))
#elif defined(_MSC_VER)
#define ECS_DEPRECATED(msg) __declspec(deprecated(msg))
#else
#define ECS_DEPRECATED(msg)
#endif
#else
#define ECS_DEPRECATED(msg)
#endif
#define ECS_ALIGN(size, alignment) (ecs_size_t)((((((size_t)size) - 1) / ((size_t)alignment)) + 1) * ((size_t)alignment))
/* Simple utility for determining the max of two values */
#define ECS_MAX(a, b) ((a > b) ? a : b)
/* Abstraction on top of C-style casts so that C functions can be used in C++
* code without producing warnings */
#ifndef __cplusplus
#define ECS_CAST(T, V) ((T)(V))
#else
#define ECS_CAST(T, V) (static_cast<T>(V))
#endif
////////////////////////////////////////////////////////////////////////////////
//// Entity id macro's
////////////////////////////////////////////////////////////////////////////////
#define ECS_ROLE_MASK (0xFFull << 56)
#define ECS_ENTITY_MASK (0xFFFFFFFFull)
#define ECS_GENERATION_MASK (0xFFFFull << 32)
#define ECS_GENERATION(e) ((e & ECS_GENERATION_MASK) >> 32)
#define ECS_GENERATION_INC(e) ((e & ~ECS_GENERATION_MASK) | ((0xFFFF & (ECS_GENERATION(e) + 1)) << 32))
#define ECS_COMPONENT_MASK (~ECS_ROLE_MASK)
#define ECS_HAS_ROLE(e, role) ((e & ECS_ROLE_MASK) == ECS_##role)
#define ECS_PAIR_RELATION(e) (ecs_entity_t_hi(e & ECS_COMPONENT_MASK))
#define ECS_PAIR_OBJECT(e) (ecs_entity_t_lo(e))
#define ECS_HAS_RELATION(e, rel) (ECS_HAS_ROLE(e, PAIR) && (ECS_PAIR_RELATION(e) == rel))
#define ECS_HAS_PAIR_OBJECT(e, rel, obj)\
(ECS_HAS_RELATION(e, rel) && ECS_PAIR_OBJECT(e) == obj)
#define ECS_HAS(id, has_id)(\
(id == has_id) ||\
(ECS_HAS_PAIR_OBJECT(id, ECS_PAIR_RELATION(has_id), ECS_PAIR_OBJECT(has_id))))
////////////////////////////////////////////////////////////////////////////////
//// Convert between C typenames and variables
////////////////////////////////////////////////////////////////////////////////
/** Translate C type to id. */
#define ecs_id(T) FLECS__E##T
/** Translate C type to system function. */
#define ecs_iter_action(T) FLECS__F##T
////////////////////////////////////////////////////////////////////////////////
//// Utilities for working with pair identifiers
////////////////////////////////////////////////////////////////////////////////
#define ecs_entity_t_lo(value) ECS_CAST(uint32_t, value)
#define ecs_entity_t_hi(value) ECS_CAST(uint32_t, (value) >> 32)
#define ecs_entity_t_comb(lo, hi) ((ECS_CAST(uint64_t, hi) << 32) + ECS_CAST(uint32_t, lo))
#define ecs_pair(pred, obj) (ECS_PAIR | ecs_entity_t_comb(obj, pred))
#define ecs_case(pred, obj) (ECS_CASE | ecs_entity_t_comb(obj, pred))
/* Get object from pair with the correct (current) generation count */
#define ecs_pair_relation(world, pair) ecs_get_alive(world, ECS_PAIR_RELATION(pair))
#define ecs_pair_object(world, pair) ecs_get_alive(world, ECS_PAIR_OBJECT(pair))
////////////////////////////////////////////////////////////////////////////////
//// Convenience macro's for ctor, dtor, move and copy
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY
/* Constructor / destructor convenience macro */
#define ECS_XTOR_IMPL(type, postfix, var, ...)\
void type##_##postfix(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *entity_ptr,\
void *_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)entity_ptr;\
(void)_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t entity = entity_ptr[i];\
type *var = &((type*)_ptr)[i];\
(void)entity;\
(void)var;\
__VA_ARGS__\
}\
}
/* Copy convenience macro */
#define ECS_COPY_IMPL(type, dst_var, src_var, ...)\
void type##_##copy(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *dst_entities,\
const ecs_entity_t *src_entities,\
void *_dst_ptr,\
const void *_src_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)dst_entities;\
(void)src_entities;\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t dst_entity = dst_entities[i];\
ecs_entity_t src_entity = src_entities[i];\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_entity;\
(void)src_entity;\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
/* Move convenience macro */
#define ECS_MOVE_IMPL(type, dst_var, src_var, ...)\
void type##_##move(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *dst_entities,\
const ecs_entity_t *src_entities,\
void *_dst_ptr,\
void *_src_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)dst_entities;\
(void)src_entities;\
(void)_dst_ptr;\
(void)_src_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t dst_entity = dst_entities[i];\
ecs_entity_t src_entity = src_entities[i];\
type *dst_var = &((type*)_dst_ptr)[i];\
type *src_var = &((type*)_src_ptr)[i];\
(void)dst_entity;\
(void)src_entity;\
(void)dst_var;\
(void)src_var;\
__VA_ARGS__\
}\
}
/* Constructor / destructor convenience macro */
#define ECS_ON_SET_IMPL(type, var, ...)\
void type##_##on_set(\
ecs_world_t *world,\
ecs_entity_t component,\
const ecs_entity_t *entity_ptr,\
void *_ptr,\
size_t _size,\
int32_t _count,\
void *ctx)\
{\
(void)world;\
(void)component;\
(void)entity_ptr;\
(void)_ptr;\
(void)_size;\
(void)_count;\
(void)ctx;\
for (int32_t i = 0; i < _count; i ++) {\
ecs_entity_t entity = entity_ptr[i];\
type *var = &((type*)_ptr)[i];\
(void)entity;\
(void)var;\
__VA_ARGS__\
}\
}
#endif
////////////////////////////////////////////////////////////////////////////////
//// Deprecated constants
////////////////////////////////////////////////////////////////////////////////
/* These constants should no longer be used, but are required by the core to
* guarantee backwards compatibility */
#define ECS_AND (ECS_ROLE | (0x79ull << 56))
#define ECS_OR (ECS_ROLE | (0x78ull << 56))
#define ECS_XOR (ECS_ROLE | (0x77ull << 56))
#define ECS_NOT (ECS_ROLE | (0x76ull << 56))
#ifdef __cplusplus
}
#endif
#endif
/**
* @file log.h
* @brief Logging addon.
*
* The logging addon provides an API for (debug) tracing and reporting errors
* at various levels. When enabled, the logging addon can provide more detailed
* information about the state of the ECS and any errors that may occur.
*
* The logging addon can be disabled to reduce footprint of the library, but
* limits information logged to only file, line and error code.
*
* When enabled the logging addon can be configured to exclude levels of tracing
* from the build to reduce the impact on performance. By default all debug
* tracing is enabled for debug builds, tracing is enabled at release builds.
*
* Applications can change the logging level at runtime with ecs_log_set_level,
* but what is actually logged depends on what is compiled (when compiled
* without debug tracing, setting the runtime level to debug won't have an
* effect).
*
* The logging addon uses the OS API log_ function for all tracing.
*
* Note that even when the logging addon is not enabled, its header/source must
* be included in a build. To prevent unused variable warnings in the code, some
* API functions are included when the addon is disabled, but have empty bodies.
*/
#ifndef FLECS_LOG_H
#define FLECS_LOG_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef FLECS_LOG
////////////////////////////////////////////////////////////////////////////////
//// Tracing
////////////////////////////////////////////////////////////////////////////////
FLECS_API
void _ecs_deprecated(
const char *file,
int32_t line,
const char *msg);
FLECS_API
void ecs_log_push(void);
FLECS_API
void ecs_log_pop(void);
////////////////////////////////////////////////////////////////////////////////
//// Error reporting
////////////////////////////////////////////////////////////////////////////////
/** Get description for error code */
FLECS_API
const char* ecs_strerror(
int32_t error_code);
#else // FLECS_LOG
////////////////////////////////////////////////////////////////////////////////
//// Dummy macro's for when logging is disabled
////////////////////////////////////////////////////////////////////////////////
#define _ecs_deprecated(file, line, msg)\
(void)file;\
(void)line;\
(void)msg
#define ecs_log_push()
#define ecs_log_pop()
#define ecs_strerror(error_code)\
(void)error_code
#endif // FLECS_LOG
////////////////////////////////////////////////////////////////////////////////
//// Logging functions (do nothing when logging is enabled)
////////////////////////////////////////////////////////////////////////////////
FLECS_API
void _ecs_log(
int32_t level,
const char *file,
int32_t line,
const char *fmt,
...);
FLECS_API
void _ecs_logv(
int level,
const char *file,
int32_t line,
const char *fmt,
va_list args);
FLECS_API
void _ecs_abort(
int32_t error_code,
const char *file,
int32_t line,
const char *fmt,
...);
FLECS_API
bool _ecs_assert(
bool condition,
int32_t error_code,
const char *condition_str,
const char *file,
int32_t line,
const char *fmt,
...);
FLECS_API
void _ecs_parser_error(
const char *name,
const char *expr,
int64_t column,
const char *fmt,
...);
FLECS_API
void _ecs_parser_errorv(
const char *name,
const char *expr,
int64_t column,
const char *fmt,
va_list args);
////////////////////////////////////////////////////////////////////////////////
//// Logging Macro's
////////////////////////////////////////////////////////////////////////////////
#ifndef FLECS_LEGACY /* C89 doesn't support variadic macro's */
/* Base logging function. Accepts a custom level */
#define ecs_log(level, ...)\
_ecs_log(level, __FILE__, __LINE__, __VA_ARGS__)
#define ecs_logv(level, fmt, args)\
_ecs_logv(level, __FILE__, __LINE__, fmt, args)
/* Tracing. Used for logging of infrequent events */
#define _ecs_trace(file, line, ...) _ecs_log(0, file, line, __VA_ARGS__)
#define ecs_trace(...) _ecs_trace(__FILE__, __LINE__, __VA_ARGS__)
/* Warning. Used when an issue occurs, but operation is successful */
#define _ecs_warn(file, line, ...) _ecs_log(-2, file, line, __VA_ARGS__)
#define ecs_warn(...) _ecs_warn(__FILE__, __LINE__, __VA_ARGS__)
/* Error. Used when an issue occurs, and operation failed. */
#define _ecs_err(file, line, ...) _ecs_log(-3, file, line, __VA_ARGS__)
#define ecs_err(...) _ecs_err(__FILE__, __LINE__, __VA_ARGS__)
/* Fatal. Used when an issue occurs, and the application cannot continue. */
#define _ecs_fatal(file, line, ...) _ecs_log(-3, file, line, __VA_ARGS__)
#define ecs_fatal(...) _ecs_fatal(__FILE__, __LINE__, __VA_ARGS__)
/* Optionally include warnings about using deprecated features */
#ifndef FLECS_NO_DEPRECATED_WARNINGS
#define ecs_deprecated(...)\
_ecs_deprecated(__FILE__, __LINE__, __VA_ARGS__)
#else
#define ecs_deprecated(...)
#endif // FLECS_NO_DEPRECATED_WARNINGS
/* If no tracing verbosity is defined, pick default based on build config */
#if !(defined(ECS_TRACE_0) || defined(ECS_TRACE_1) || defined(ECS_TRACE_2) || defined(ECS_TRACE_3))
#if !defined(NDEBUG)
#define ECS_TRACE_3 /* Enable all tracing in debug mode. May slow things down */
#else
#define ECS_TRACE_0 /* Only enable infrequent tracing in release mode */
#endif // !defined(NDEBUG)
#endif // !(defined(ECS_TRACE_0) || defined(ECS_TRACE_1) || defined(ECS_TRACE_2) || defined(ECS_TRACE_3))
/* Define/undefine macro's based on compiled-in tracing level. This can optimize
* out tracing statements from a build, which improves performance. */
#if defined(ECS_TRACE_3) /* All debug tracing enabled */
#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
#define ecs_dbg_3(...) ecs_log(3, __VA_ARGS__);
#elif defined(ECS_TRACE_2) /* Level 2 and below debug tracing enabled */
#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
#define ecs_dbg_2(...) ecs_log(2, __VA_ARGS__);
#define ecs_dbg_3(...)
#elif defined(ECS_TRACE_1) /* Level 1 debug tracing enabled */
#define ecs_dbg_1(...) ecs_log(1, __VA_ARGS__);
#define ecs_dbg_2(...)
#define ecs_dbg_3(...)
#elif defined(ECS_TRACE_0) /* No debug tracing enabled */
#define ecs_dbg_1(...)
#define ecs_dbg_2(...)
#define ecs_dbg_3(...)
#else /* No tracing enabled */
#undef ecs_trace
#define ecs_trace(...)
#define ecs_dbg_1(...)
#define ecs_dbg_2(...)
#define ecs_dbg_3(...)
#endif // defined(ECS_TRACE_3)
#define ecs_dbg ecs_dbg_1 /* Default debug tracing is at level 1 */
/** Abort
* Unconditionally aborts process. */
#define ecs_abort(error_code, ...)\
_ecs_abort(error_code, __FILE__, __LINE__, __VA_ARGS__);\
ecs_os_abort(); abort(); /* satisfy compiler/static analyzers */
/** Assert
* Aborts if condition is false, disabled in debug mode. */
#if defined(NDEBUG) && !defined(FLECS_KEEP_ASSERT)
#define ecs_assert(condition, error_code, ...)
#else
#define ecs_assert(condition, error_code, ...)\
if (!_ecs_assert(condition, error_code, #condition, __FILE__, __LINE__, __VA_ARGS__)) {\
ecs_os_abort();\
}\
assert(condition) /* satisfy compiler/static analyzers */
#endif // NDEBUG
/** Debug assert
* Assert that is only valid in debug mode (ignores FLECS_KEEP_ASSERT) */
#ifndef NDEBUG
#define ecs_dbg_assert(condition, error_code, ...) ecs_assert(condition, error_code, __VA_ARGS__)
#else
#define ecs_dbg_assert(condition, error_code, ...)
#endif
/* Silence dead code/unused label warnings when compiling without checks. */
#define ecs_dummy_check\
if ((false)) {\
goto error;\
}
/** Check
* goto error if condition is false. */
#if defined(NDEBUG) && !defined(FLECS_KEEP_ASSERT)
#define ecs_check(condition, error_code, ...) ecs_dummy_check
#else
#ifdef FLECS_SOFT_ASSERT
#define ecs_check(condition, error_code, ...)\
if (!_ecs_assert(condition, error_code, #condition, __FILE__, __LINE__, __VA_ARGS__)) {\
goto error;\
}
#else // FLECS_SOFT_ASSERT
#define ecs_check(condition, error_code, ...)\
ecs_assert(condition, error_code, __VA_ARGS__);\
ecs_dummy_check
#endif
#endif // NDEBUG
/** Throw
* goto error when FLECS_SOFT_ASSERT is defined, otherwise abort */
#if defined(NDEBUG) && !defined(FLECS_KEEP_ASSERT)
#define ecs_throw(error_code, ...) ecs_dummy_check
#else
#ifdef FLECS_SOFT_ASSERT
#define ecs_throw(error_code, ...)\
_ecs_abort(error_code, __FILE__, __LINE__, __VA_ARGS__);\
goto error;
#else
#define ecs_throw(error_code, ...)\
ecs_abort(error_code, __VA_ARGS__);\
ecs_dummy_check
#endif
#endif // NDEBUG
/** Parser error */
#define ecs_parser_error(name, expr, column, ...)\
_ecs_parser_error(name, expr, column, __VA_ARGS__)
#define ecs_parser_errorv(name, expr, column, fmt, args)\
_ecs_parser_errorv(name, expr, column, fmt, args)
#endif // FLECS_LEGACY
////////////////////////////////////////////////////////////////////////////////
//// Functions that are always available
////////////////////////////////////////////////////////////////////////////////
/** Enable or disable tracing.
* This will enable builtin tracing. For tracing to work, it will have to be
* compiled in which requires defining one of the following macro's:
*
* ECS_TRACE_0 - All tracing is disabled
* ECS_TRACE_1 - Enable tracing level 1
* ECS_TRACE_2 - Enable tracing level 2 and below
* ECS_TRACE_3 - Enable tracing level 3 and below
*
* If no tracing level is defined and this is a debug build, ECS_TRACE_3 will
* have been automatically defined.
*
* The provided level corresponds with the tracing level. If -1 is provided as
* value, warnings are disabled. If -2 is provided, errors are disabled as well.
*
* @param level Desired tracing level.
* @return Previous tracing level.
*/
FLECS_API
int ecs_log_set_level(
int level);
/** Enable/disable tracing with colors.
* By default colors are enabled.
*
* @param enabled Whether to enable tracing with colors.
* @return Previous color setting.
*/
FLECS_API
bool ecs_log_enable_colors(
bool enabled);
/** Get last logged error code.
* Calling this operation resets the error code.
*
* @return Last error, 0 if none was logged since last call to last_error.
*/
FLECS_API
int ecs_log_last_error(void);
////////////////////////////////////////////////////////////////////////////////
//// Error codes
////////////////////////////////////////////////////////////////////////////////
#define ECS_INVALID_OPERATION (1)
#define ECS_INVALID_PARAMETER (2)
#define ECS_CONSTRAINT_VIOLATED (3)
#define ECS_OUT_OF_MEMORY (4)
#define ECS_OUT_OF_RANGE (5)
#define ECS_UNSUPPORTED (6)
#define ECS_INTERNAL_ERROR (7)
#define ECS_ALREADY_DEFINED (8)
#define ECS_MISSING_OS_API (9)
#define ECS_OPERATION_FAILED (10)
#define ECS_INVALID_CONVERSION (11)
#define ECS_INCONSISTENT_NAME (20)
#define ECS_NAME_IN_USE (21)
#define ECS_NOT_A_COMPONENT (22)
#define ECS_INVALID_COMPONENT_SIZE (23)
#define ECS_INVALID_COMPONENT_ALIGNMENT (24)
#define ECS_COMPONENT_NOT_REGISTERED (25)
#define ECS_INCONSISTENT_COMPONENT_ID (26)
#define ECS_INCONSISTENT_COMPONENT_ACTION (27)
#define ECS_MODULE_UNDEFINED (28)
#define ECS_COLUMN_ACCESS_VIOLATION (40)
#define ECS_COLUMN_INDEX_OUT_OF_RANGE (41)
#define ECS_COLUMN_IS_NOT_SHARED (42)
#define ECS_COLUMN_IS_SHARED (43)
#define ECS_COLUMN_TYPE_MISMATCH (45)
#define ECS_TYPE_INVALID_CASE (62)
#define ECS_INVALID_WHILE_ITERATING (70)
#define ECS_LOCKED_STORAGE (71)
#define ECS_INVALID_FROM_WORKER (72)
////////////////////////////////////////////////////////////////////////////////
//// Used when logging with colors is enabled
////////////////////////////////////////////////////////////////////////////////
#define ECS_BLACK "\033[1;30m"
#define ECS_RED "\033[0;31m"
#define ECS_GREEN "\033[0;32m"
#define ECS_YELLOW "\033[0;33m"
#define ECS_BLUE "\033[0;34m"
#define ECS_MAGENTA "\033[0;35m"
#define ECS_CYAN "\033[0;36m"
#define ECS_WHITE "\033[1;37m"
#define ECS_GREY "\033[0;37m"
#define ECS_NORMAL "\033[0;49m"
#define ECS_BOLD "\033[1;49m"
#ifdef __cplusplus
}
#endif
#endif // FLECS_LOG_H
/**
* @file vector.h
* @brief Vector datastructure.
*
* This is an implementation of a simple vector type. The vector is allocated in
* a single block of memory, with the element count, and allocated number of
* elements encoded in the block. As this vector is used for user-types it has
* been designed to support alignments higher than 8 bytes. This makes the size
* of the vector header variable in size. To reduce the overhead associated with
* retrieving or computing this size, the functions are wrapped in macro calls
* that compute the header size at compile time.
*
* The API provides a number of _t macro's, which accept a size and alignment.
* These macro's are used when no compile-time type is available.
*
* The vector guarantees contiguous access to its elements. When an element is
* removed from the vector, the last element is copied to the removed element.
*
* The API requires passing in the type of the vector. This type is used to test
* whether the size of the provided type equals the size of the type with which
* the vector was created. In release mode this check is not performed.
*
* When elements are added to the vector, it will automatically resize to the
* next power of two. This can change the pointer of the vector, which is why
* operations that can increase the vector size, accept a double pointer to the
* vector.
*/
#ifndef FLECS_VECTOR_H
#define FLECS_VECTOR_H
#ifdef __cplusplus
extern "C" {
#endif
/* Public, so we can do compile-time header size calculation */
struct ecs_vector_t {
int32_t count;
int32_t size;
#ifndef NDEBUG
int64_t elem_size;
#endif
};
/* Compute the header size of the vector from size & alignment */
#define ECS_VECTOR_U(size, alignment) size, ECS_CAST(int16_t, ECS_MAX(ECS_SIZEOF(ecs_vector_t), alignment))
/* Compute the header size of the vector from a provided compile-time type */
#define ECS_VECTOR_T(T) ECS_VECTOR_U(ECS_SIZEOF(T), ECS_ALIGNOF(T))
/* Utility macro's for creating vector on stack */
#ifndef NDEBUG
#define ECS_VECTOR_VALUE(T, elem_count)\
{\
.elem_size = (int32_t)(ECS_SIZEOF(T)),\
.count = elem_count,\
.size = elem_count\
}
#else
#define ECS_VECTOR_VALUE(T, elem_count)\
{\
.count = elem_count,\
.size = elem_count\
}
#endif
#define ECS_VECTOR_DECL(name, T, elem_count)\
struct {\
union {\
ecs_vector_t vector;\
uint64_t align;\
} header;\
T array[elem_count];\
} __##name##_value = {\
.header.vector = ECS_VECTOR_VALUE(T, elem_count)\
};\
const ecs_vector_t *name = (ecs_vector_t*)&__##name##_value
#define ECS_VECTOR_IMPL(name, T, elems, elem_count)\
ecs_os_memcpy(__##name##_value.array, elems, sizeof(T) * elem_count)
#define ECS_VECTOR_STACK(name, T, elems, elem_count)\
ECS_VECTOR_DECL(name, T, elem_count);\
ECS_VECTOR_IMPL(name, T, elems, elem_count)
typedef struct ecs_vector_t ecs_vector_t;
typedef int (*ecs_comparator_t)(
const void* p1,
const void *p2);
/** Create new vector. */
FLECS_API
ecs_vector_t* _ecs_vector_new(
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_new(T, elem_count) \
_ecs_vector_new(ECS_VECTOR_T(T), elem_count)
#define ecs_vector_new_t(size, alignment, elem_count) \
_ecs_vector_new(ECS_VECTOR_U(size, alignment), elem_count)
/* Create new vector, initialize it with provided array */
FLECS_API
ecs_vector_t* _ecs_vector_from_array(
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count,
void *array);
#define ecs_vector_from_array(T, elem_count, array)\
_ecs_vector_from_array(ECS_VECTOR_T(T), elem_count, array)
/* Initialize vector with zero's */
FLECS_API
void _ecs_vector_zero(
ecs_vector_t *vector,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_zero(vector, T) \
_ecs_vector_zero(vector, ECS_VECTOR_T(T))
/** Free vector */
FLECS_API
void ecs_vector_free(
ecs_vector_t *vector);
/** Clear values in vector */
FLECS_API
void ecs_vector_clear(
ecs_vector_t *vector);
/** Assert when the provided size does not match the vector type. */
FLECS_API
void ecs_vector_assert_size(
ecs_vector_t* vector_inout,
ecs_size_t elem_size);
/** Assert when the provided alignment does not match the vector type. */
FLECS_API
void ecs_vector_assert_alignment(
ecs_vector_t* vector,
ecs_size_t elem_alignment);
/** Add element to vector. */
FLECS_API
void* _ecs_vector_add(
ecs_vector_t **array_inout,
ecs_size_t elem_size,
int16_t offset);
#define ecs_vector_add(vector, T) \
((T*)_ecs_vector_add(vector, ECS_VECTOR_T(T)))
#define ecs_vector_add_t(vector, size, alignment) \
_ecs_vector_add(vector, ECS_VECTOR_U(size, alignment))
/** Add n elements to the vector. */
FLECS_API
void* _ecs_vector_addn(
ecs_vector_t **array_inout,
ecs_size_t elem_size,
int16_t offset,
int32_t elem_count);
#define ecs_vector_addn(vector, T, elem_count) \
((T*)_ecs_vector_addn(vector, ECS_VECTOR_T(T), elem_count))
#define ecs_vector_addn_t(vector, size, alignment, elem_count) \
_ecs_vector_addn(vector, ECS_VECTOR_U(size, alignment), elem_count)
/** Get element from vector. */
FLECS_API
void* _ecs_vector_get(