- Compiler Used: MSVC 2019
- Operating System: Windows 10
- Architecture (ARM/x86/32bit/64bit/etc): x86 32-bit
I want to add an enum class to ChaiScript, for this I use chaiscript::utility::add_class.
My first attempt is to simply pass a reference to the chaiscript::ChaiScript class instance as the first argument, like this:
chaiscript::ChaiScript chai;
chaiscript::utility::add_class<MyEnumClass>(chai, "MyEnumClass", {
{MyEnumClass::Apple, "MyEnumClass_Apple"},
{MyEnumClass::Banana, "MyEnumClass_Banana"},
{MyEnumClass::Pear, "MyEnumClass_Pear"}});
However, in the latest version 6.1, this results to a compile error:

Indeed, if we take a look inside add_class:
|
template<typename EnumClass, typename ModuleType> |
|
typename std::enable_if<std::is_enum<EnumClass>::value, void>::type |
|
add_class(ModuleType &t_module, const std::string &t_class_name, const std::vector<std::pair<EnumClass, std::string>> &t_constants) { |
|
t_module.add(chaiscript::user_type<EnumClass>(), t_class_name); |
|
|
|
t_module.add(chaiscript::constructor<EnumClass()>(), t_class_name); |
|
t_module.add(chaiscript::constructor<EnumClass(const EnumClass &)>(), t_class_name); |
|
|
|
using namespace chaiscript::bootstrap::operators; |
|
equal<EnumClass>(t_module); |
|
not_equal<EnumClass>(t_module); |
|
assign<EnumClass>(t_module); |
|
|
|
for (const auto &constant : t_constants) { |
|
t_module.add_global_const(chaiscript::const_var(EnumClass(constant.first)), constant.second); |
|
} |
|
} |
This is because when calling
equal,
not_equal and
assign methods from the namespace
chaiscript::bootstrap::operators, the first argument
t_module is required to be of type
Module, since the first argument of
equal etc. is of type
Module&:
|
template<typename T> |
|
void equal(Module &m) { |
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs) { return lhs == rhs; }), "=="); |
|
} |
I know that one solution is to create a Module instance, use add_class on the Module instance, and then add the Module instance to the ChaiScript instance. However this feels like additional steps that don't seem to be necessary, especially when the add_class variant for non enum types works with the first argument being a ChaiScript reference fine. Since there is a template argument for the first argument, I do feel that there was intention to make it work with a ChaiScript reference, and maybe other types, though as for enum classes it only works with Module.
I think a good fix would be to give to all functions in chaiscript::bootstrap::operators a second template argument for the first parameter, so they can accept any other type than Module, like this:
template<typename T, typename M>
void equal(M &m) {
m.add(chaiscript::fun([](const T &lhs, const T &rhs) { return lhs == rhs; }), "==");
}
So when giving M=chaiscript::ChaiScript it should still work as it has the add method, and it will also make add_class work with ChaiScript as first parameter type at the same time. There might be other solutions I don't know, but I think it would be nice if I could just add enums directly to the ChaiScript without having to pass through a Module.
PS: Btw great project! Integrating it to my program was surprisingly very fast, but it does lack a lot of documentation and hasn't been updated very much...
I want to add an enum class to ChaiScript, for this I use
chaiscript::utility::add_class.My first attempt is to simply pass a reference to the
chaiscript::ChaiScriptclass instance as the first argument, like this:However, in the latest version 6.1, this results to a compile error:

Indeed, if we take a look inside
add_class:ChaiScript/include/chaiscript/utility/utility.hpp
Lines 85 to 101 in 2898ae6
This is because when calling
equal,not_equalandassignmethods from the namespacechaiscript::bootstrap::operators, the first argumentt_moduleis required to be of typeModule, since the first argument ofequaletc. is of typeModule&:ChaiScript/include/chaiscript/dispatchkit/operators.hpp
Lines 83 to 86 in 2898ae6
I know that one solution is to create a
Moduleinstance, useadd_classon theModuleinstance, and then add theModuleinstance to theChaiScriptinstance. However this feels like additional steps that don't seem to be necessary, especially when theadd_classvariant for non enum types works with the first argument being aChaiScriptreference fine. Since there is a template argument for the first argument, I do feel that there was intention to make it work with aChaiScriptreference, and maybe other types, though as for enum classes it only works withModule.I think a good fix would be to give to all functions in
chaiscript::bootstrap::operatorsa second template argument for the first parameter, so they can accept any other type thanModule, like this:So when giving M=
chaiscript::ChaiScriptit should still work as it has theaddmethod, and it will also makeadd_classwork withChaiScriptas first parameter type at the same time. There might be other solutions I don't know, but I think it would be nice if I could just add enums directly to theChaiScriptwithout having to pass through aModule.PS: Btw great project! Integrating it to my program was surprisingly very fast, but it does lack a lot of documentation and hasn't been updated very much...