Skip to content
Flecs v4.1
type.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/type.hpp
3 * @brief Utility functions for ID vector.
4 */
5
6#pragma once
7
8namespace flecs {
9
10/**
11 * @defgroup cpp_types Types
12 * @ingroup cpp_core
13 * @brief Type operations.
14 *
15 * @{
16 */
17
18/** Type class.
19 * A type is a vector of component IDs that can be requested from entities or tables.
20 */
21struct type {
22 /** Default constructor. Creates an empty type. */
23 type() : world_(nullptr), type_(nullptr) { }
24
25 /** Construct a type from a world and C type pointer.
26 *
27 * @param world The world.
28 * @param t Pointer to the C type.
29 */
31 : world_(world)
32 , type_(t) { }
33
34 /** Convert the type to a comma-separated string.
35 *
36 * @return String representation of the type.
37 */
39 return flecs::string(ecs_type_str(world_, type_));
40 }
41
42 /** Return the number of IDs in the type.
43 *
44 * @return The number of IDs.
45 */
46 int32_t count() const {
47 if (!type_) {
48 return 0;
49 }
50 return type_->count;
51 }
52
53 /** Return a pointer to the ID array.
54 *
55 * @return Pointer to the array of IDs, or nullptr if empty.
56 */
57 flecs::id_t* array() const {
58 if (!type_) {
59 return nullptr;
60 }
61 return type_->array;
62 }
63
64 /** Get the ID at a specified index in the type.
65 *
66 * @param index The index of the ID to get.
67 * @return The ID at the specified index.
68 */
69 flecs::id get(int32_t index) const {
70 ecs_assert(type_ != nullptr, ECS_INVALID_PARAMETER, nullptr);
71 ecs_assert(type_->count > index, ECS_OUT_OF_RANGE, nullptr);
72 if (!type_) {
73 return flecs::id();
74 }
75 return flecs::id(world_, type_->array[index]);
76 }
77
78 /** Get an iterator to the beginning of the type's ID array. */
79 const flecs::id_t* begin() const {
80 if (type_ && type_->count) {
81 return type_->array;
82 } else {
83 return &empty_;
84 }
85 }
86
87 /** Get an iterator to the end of the type's ID array. */
88 const flecs::id_t* end() const {
89 if (type_ && type_->count) {
90 return &type_->array[type_->count];
91 } else {
92 return &empty_;
93 }
94 }
95
96 /** Implicit conversion to type_t. */
97 operator const type_t*() const {
98 return type_;
99 }
100private:
101 world_t *world_;
102 const type_t *type_;
103 flecs::id_t empty_;
104};
105
106/** @} */
107
108}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_OUT_OF_RANGE
Out of range error code.
Definition log.h:677
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_type_t type_t
Type type.
Definition c_types.hpp:22
ecs_world_t world_t
World type.
Definition c_types.hpp:18
char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
Convert a type to a string.
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Owned string wrapper.
Definition string.hpp:15
const flecs::id_t * end() const
Get an iterator to the end of the type's ID array.
Definition type.hpp:88
flecs::id_t * array() const
Return a pointer to the ID array.
Definition type.hpp:57
int32_t count() const
Return the number of IDs in the type.
Definition type.hpp:46
flecs::string str() const
Convert the type to a comma-separated string.
Definition type.hpp:38
type()
Default constructor.
Definition type.hpp:23
flecs::id get(int32_t index) const
Get the ID at a specified index in the type.
Definition type.hpp:69
type(world_t *world, const type_t *t)
Construct a type from a world and C type pointer.
Definition type.hpp:30
const flecs::id_t * begin() const
Get an iterator to the beginning of the type's ID array.
Definition type.hpp:79
The world.
Definition world.hpp:129