Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

untyped refs #1598

Merged
merged 2 commits into from
Mar 12, 2025
Merged

untyped refs #1598

merged 2 commits into from
Mar 12, 2025

Conversation

jpeletier
Copy link
Contributor

This PR adds support for obtaining untyped refs in C++, useful to obtain type-erased refs or to obtain refs to runtime-components or components that are explored via reflection.

flecs::ref<T> now inherits from a common flecs::untyped_ref, so there should be less template code. This naming is similar to flecs::component<T> inheriting from flecs::untyped_component.

flecs::entity class also gets two new overloads of get_ref() that allow to get an untyped_ref if no type is specified at compile-time.

The change is completely backwards-compatible.

The following example illustrates how to use this new feature with runtime components:

    flecs::world world;

    // Create a runtime component:
    auto Position = world.component("Position")
        .member(flecs::I32, "x")
        .member(flecs::I32, "y");

    // Create a test entity:
    auto e = world.entity();

    // Add a component instance to the test entity:
    void* pos_ptr = e.ensure(Position);

    // Create a cursor to manipulate the runtime instance 
    // and set some values
    flecs::cursor cur(world, Position, pos_ptr);
    cur.push();
    cur.member("x");
    cur.set_int(10);
    cur.member("y");
    cur.set_int(20);
    cur.pop();

    // obtain a ref:
    flecs::untyped_ref ref = e.get_ref(Position);

    // Create a new cursor to read from the ref and
    // verify the values:
    cur = flecs::cursor(world, Position, ref.get());

    cur.push();
    cur.member("x");
    assert(cur.get_int() == 10);
    cur.member("y");
    assert(cur.get_int() == 20);
    cur.pop();

@SanderMertens
Copy link
Owner

LGTM, once conflicts are resolved I'll merge it

@jpeletier
Copy link
Contributor Author

Conflicts resolved 👍

@SanderMertens
Copy link
Owner

Awesome!

@SanderMertens SanderMertens merged commit dd7aa89 into SanderMertens:master Mar 12, 2025
73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants