add built-in support for custom visitation contexts#36
Conversation
follows plan in issue #34 building but still needs tests
|
this needs docu, then i'm probably happy to ship it |
|
This is perfect, thank you for taking your time and implementing this! I tried this branch with my Transform test and it works perfectly! I like that the struct SerializeContext {};
struct PrintContext {};
struct ScriptingContext {};
struct Transform {
private:
BEFRIEND_VISITABLE();
glm::vec3 m_position;
glm::vec3 m_scale;
glm::quat m_rotation;
};
VISITABLE_STRUCT(Transform, m_position, m_scale, m_rotation);
VISITABLE_STRUCT_IN_CONTEXT(SerializeContext, Transform, m_position);
VISITABLE_STRUCT_IN_CONTEXT(PrintContext, Transform, m_scale);
VISITABLE_STRUCT_IN_CONTEXT(ScriptingContext, Transform, m_rotation);
struct debug_printer {
template <typename T>
void operator()(const char* name, const T&) {
std::cout << name << std::endl;
}
};
//template <typename T, typename Context = void>
// Swapped Context and T so I don't have to specify T when calling print_properties
template <typename Context = void, typename T>
void print_properties(const T& obj) {
visit_struct::context<Context>::for_each(obj, debug_printer{});
}
int main() {
Transform transform;
print_properties(transform); // uses void, thus default macro, thus: m_position, m_scale, m_rotation
print_properties<SerializeContext>(transform); // m_position
print_properties<PrintContext>(transform); // m_scale
print_properties<ScriptingContext>(transform); // m_rotation
}I'm planning on using it a lot when making custom engines. Probably going to use it to differentiate between:
Limitations are endless. Thanks again for this feature and your time! |
Sven-vh
left a comment
There was a problem hiding this comment.
Like I said, ran my Transfrom test and everything works.
Tested with msvc on C++ 14.
Errors are also super nice. Misspelling or having a duplicate context give clear errors that are easy to debug/know what is wrong. Awesome job!
|
Thank you for testing! I still need to write some docs and maybe extend the tests, then will merge |
this avoids the complexity of introducing proxy objects (and handling copies of these) when one wants to visit the same object in multiple ways
follows plan in issue #34
building but still needs tests