Flecs: include/flecs/addons/cpp/mixins/metrics/impl.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
5
6#pragma once
7
8namespace flecs {
9
10 inline metrics::metrics(flecs::world& world) {
11 world.import<flecs::units>();
12
13 // Import C module.
14 FlecsMetricsImport(world);
15
16 world.component<Value>();
17 world.component<Source>();
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
27 inline metric_builder::~metric_builder() {
28 if (!created_) {
29 ecs_metric_init(world_, &desc_);
30 }
31}
32
33 inline metric_builder& metric_builder::member(const char *name) {
34 flecs::entity m;
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>
48 inline 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
59 inline metric_builder& metric_builder::dotmember(const char *expr) {
60 desc_.dotmember = expr;
61 return *this;
62}
63
64template <typename T>
65 inline metric_builder& metric_builder::dotmember(const char *expr) {
66 desc_.dotmember = expr;
67 desc_.id = _::type<T>::id(world_);
68 return *this;
69}
70
71 inline 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>
83 inline 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>
89 inline untyped_component& untyped_component::metric(
90 flecs::entity_t parent,
91 const char *brief,
92 const char *metric_name)
93{
94 flecs::world w(world_);
95 flecs::entity e(world_, id_);
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}
ecs_metric_init
FLECS_API ecs_entity_t ecs_metric_init(ecs_world_t *world, const ecs_metric_desc_t *desc)
Create a new metric.
FlecsMetricsImport
FLECS_API void FlecsMetricsImport(ecs_world_t *world)
Metrics module import function.
flecs::member_t
ecs_member_t member_t
Embedded type aliases.
Definition decl.hpp:35
flecs::world::metric
flecs::metric_builder metric(Args &&... args) const
Create a metric.
Definition impl.hpp:83
flecs::untyped_component::metric
untyped_component & metric(flecs::entity_t parent=0, const char *brief=nullptr, const char *name=nullptr)
Register a member as a metric.
Definition impl.hpp:89
flecs::world::import
flecs::entity import()
Import a module.
Definition impl.hpp:107
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::log::err
void err(const char *fmt,...)
Error (level -3).
Definition log.hpp:96
ecs_get_typeid
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t component)
Get the type for a component.
ecs_member_t::name
const char * name
Must be set when used with ecs_struct_desc_t.
Definition meta.h:222
ecs_member_t::member
ecs_entity_t member
Should not be set by ecs_struct_desc_t.
Definition meta.h:260
flecs::_::type
Definition flecs.hpp:39
flecs::entity_view::name
flecs::string_view name() const
Return the entity name.
Definition entity_view.hpp:88
flecs::entity_view::path
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Definition entity_view.hpp:106
flecs::entity_view::lookup
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:172
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
flecs::entity
Entity.
Definition entity.hpp:30
flecs::id::id_
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::id::world_
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
flecs::metric_builder
Metric builder interface.
Definition builder.hpp:16
flecs::metric_builder::~metric_builder
~metric_builder()
Destructor.
Definition impl.hpp:27
flecs::metric_builder::id
metric_builder & id(flecs::id_t the_id)
Set the ID for the metric.
Definition builder.hpp:48
flecs::metric_builder::metric_builder
metric_builder(flecs::world_t *world, flecs::entity_t entity)
Construct a metric builder.
Definition builder.hpp:18
flecs::metric_builder::dotmember
metric_builder & dotmember(const char *name)
Set the member to use for the metric using dot notation.
Definition impl.hpp:59
flecs::metric_builder::member
metric_builder & member(flecs::entity_t e)
Set the member to use for the metric by entity ID.
Definition builder.hpp:28
flecs::metric_builder::kind
metric_builder & kind(flecs::entity_t the_kind)
Set the metric kind (e.g., Counter, Gauge).
Definition builder.hpp:90
flecs::metrics::CounterId
Counter ID metric kind.
Definition decl.hpp:37
flecs::metrics::CounterIncrement
Counter increment metric kind.
Definition decl.hpp:35
flecs::metrics::Counter
Counter metric kind.
Definition decl.hpp:33
flecs::metrics::Gauge
Gauge metric kind.
Definition decl.hpp:39
flecs::metrics::Instance
Metric instance tag.
Definition decl.hpp:29
flecs::metrics::Metric
Metric tag.
Definition decl.hpp:31
flecs::metrics::metrics
metrics(flecs::world &world)
Construct the metrics module.
Definition impl.hpp:10
flecs::metrics::Value
EcsMetricValue Value
Metric value component.
Definition decl.hpp:24
flecs::metrics::Source
EcsMetricSource Source
Metric source component.
Definition decl.hpp:26
flecs::string::c_str
const char * c_str() const
Return the C string.
Definition string.hpp:109
flecs::type
Type class.
Definition type.hpp:21
flecs::units
Units module.
Definition decl.hpp:10
flecs::untyped_component::untyped_component
untyped_component()
Default constructor.
Definition component.hpp:317
flecs::world
The world.
Definition world.hpp:246
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540
flecs::world::lookup
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
flecs::world::scope
void scope(id_t parent, const Func &func) const
All entities created in the function are created in the scope.
Definition world.hpp:1228