Ok so I ran into a problem when implementing a c# property like system in c++ (see: https://mcmap.net/q/16573/-c-like-properties-in-native-c).
Consider the following example:
struct TransformCmp
{
PropertyDelGetSet<vec3> position =
PropertyDelGetSet<vec3>(
[&]() -> const vec3& { return m_position; },
[&](const vec3& val) { m_position = val; m_dirty = true; });
private:
bool m_dirty = true;
vec3 m_position = vec3(0);
}
If/when the instance of the TransformCmp is copied/moved (e.g. if it was stored in std::vector
and resize was called), the reference capture is now invalid.
The question is how do I make sure when the copy/move happens that I also update the reference capture?
I've tried implementing a copy constructor for the Property classes but I ran into the same issue, probably because I didn't do it correctly. Any ideas?
Update:
I'm trying to do a similar idea to the functors Matthias Grün suggested, basically I'm passing a TransformCmp pointer to the PropertyDelGetSet constructor, which will be passed on the get-set functions.
When initializing the property I'm doing something like this:
PropertyDelGetSet<TransformCmp, vec3> position =
PropertyDelGetSet<TransformCmp, vec3>(this, // <- now passing this
[](TransformCmp* p) -> const vec3& { return p->m_position; },
[](TransformCmp* p, const vec3& val) { p->m_position = val; p->m_dirty = false; });
However, I need to be able to update the pointer stored in PropertyDelGetSet to make this work.