Flecs v4.1
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
8namespace flecs {
9
12
13 // Import C module.
15
18
19 world.entity<metrics::Instance>("::flecs::metrics::Instance");
20 world.entity<metrics::Metric>("::flecs::metrics::Metric");
21 world.entity<metrics::Counter>("::flecs::metrics::Metric::Counter");
22 world.entity<metrics::CounterId>("::flecs::metrics::Metric::CounterId");
23 world.entity<metrics::CounterIncrement>("::flecs::metrics::Metric::CounterIncrement");
24 world.entity<metrics::Gauge>("::flecs::metrics::Metric::Gauge");
25}
26
28 if (!created_) {
29 ecs_metric_init(world_, &desc_);
30 }
31}
32
33inline metric_builder& metric_builder::member(const char *name) {
35 if (desc_.id) {
36 flecs::entity_t type = ecs_get_typeid(world_, desc_.id);
37 m = flecs::entity(world_, type).lookup(name);
38 } else {
39 m = flecs::world(world_).lookup(name);
40 }
41 if (!m) {
42 flecs::log::err("member '%s' not found", name);
43 }
44 return member(m);
45}
46
47template <typename T>
48inline metric_builder& metric_builder::member(const char *name) {
49 flecs::entity e (world_, _::type<T>::id(world_));
50 flecs::entity_t m = e.lookup(name);
51 if (!m) {
52 flecs::log::err("member '%s' not found in type '%s'",
53 name, e.path().c_str());
54 return *this;
55 }
56 return member(m);
57}
58
59inline metric_builder& metric_builder::dotmember(const char *expr) {
60 desc_.dotmember = expr;
61 return *this;
62}
63
64template <typename T>
65inline metric_builder& metric_builder::dotmember(const char *expr) {
66 desc_.dotmember = expr;
67 desc_.id = _::type<T>::id(world_);
68 return *this;
69}
70
71inline metric_builder::operator flecs::entity() {
72 if (!created_) {
73 created_ = true;
74 flecs::entity result(world_, ecs_metric_init(world_, &desc_));
75 desc_.entity = result;
76 return result;
77 } else {
78 return flecs::entity(world_, desc_.entity);
79 }
80}
81
82template <typename... Args>
83inline flecs::metric_builder world::metric(Args &&... args) const {
84 flecs::entity result(world_, FLECS_FWD(args)...);
85 return flecs::metric_builder(world_, result);
86}
87
88template <typename Kind>
89inline untyped_component& untyped_component::metric(
90 flecs::entity_t parent,
91 const char *brief,
92 const char *metric_name)
93{
96
97 const flecs::member_t *m = ecs_cpp_last_member(w, e);
98 if (!m) {
99 return *this;
100 }
101
102 flecs::entity me = w.entity(m->member);
103 flecs::entity metric_entity = me;
104 if (parent) {
105 const char *component_name = e.name();
106 if (!metric_name) {
107 if (ecs_os_strcmp(m->name, "value") || !component_name) {
108 metric_entity = w.scope(parent).entity(m->name);
109 } else {
110 // If name of member is "value", use name of type.
111 char *snake_name = flecs_to_snake_case(component_name);
112 metric_entity = w.scope(parent).entity(snake_name);
113 ecs_os_free(snake_name);
114 }
115 } else {
116 metric_entity = w.scope(parent).entity(metric_name);
117 }
118 }
119
120 w.metric(metric_entity).member(me).kind<Kind>().brief(brief);
121
122 return *this;
123}
124
125}
FLECS_API ecs_entity_t ecs_metric_init(ecs_world_t *world, const ecs_metric_desc_t *desc)
Create a new metric.
FLECS_API void FlecsMetricsImport(ecs_world_t *world)
Metrics module import function.
flecs::metric_builder metric(Args &&... args) const
Create a metric.
flecs::entity import()
Import a module.
flecs::component< T > component(Args &&... args) const
Find or register a component.
flecs::entity entity(Args &&... args) const
Create an entity.
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
void err(const char *fmt,...)
Error (level -3).
Definition log.hpp:96
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t component)
Get the type for a component.
Component that stores metric source.
Definition metrics.h:71
Component that stores metric value.
Definition metrics.h:66
Element type of members vector in EcsStruct.
Definition meta.h:220
const char * name
Must be set when used with ecs_struct_desc_t.
Definition meta.h:222
ecs_entity_t member
Should not be set by ecs_struct_desc_t.
Definition meta.h:260
const char * dotmember
Member dot expression.
Definition metrics.h:89
ecs_id_t id
Tracks whether entities have the specified component ID.
Definition metrics.h:93
const Self & scope(const Func &func) const
The function will be run with the scope set to the current entity.
Definition builder.hpp:1167
flecs::string_view name() const
Return the entity name.
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:172
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
Metric builder interface.
Definition builder.hpp:16
~metric_builder()
Destructor.
Definition impl.hpp:27
metric_builder & dotmember(const char *name)
Set the member to use for the metric using dot notation.
Definition impl.hpp:59
metric_builder & member(flecs::entity_t e)
Set the member to use for the metric by entity ID.
Definition builder.hpp:28
Counter ID metric kind.
Definition decl.hpp:37
Counter increment metric kind.
Definition decl.hpp:35
Counter metric kind.
Definition decl.hpp:33
Gauge metric kind.
Definition decl.hpp:39
Metric instance tag.
Definition decl.hpp:29
Metric tag.
Definition decl.hpp:31
metrics(flecs::world &world)
Construct the metrics module.
Definition impl.hpp:10
const char * c_str() const
Return the C string.
Definition string.hpp:109
Type class.
Definition type.hpp:21
Units module.
Definition decl.hpp:10
The world.
Definition world.hpp:246
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1545
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:102