sol2
sol2 copied to clipboard
[v4] Feature request: auto generate to_string meta function for usertype for all properties
For example, I have usertype like that:
sol::usertype<Transform> transform_type {
"localToParent", sol::property(&Transform::localToParent),
"localToWorld", sol::property(&Transform::localToWorld),
"worldToLocal", sol::property(&Transform::worldToLocal),
"object", sol::property(&Transform::object),
"position", sol::property(&Transform::position, &Transform::setPosition),
"facing", sol::writeonly_property([](Transform& tr, const glm::vec3& point){
tr.makeFacing(point);
}),
"localFacing", sol::writeonly_property([](Transform& tr, const glm::vec3& point){
tr.makeLocalFacing(point);
}),
};
SOL can generate in lua to_string
function that print something like this:
std::string Transform::to_string(std::string indent) const {
std::string indent = " ";
std::stringstream ss;
ss << "{\n";
ss << indent << " localToParent = " << to_string(localToParent()) << ",\n";
ss << indent << " localToWorld = " << to_string(localToWorld()) << ",\n";
ss << indent << " worldToLocal = " << to_string(worldToLocal()) << ",\n";
ss << indent << " object = " << to_string(object()) << ",\n";
ss << indent << " position = " << to_string(position) << ",\n";
ss << indent << "}";
return ss.str();
}
I'm working on this. (Slowly...)
I don't think there could be an implementation that fits everyone. At least, please, don't make it enabled by default, because:
- Some properties may require heavy calculations
- Automatically generated
to_string
may lead to infinite recursion (A A::get_child()
+A A::get_parent()
) - Nested indentation should be handled properly (a lot of allocations..)
- Table-like
to_string
may be broken i.e. for strings with internal \n - Member ordering?