Skip to content
Flecs v4.1
impl.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/meta/impl.hpp
3 * @brief Meta implementation.
4 */
5
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>
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 built-in ones) for
65 // platform-specific types.
66
69 // Remove symbol to prevent validation errors, as it doesn't match
70 // the typename.
72 }
73
76 // Remove symbol to prevent validation errors, as it doesn't match
77 // the typename.
79 }
80
81 // Register opaque type support for C++ entity wrappers.
82 world.entity("::flecs::cpp").add(flecs::Module).scope([&]{
84 .opaque(flecs_entity_support<flecs::entity_view>);
86 .opaque(flecs_entity_support<flecs::entity>);
87 });
88}
89
90} // namespace _
91
92} // namespace meta
93
94
97}
98
101}
102
105}
106
107/** Create a primitive type. */
109 ecs_primitive_desc_t desc = {};
110 desc.kind = kind;
112 ecs_assert(eid != 0, ECS_INVALID_OPERATION, nullptr);
113 return flecs::entity(world_, eid);
114}
115
116/** Create an array type. */
117inline flecs::entity world::array(flecs::entity_t elem_id, int32_t array_count) {
118 ecs_array_desc_t desc = {};
119 desc.type = elem_id;
120 desc.count = array_count;
122 ecs_assert(eid != 0, ECS_INVALID_OPERATION, nullptr);
123 return flecs::entity(world_, eid);
124}
125
126/** Create an array type. */
127template <typename T>
128inline flecs::entity world::array(int32_t array_count) {
129 return this->array(_::type<T>::id(world_), array_count);
130}
131
133 ecs_vector_desc_t desc = {};
134 desc.type = elem_id;
136 ecs_assert(eid != 0, ECS_INVALID_OPERATION, nullptr);
137 return flecs::entity(world_, eid);
138}
139
140template <typename T>
142 return this->vector(_::type<T>::id(world_));
143}
144
145} // namespace flecs
146
147inline int ecs_serializer_t::value(ecs_entity_t type, const void *v) const {
148 return this->value_(this, type, v);
149}
150
151template <typename T>
152inline int ecs_serializer_t::value(const T& v) const {
153 return this->value(flecs::_::type<T>::id(
154 const_cast<flecs::world_t*>(this->world)), &v);
155}
156
157inline int ecs_serializer_t::member(const char *name) const {
158 return this->member_(this, name);
159}
component & opaque(const Func &type_support)
Register an opaque type interface.
Definition component.inl:8
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:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
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.
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.
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:395
flecs::entity primitive(flecs::meta::primitive_kind_t kind)
Create a primitive type.
Definition impl.hpp:108
EcsType Type
Components.
Definition decl.hpp:40
ecs_member_t member_t
Embedded type aliases.
Definition decl.hpp:35
ecs_primitive_kind_t primitive_kind_t
Primitive type kinds supported by the reflection system.
Definition decl.hpp:93
ecs_serializer_t serializer
Serializer object, used for serializing opaque types.
Definition opaque.hpp:19
flecs::entity vector(flecs::entity_t elem_id)
Create a vector type.
Definition impl.hpp:132
flecs::entity array(flecs::entity_t elem_id, int32_t array_count)
Create an array type.
Definition impl.hpp:117
flecs::entity vector()
Create a vector type.
Definition impl.hpp:141
ecs_bool_t bool_t
Primitive type aliases.
Definition decl.hpp:19
ecs_type_kind_t type_kind_t
Type kinds supported by the reflection system.
Definition decl.hpp:80
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
#define ecs_remove_pair(world, subject, first, second)
Remove a pair from an entity.
Definition flecs_c.h:283
flecs::opaque< EntityType > flecs_entity_support(flecs::world &)
Type support for entity wrappers.
Definition impl.hpp:17
A (string) identifier.
Definition flecs.h:1611
Used with ecs_array_init().
Definition meta.h:1523
ecs_entity_t type
Element type.
Definition meta.h:1525
int32_t count
Number of elements.
Definition meta.h:1526
const ecs_world_t * world
The world.
Definition meta.h:636
Used with ecs_primitive_init().
Definition meta.h:1468
ecs_primitive_kind_t kind
Primitive type kind.
Definition meta.h:1470
int(*) member(const struct ecs_serializer_t *ser, const char *member)
Serialize member.
Definition meta.h:356
int(*) value(const struct ecs_serializer_t *ser, ecs_entity_t type, const void *value)
Serialize value.
Definition meta.h:350
const ecs_world_t * world
The world.
Definition meta.h:360
Used with ecs_vector_init().
Definition meta.h:1542
ecs_entity_t type
Element type.
Definition meta.h:1544
flecs::entity get_type() const
Get the type of a value.
Definition impl.hpp:95
flecs::entity get_entity() const
Get entity value.
Definition impl.hpp:103
flecs::entity get_unit() const
Get the unit of a value.
Definition impl.hpp:99
ecs_meta_cursor_t cursor_
Cursor object.
Definition cursor.hpp:157
const Self & scope(const Func &func) const
The function will be run with the scope set to the current entity.
Definition builder.hpp:533
Entity.
Definition entity.hpp:30
Type-safe interface for opaque types.
Definition opaque.hpp:39
opaque & as_type(flecs::id_t func)
Set the type that describes the kind/structure of the opaque type.
Definition opaque.hpp:47
opaque & serialize(flecs::serialize< T > func)
Set the serialize function.
Definition opaque.hpp:53
The world.
Definition world.hpp:129
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1009