Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dump User defined c++ types, functions from c++ code #1664

Open
sanjuchopracool opened this issue Jan 14, 2025 · 0 comments
Open

How to dump User defined c++ types, functions from c++ code #1664

sanjuchopracool opened this issue Jan 14, 2025 · 0 comments

Comments

@sanjuchopracool
Copy link

sanjuchopracool commented Jan 14, 2025

I was trying to dump all the User defined functions, variables and types. This following program get struck in endless loop.

#include <fmt/format.h>
#include <sol/sol.hpp>

template<typename T>
void helper(const T &table, int indent)
{
    if constexpr (std::is_same_v<T, sol::table>) {
        if (!table.valid()) {
            return;
        }
    }

    for (auto &element : table) {
        const sol::object &key = element.first;
        const sol::object &value = element.second;
        std::string k = key.as<std::string>();
        sol::type v_t = value.get_type();
        sol::type k_t = value.get_type();

        std::string filler;
        filler.resize(indent, ' ');
        switch (v_t) {
        case sol::type::string:
            std::cout << fmt::format("{}{} : {}", filler, k, value.as<std::string>());
            break;
        case sol::type::number:
            std::cout << fmt::format("{}{} : {}", filler, k, value.as<double>());
            break;
        case sol::type::userdata: {
            {
                std::cout << fmt::format("User Data: {}{}", filler, k);
            }
        } break;
        case sol::type::table: {
            std::cout << fmt::format("Table Data: {}{}", filler, k);
            const sol::optional<sol::table> &t2 = table[k];
            if (t2) {
                helper(t2.value(), indent + 3);
            }
        } break;
        default:
            // std::cout << fmt::format("{}{} : Unknown Type", filler, k);
            break;
        }
    }
}

dump_state(const sol::state &state)
{
    helper<sol::state>(state, 0);
}

How can I identify

  1. user defined function and print
  2. user defined variables and print
  3. user defined c++ types with the properties and functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant