Skip to content
Flecs v4.1
field.hpp
Go to the documentation of this file.
1
2/**
3 * @file addons/cpp/field.hpp
4 * @brief Wrapper classes for fields returned by flecs::iter.
5 */
6
7#pragma once
8
9/**
10 * @defgroup cpp_field Fields
11 * @ingroup cpp_core
12 * Field helper types.
13 *
14 * @{
15 */
16
17namespace flecs
18{
19
20/** Unsafe wrapper class around a field.
21 * This class can be used when a system does not know the type of a field at
22 * compile time.
23 *
24 * @ingroup cpp_iterator
25 */
26struct untyped_field {
27 untyped_field(void* array, size_t size, size_t count, bool is_shared = false)
28 : data_(array)
29 , size_(size)
30 , count_(count)
31 , is_shared_(is_shared) {}
32
33 /** Return an element in the component array.
34 * This operator may only be used if the field is not shared.
35 *
36 * @param index Index of element.
37 * @return Reference to element.
38 */
39 void* operator[](size_t index) const {
40 ecs_assert(!is_shared_ || !index, ECS_INVALID_PARAMETER,
41 "invalid usage of [] operator for shared component field");
43 "index %d out of range for field", index);
44 return ECS_OFFSET(data_, size_ * index);
45 }
46
47protected:
48 void* data_;
49 size_t size_;
50 size_t count_;
51 bool is_shared_;
52};
53
54/** Wrapper class around a field.
55 *
56 * @tparam T Component type of the field.
57 *
58 * @ingroup cpp_iterator
59 */
60template <typename T>
61struct field {
62 static_assert(std::is_empty<T>::value == false,
63 "invalid type for field, cannot iterate empty type");
64
65 /** Create a field from a component array.
66 *
67 * @param array Pointer to the component array.
68 * @param count Number of elements in the component array.
69 * @param is_shared Whether the component is shared.
70 */
71 field(T* array, size_t count, bool is_shared = false)
72 : data_(array)
73 , count_(count)
74 , is_shared_(is_shared) {}
75
76 /** Create a field from an iterator.
77 *
78 * @param iter Iterator object.
79 * @param field Index of the field in the query being iterated over.
80 */
82
83 /** Return an element in the component array.
84 * This operator may only be used if the field is not shared.
85 *
86 * @param index Index of element.
87 * @return Reference to element.
88 */
89 T& operator[](size_t index) const;
90
91 /** Return the first element of the component array.
92 * This operator is typically used when the field is shared.
93 *
94 * @return Reference to the first element.
95 */
96 T& operator*() const;
97
98 /** Return the first element of the component array.
99 * This operator is typically used when the field is shared.
100 *
101 * @return Pointer to the first element.
102 */
103 T* operator->() const;
104
105protected:
106 T* data_;
107 size_t count_;
108 bool is_shared_;
109};
110
111} // namespace flecs
112
113/** @} */
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#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
Array class (primary template, disabled).
Definition array.hpp:47
T * operator->() const
Return the first element of the component array.
Definition field.hpp:59
field(iter &iter, int field)
Create a field from an iterator.
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
void * operator[](size_t index) const
Return an element in the component array.
Definition field.hpp:39