Skip to content

Commit

Permalink
#663 add unit examples, add get value functions to meta cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Mar 7, 2022
1 parent 27412b6 commit 427ed1c
Show file tree
Hide file tree
Showing 16 changed files with 772 additions and 38 deletions.
5 changes: 5 additions & 0 deletions examples/c/reflection/units/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.bake_cache
.DS_Store
.vscode
gcov
bin
16 changes: 16 additions & 0 deletions examples/c/reflection/units/include/units.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef UNITS_H
#define UNITS_H

/* This generated file contains includes for project dependencies */
#include "units/bake_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif

24 changes: 24 additions & 0 deletions examples/c/reflection/units/include/units/bake_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef UNITS_BAKE_CONFIG_H
#define UNITS_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>

#endif

9 changes: 9 additions & 0 deletions examples/c/reflection/units/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "units",
"type": "application",
"value": {
"use": [
"flecs"
]
}
}
71 changes: 71 additions & 0 deletions examples/c/reflection/units/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <units.h>
#include <stdio.h>

typedef struct {
double temperature;
double pressure;
double precipitation;
} WeatherStation;

void print_value(ecs_world_t *world, const ecs_meta_cursor_t *cur) {
// Get unit entity and component
ecs_entity_t u = ecs_meta_get_unit(cur);
const EcsUnit *u_data = ecs_get(world, u, EcsUnit);

// Print value with unit symbol
printf("%s: %.1f %s\n", ecs_meta_get_member(cur), ecs_meta_get_float(cur),
u_data->symbol);
}

int main(int argc, char *argv[]) {
ecs_world_t *ecs = ecs_init_w_args(argc, argv);

// Import units module.
ECS_IMPORT(ecs, FlecsUnits);

ECS_COMPONENT(ecs, WeatherStation);

// Register reflection data with units. This can improve the way information
// is visualized in tools, such as the explorer.
ecs_struct_init(ecs, &(ecs_struct_desc_t) {
.entity.entity = ecs_id(WeatherStation),
.members = {
{
.name = "temperature",
.type = ecs_id(ecs_f64_t),
.unit = EcsCelsius
},
{
.name = "pressure",
.type = ecs_id(ecs_f64_t),
.unit = EcsBar
},
{
.name = "precipitation",
.type = ecs_id(ecs_f64_t),
.unit = EcsMilliMeters
},
}
});

ecs_entity_t e = ecs_set(ecs, 0, WeatherStation, {24, 1.2, 0.5});

// Use cursor API to print values with units
WeatherStation *ptr = ecs_get_mut(ecs, e, WeatherStation, 0);
ecs_meta_cursor_t cur = ecs_meta_cursor(ecs, ecs_id(WeatherStation), ptr);

ecs_meta_push(&cur);
print_value(ecs, &cur);
ecs_meta_next(&cur);
print_value(ecs, &cur);
ecs_meta_next(&cur);
print_value(ecs, &cur);
ecs_meta_pop(&cur);

return ecs_fini(ecs);

// Output:
// temperature: 24.0 °C
// pressure: 1.2 bar
// precipitation: 0.5 mm
}
16 changes: 16 additions & 0 deletions examples/cpp/reflection/units/include/units.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef UNITS_H
#define UNITS_H

/* This generated file contains includes for project dependencies */
#include "units/bake_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif

24 changes: 24 additions & 0 deletions examples/cpp/reflection/units/include/units/bake_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef UNITS_BAKE_CONFIG_H
#define UNITS_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>

#endif

11 changes: 11 additions & 0 deletions examples/cpp/reflection/units/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "units",
"type": "application",
"value": {
"use": [
"flecs"
],
"language": "c++",
"public": false
}
}
50 changes: 50 additions & 0 deletions examples/cpp/reflection/units/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <units.h>
#include <iostream>

struct WeatherStation {
double temperature;
double pressure;
double precipitation;
};

void print_value(const flecs::meta::cursor& cur) {
// Get unit entity and component
flecs::entity u = cur.get_unit();
const flecs::Unit *u_data = u.get<flecs::Unit>();

// Print value with unit symbol
std::cout << cur.get_member() << ": " << cur.get_float()
<< " " << u_data->symbol << "\n";
}

int main(int, char *[]) {
flecs::world ecs;

// Import units module.
ecs.import<flecs::units>();

// Register reflection data with units. This can improve the way information
// is visualized in tools, such as the explorer.
ecs.component<WeatherStation>()
.member<double, flecs::units::temperature::Celsius>("temperature")
.member<double, flecs::units::pressure::Bar>("pressure")
.member<double, flecs::units::length::MilliMeters>("precipitation");

auto e = ecs.entity().set<WeatherStation>({24, 1.2, 0.5});

// Use cursor API to print values with units
WeatherStation *ptr = e.get_mut<WeatherStation>();
auto cur = ecs.cursor<WeatherStation>(ptr);
cur.push();
print_value(cur);
cur.next();
print_value(cur);
cur.next();
print_value(cur);
cur.pop();

// Output:
// temperature: 24 °C
// pressure: 1.2 bar
// precipitation: 0.5 mm
}
Loading

0 comments on commit 427ed1c

Please sign in to comment.