rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.17k stars 439 forks source link

In-place, copy-free getting/setting of properties #275

Closed iridinite closed 4 years ago

iridinite commented 4 years ago

Hi,

I'd like to use RTTR for implementation of a binary (de)serialization tool. In essence, what I want to do, is this: given any arbitrary rttr::instance of some rttr::type, I want to obtain a list of pointers to every single instance member of type Foo (recursively). So all Foos declared as class members, or contained in an array, or included in a base class, etc.

I tried doing this by getting the properties of the type, and recursively iterating over those if they also have members, and this works in theory. However the style of copying property values to a by-value variant does not work for me, because I need the pointer to persist longer than the rttr::variant, and point to the actual original object (or subobject). The pointer to Foo I'd extract would then point to the variant on the stack (which will be destroyed), not the real object.

The property policies do not work for me because I need this to apply to all instances of Foo, specifically also including those in sequential or associative containers. (Getting the value of a variant_sequential_view will, again, make a copy into a variant.)

Is it possible to obtain a raw pointer or reference to a property given an rttr::instance, without copying it to a rttr::variant?

bruceauyeung commented 4 years ago

@iridinite this is definitely the feature what i looking for! if this feature is not implemented yet, is there any workaround ? @acki-m

iridinite commented 4 years ago

@bruceauyeung Unfortunately I wasn't able to find one. I ultimately ended up dropping this library, because it didn't quite cover my use cases, and rolled my own custom solution.

bruceauyeung commented 4 years ago

@iridinite registering property with policy policy::prop::bind_as_ptr can solve the problem.

iridinite commented 4 years ago

@bruceauyeung That doesn't solve the issue for me, because I need to be able to find all pointers to Foo, including those in array-like or map-like containers, and sequential views always return a variant with a copy of the object. (The array itself would be bound as pointer, but not its contents.) Thanks for the suggestion though!