visit_struct icon indicating copy to clipboard operation
visit_struct copied to clipboard

Feature request: optional visitable metadata

Open NikolausDemmel opened this issue 6 years ago • 2 comments

I'm using visit_struct to declare structs with parameters and automatically link specific option values to GUI widgets. It works great for simple cases.

However, more often than not I would like to define additional metadata where I specify the options in the struct. For example for numeric options, I would like to specify a range with minimum and maximum acceptable value, such that a slider with the correct limits can be created in the GUI.

I'm currently working around this by using some custom types for the struct member variables, e.g.

template <class T>
struct ranged {
  using ValueType = T;
  T val {0};
  const T min {0};
  const T max {100};
};

struct Foo {

    BEGIN_VISITABLES(Foo);

    VISITABLE_INIT(bool, foo, true);
    VISITABLE_DIRECT_INIT(ranged<double>, bar, {0.5, 0, 1});
    END_VISITABLES;

};

However this is a bit awkward, as a I have to access the actual value always via the val member.

Foo foo;
foo.bar.val = 0.1;

Would it be possible to extend visit_struct to allow declaring additional (typed) meta-data for every member in such a way, that it is transparent (i.e. foo.bar refers to the actual value, not some enclosing struct containing also the metadata), and it is (optionally) accessible during visitation.

I guess for my use-cases I so far only need constant meta-data.

I'm mostly interested in the intrusive syntax, since I want to define "all in one place", but one might also consider adding metadata to existing structs with a non-intrusive syntax.

NikolausDemmel avatar Feb 27 '19 20:02 NikolausDemmel

i will think about this, sorry i didn't respond already

cbeck88 avatar Mar 17 '20 03:03 cbeck88

No worries.

In the meantime I am using a customized solution specific to my application. As it stands it's not very general, but I'm quite happy with it.

Just for some inspiration, the syntax looks something like this:

struct FooOptions : public VisitableOptions<FooOptions> {

  VISITABLE_OPTIONS_DEFAULT_META(readonly());

  BEGIN_VISITABLES(FooOptions);

  VISITABLE_META(int, bar, init(3).range(1, 100).help("foos the bar"));
  VISITABLE_META(BazEnum, baz, init(BazEnum::Baz1));
  VISITABLE_META(double, quux, init(1e2).range(1e-10, 1e10).logscale());
  VISITABLE_META_DEFAULT(std::string, quax));
  VISITABLE_META(QuuzFlags, quuz_flags, init(QuuzFlag::Quuz23 | QuuzFlag::Quuz42));
  VISITABLE_META(bool, corge, init(false).readonly(false));
  VISITABLE_META(std::vector<int>, grault, init({42, 23, 7}).readonly(false));

  END_VISITABLES;
}

NikolausDemmel avatar Mar 20 '20 00:03 NikolausDemmel