Skip to content
Flecs v4.1
field.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/impl/field.hpp
3 * @brief Field implementation.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11/** Construct a field from an iterator and field index.
12 *
13 * @param iter The iterator.
14 * @param index The field index.
15 */
16template <typename T>
17inline field<T>::field(iter &iter, int32_t index) {
18 *this = iter.field<T>(index);
19}
20
21/** Access an element at an index in the field.
22 *
23 * @param index The element index.
24 * @return Reference to the element.
25 */
26template <typename T>
27T& field<T>::operator[](size_t index) const {
28 ecs_assert(data_ != nullptr, ECS_INVALID_OPERATION,
29 "invalid nullptr dereference of component type %s",
30 _::type_name<T>());
32 "index %d out of range for array of component type %s",
33 index, _::type_name<T>());
34 ecs_assert(!index || !is_shared_, ECS_INVALID_PARAMETER,
35 "non-zero index invalid for shared field of component type %s",
36 _::type_name<T>());
37 return data_[index];
38}
39
40/** Return the first element of the component array.
41 * This operator is typically used when the field is shared.
42 *
43 * @return Reference to the first element.
44 */
45template <typename T>
47 ecs_assert(data_ != nullptr, ECS_INVALID_OPERATION,
48 "invalid nullptr dereference of component type %s",
49 _::type_name<T>());
50 return *data_;
51}
52
53/** Return the first element of the component array.
54 * This operator is typically used when the field is shared.
55 *
56 * @return Pointer to the first element.
57 */
58template <typename T>
60 ecs_assert(data_ != nullptr, ECS_INVALID_OPERATION,
61 "invalid nullptr dereference of component type %s",
62 _::type_name<T>());
63 ecs_assert(data_ != nullptr, ECS_INVALID_OPERATION,
64 "-> operator invalid for an array with >1 element of "
65 "component type %s, use [row] instead",
66 _::type_name<T>());
67 return data_;
68}
69
70}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
#define ECS_COLUMN_INDEX_OUT_OF_RANGE
Column index out of range error code.
Definition log.h:721
T * operator->() const
Return the first element of the component array.
Definition field.hpp:59
T & operator*() const
Return the first element of the component array.
Definition field.hpp:46
field(T *array, size_t count, bool is_shared=false)
Create a field from a component array.
Definition field.hpp:71
T & operator[](size_t index) const
Return an element in the component array.
Definition field.hpp:27
Class for iterating over query results.
Definition iter.hpp:68
flecs::field< A > field(int8_t index) const
Get read-only access to field data.
Definition iter.hpp:80