Flecs v4.0
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
10inline 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
27inline metric_builder::~metric_builder() {
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{
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}
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 metric.
flecs::entity import()
Import a module.
flecs::component< T > component(Args &&... args) const
Find or register component.
flecs::entity entity(Args &&... args) const
Create an entity.
void err(const char *fmt,...)
Trace (level -3)
Definition log.hpp:68
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t id)
Get the type for an id.
Element type of members vector in EcsStruct.
Definition meta.h:222
const char * name
Must be set when used with ecs_struct_desc_t.
Definition meta.h:224
ecs_entity_t member
Should not be set by ecs_struct_desc_t.
Definition meta.h:261
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 ran with the scope set to the current entity.
Definition builder.hpp:966
flecs::string_view name() const
Return the entity name.
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:177
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:73
Entity.
Definition entity.hpp:30
Event builder interface.
Definition builder.hpp:18
The world.
Definition world.hpp:137
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:76