I would like to pass a std::map
to chaiscript. However, I'm not sure how to do this. My code is currently as follows:
#include <map>
#include <string>
#include <chaiscript/chaiscript.hpp>
int main(int argc, char* argv[]) {
chaiscript::ChaiScript chai;
auto data = std::map<std::string, std::string>{
{ "key1", "val1"},
{ "key2", "val2"},
};
chai.add_global(chaiscript::var(&data), "data");
chai.eval(R"(
print(data["key1"]);
)");
}
However, this code crashes with an exception saying, that chaiscript doesn't know what to do with the bracket []
operator. How can I fix this?
I could tell chaiscript what the right function is, but I would prefer it, if the map is compatible with chaiscripts internal Map
type!
Update:
I found a bit in the documentation, which explains that the chaiscript map type supports arbitrary input. Looking at the code, this seems to be done by the Boxed_Value
type. However, this probably means that it is fundamentally impossible to directly insert std::map
into scripts.
I'm now thinking about either writing a custom type, or a conversion function to solve the problem. Keeping you posted...