sol2
sol2 copied to clipboard
How to dump User defined c++ types, functions from c++ code
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
- user defined function and print
- user defined variables and print
- user defined c++ types with the properties and functions