Skip to content
Flecs v4.1
reflection/portable_type

  • C++

    View on GitHub

    1#include <portable_type.h>
    2#include <iostream>
    3
    4// The actual types of int32_t and uintptr_t vary between platforms.
    5struct PortableType {
    6 int32_t i32;
    7 uintptr_t intptr;
    8};
    9
    10int main(int, char *[]) {
    11 flecs::world ecs;
    12
    13 // Register component. Do not use std::int32_t or std::uintptr_t for the
    14 // member type as this will resolve to a different integer type depending on
    15 // the platform, which can cause unexpected issues when type information is
    16 // shared between platforms.
    17 ecs.component<PortableType>()
    18 .member(flecs::I32, "i32") // Use platform-independent type ids
    19 .member(flecs::Uptr, "intptr");
    20
    21 // Create entity with PortableType
    22 flecs::entity e = ecs.entity()
    23 .set<PortableType>({10, 20});
    24
    25 // Convert PortableType component to flecs expression string
    26 const PortableType& ptr = e.get<PortableType>();
    27 std::cout << ecs.to_expr(&ptr).c_str() << "\n"; // {i32: 10, inptr: 20}
    28}
    flecs::string to_expr(flecs::entity_t tid, const void *value)
    Convert a value to a string.
    Definition world.hpp:81
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    Entity.
    Definition entity.hpp:30
    const char * c_str() const
    Return the C string.
    Definition string.hpp:109
    The world.
    Definition world.hpp:129