sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

[v4] Feature request: auto generate to_string meta function for usertype for all properties

Open KindDragon opened this issue 4 years ago • 2 comments

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();
}

KindDragon avatar Aug 04 '20 21:08 KindDragon

I'm working on this. (Slowly...)

ThePhD avatar Sep 26 '20 10:09 ThePhD

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?

Smertig avatar Oct 24 '21 06:10 Smertig