Hacker News new | ask | show | jobs
by seanbax 1703 days ago
My favorite use is putting user-defined attributes on data members, and using reflection to generate a UI to manipulate those values. I do it with these shadertoys:

https://github.com/seanbaxter/shaders#reflection-and-attribu...

Just mark your declarations up with custom attributes:

    [[.imgui::range_float {  .1,  5 }]] float Zoom = 1.5;
    [[.imgui::range_float {   0,  1 }]] float Speed = .15;
    [[.imgui::range_float {  .1,  1 }]] float XScale = .3;
    [[.imgui::range_float {   0, .5 }]] float YScale = .2;
Then loop over the members with a meta for, and emit widget code that's guided by the attribute kind and data. These attributes each define a scrollbar.

The kind of data you reflect over will likely come from within the program.

2 comments

Nice! This is definitely my main usecase for reflecty things lately, along with serialization for the data (game engine scenarios). I've been doing static reflection + attributes through this: https://godbolt.org/z/enh8za4ja Which actually comes out to a not-so-bad syntax and the attributes can be read constexpr (some limitations though: simple aggregate-y structs, and all reflected fieds must appear before non-reflected ones).

Lately I've been working on a Go (pretty small subset -- no GC or concurrency) to C++ compiler and generating meta stuff as part of it. Mostly because I like how focused Go's syntax is and also it having an official parser and type checker in the standard library makes things pretty quick to get started.

I should play with Circle some time! It's definitely touching on a lot of the desired things from a metaprogramming layer.

In c++ there are some hacks that can start to help getting someswhere close (https://ossia.io/posts/reflection/) but this solution with attributes is what everyone dreams of