Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8FLECS_ENUM_LAST(flecs::meta::type_kind_t, flecs::meta::TypeKindLast)
9FLECS_ENUM_LAST(flecs::meta::primitive_kind_t, flecs::meta::PrimitiveKindLast)
10
11namespace flecs {
12namespace meta {
13namespace _ {
14
15/* Type support for entity wrappers */
16template <typename EntityType>
17inline flecs::opaque<EntityType> flecs_entity_support(flecs::world&) {
19 .as_type(flecs::Entity)
20 .serialize([](const flecs::serializer *ser, const EntityType *data) {
21 flecs::entity_t id = data->id();
22 return ser->value(flecs::Entity, &id);
23 })
24 .assign_entity(
25 [](EntityType *dst, flecs::world_t *world, flecs::entity_t e) {
26 *dst = EntityType(world, e);
27 });
28}
29
30inline void init(flecs::world& world) {
31 world.component<bool_t>("flecs::meta::bool");
32 world.component<char_t>("flecs::meta::char");
33 world.component<u8_t>("flecs::meta::u8");
34 world.component<u16_t>("flecs::meta::u16");
35 world.component<u32_t>("flecs::meta::u32");
36 world.component<u64_t>("flecs::meta::u64");
37 world.component<i8_t>("flecs::meta::i8");
38 world.component<i16_t>("flecs::meta::i16");
39 world.component<i32_t>("flecs::meta::i32");
40 world.component<i64_t>("flecs::meta::i64");
41 world.component<f32_t>("flecs::meta::f32");
42 world.component<f64_t>("flecs::meta::f64");
43
44 world.component<type_kind_t>("flecs::meta::type_kind");
45 world.component<primitive_kind_t>("flecs::meta::primitive_kind");
46 world.component<member_t>("flecs::meta::member_t");
47 world.component<enum_constant_t>("flecs::meta::enum_constant");
48 world.component<bitmask_constant_t>("flecs::meta::bitmask_constant");
49
50 world.component<Type>("flecs::meta::type");
51 world.component<TypeSerializer>("flecs::meta::TypeSerializer");
52 world.component<Primitive>("flecs::meta::primitive");
53 world.component<Enum>("flecs::meta::enum");
54 world.component<Bitmask>("flecs::meta::bitmask");
55 world.component<Member>("flecs::meta::member");
56 world.component<MemberRanges>("flecs::meta::member_ranges");
57 world.component<Struct>("flecs::meta::struct");
58 world.component<Array>("flecs::meta::array");
59 world.component<Vector>("flecs::meta::vector");
60
61 world.component<Unit>("flecs::meta::unit");
62
63 // To support member<uintptr_t> and member<intptr_t> register components
64 // (that do not have conflicting symbols with builtin ones) for platform
65 // specific types.
66
67 if (!flecs::is_same<i32_t, iptr_t>() && !flecs::is_same<i64_t, iptr_t>()) {
68 flecs::_::type<iptr_t>::init(flecs::Iptr, true);
69 ecs_assert(flecs::type_id<iptr_t>() == flecs::Iptr,
70 ECS_INTERNAL_ERROR, NULL);
71 // Remove symbol to prevent validation errors, as it doesn't match with
72 // the typename
73 ecs_remove_pair(world, flecs::Iptr, ecs_id(EcsIdentifier), EcsSymbol);
74 }
75
76 if (!flecs::is_same<u32_t, uptr_t>() && !flecs::is_same<u64_t, uptr_t>()) {
77 flecs::_::type<uptr_t>::init(flecs::Uptr, true);
78 ecs_assert(flecs::type_id<uptr_t>() == flecs::Uptr,
79 ECS_INTERNAL_ERROR, NULL);
80 // Remove symbol to prevent validation errors, as it doesn't match with
81 // the typename
82 ecs_remove_pair(world, flecs::Uptr, ecs_id(EcsIdentifier), EcsSymbol);
83 }
84
85 // Register opaque type support for C++ entity wrappers
86 world.entity("::flecs::cpp").add(flecs::Module).scope([&]{
88 .opaque(flecs_entity_support<flecs::entity_view>);
90 .opaque(flecs_entity_support<flecs::entity>);
91 });
92}
93
94} // namespace _
95
96} // namespace meta
97
98
99inline flecs::entity cursor::get_type() const {
101}
102
103inline flecs::entity cursor::get_unit() const {
105}
106
107inline flecs::entity cursor::get_entity() const {
109}
110
113 ecs_primitive_desc_t desc = {};
114 desc.kind = kind;
115 flecs::entity_t eid = ecs_primitive_init(world_, &desc);
116 ecs_assert(eid != 0, ECS_INVALID_OPERATION, NULL);
117 return flecs::entity(world_, eid);
118}
119
121inline flecs::entity world::array(flecs::entity_t elem_id, int32_t array_count) {
122 ecs_array_desc_t desc = {};
123 desc.type = elem_id;
124 desc.count = array_count;
125 flecs::entity_t eid = ecs_array_init(world_, &desc);
126 ecs_assert(eid != 0, ECS_INVALID_OPERATION, NULL);
127 return flecs::entity(world_, eid);
128}
129
131template <typename T>
132inline flecs::entity world::array(int32_t array_count) {
133 return this->array(_::type<T>::id(world_), array_count);
134}
135
136inline flecs::entity world::vector(flecs::entity_t elem_id) {
137 ecs_vector_desc_t desc = {};
138 desc.type = elem_id;
139 flecs::entity_t eid = ecs_vector_init(world_, &desc);
140 ecs_assert(eid != 0, ECS_INVALID_OPERATION, NULL);
141 return flecs::entity(world_, eid);
142}
143
144template <typename T>
146 return this->vector(_::type<T>::id(world_));
147}
148
149} // namespace flecs
150
151inline int ecs_serializer_t::value(ecs_entity_t type, const void *v) const {
152 return this->value_(this, type, v);
153}
154
155template <typename T>
156inline int ecs_serializer_t::value(const T& v) const {
157 return this->value(flecs::_::type<T>::id(
158 const_cast<flecs::world_t*>(this->world)), &v);
159}
160
161inline int ecs_serializer_t::member(const char *name) const {
162 return this->member_(this, name);
163}
const ecs_entity_t EcsSymbol
Tag to indicate symbol identifier.
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component id for EcsDocDescription.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
FLECS_API ecs_entity_t ecs_meta_get_entity(const ecs_meta_cursor_t *cursor)
Get field value as entity.
FLECS_API ecs_entity_t ecs_meta_get_unit(const ecs_meta_cursor_t *cursor)
Get unit of current field.
ecs_primitive_kind_t
Primitive type kinds supported by meta addon.
Definition meta.h:172
FLECS_API ecs_entity_t ecs_meta_get_type(const ecs_meta_cursor_t *cursor)
Get type of current field.
FLECS_API ecs_entity_t ecs_vector_init(ecs_world_t *world, const ecs_vector_desc_t *desc)
Create a new vector type.
ecs_type_kind_t
Type kinds supported by meta addon.
Definition meta.h:153
FLECS_API ecs_entity_t ecs_primitive_init(ecs_world_t *world, const ecs_primitive_desc_t *desc)
Create a new primitive type.
FLECS_API ecs_entity_t ecs_array_init(ecs_world_t *world, const ecs_array_desc_t *desc)
Create a new array type.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
flecs::entity primitive(flecs::meta::primitive_kind_t kind)
Create primitive type.
flecs::entity array(flecs::entity_t elem_id, int32_t array_count)
Create array type.
flecs::entity vector()
Create vector type.
flecs::component< T > component(Args &&... args) const
Find or register component.
flecs::entity entity(Args &&... args) const
Create an entity.
A (string) identifier.
Definition flecs.h:1441
Used with ecs_array_init().
Definition meta.h:996
ecs_entity_t type
Element type.
Definition meta.h:998
int32_t count
Number of elements.
Definition meta.h:999
const ecs_world_t * world
The world.
Definition meta.h:583
Used with ecs_primitive_init().
Definition meta.h:942
ecs_primitive_kind_t kind
Primitive type kind.
Definition meta.h:944
Serializer interface.
Definition meta.h:323
int(* value)(const struct ecs_serializer_t *ser, ecs_entity_t type, const void *value)
Pointer to the value to serialize.
Definition meta.h:325
int(* member)(const struct ecs_serializer_t *ser, const char *member)
Member name.
Definition meta.h:331
Used with ecs_vector_init().
Definition meta.h:1015
ecs_entity_t type
Element type.
Definition meta.h:1017
flecs::entity get_type() const
Get type of value.
flecs::entity get_entity() const
Get entity value.
flecs::entity get_unit() const
Get unit of value.
ecs_meta_cursor_t cursor_
Cursor object.
Definition cursor.hpp:157
const Self & scope(const Func &func) const
The function will be ran with the scope set to the current entity.
Definition builder.hpp:966
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
Entity.
Definition entity.hpp:30
Type safe interface for opaque types.
Definition opaque.hpp:32
opaque & as_type(flecs::id_t func)
Type that describes the type kind/structure of the opaque type.
Definition opaque.hpp:40
opaque & serialize(flecs::serialize< T > func)
Serialize function.
Definition opaque.hpp:46
The world.
Definition world.hpp:137