These are chat archives for ChaiScript/ChaiScript

23rd
Nov 2016
StanEpp
@StanEpp
Nov 23 2016 13:11

@lefticus I am currently trying to pass an enum class to chaiscript. I have seen this discussion here exporting enums and looked at your example. Unfortunately this only works for enums but not for enum classes when I'm correct. I looked at the overloaded helper function add_class in utility.hpp and added a new overload for enum classes:

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;
  t_module.add([]() {
    // add some comparison and assignment operators
    return assign<EnumClass>(not_equal<EnumClass>(equal<EnumClass>()));
  }());

  t_module.add(chaiscript::fun([](const EnumClass &e1, const EnumClass &e2) { return e1 == e2; }), "==");

  for (const auto &constant : t_constants)
  {
    t_module.add_global_const(chaiscript::const_var(EnumClass(constant.first)), constant.second);
  }
}
}

Now I can easily do something like this:

chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
chaiscript::utility::add_class<TerrainType>(*m, 
  "TerrainType", 
  { { TerrainType::SOIL, "SOIL" },
    {TerrainType::PASSABLE, "PASSABLE"},
    { TerrainType::STONE, "STONE" }
  }
);

Have I missed there something? Maybe another way to pass an enum class to chaiscript?

Jason Turner
@lefticus
Nov 23 2016 14:58
@StanEpp I'm curious how your overload differs from the one that's already there: https://github.com/ChaiScript/ChaiScript/blob/develop/include/chaiscript/utility/utility.hpp#L67-L91
StanEpp
@StanEpp
Nov 23 2016 15:13
enum classes cannot be implicitly converted to their underlying type. That was the reason why I couldn't use the TerrainType declaration above. Only if I explicitly cast my enum values to the underlying type the already existent overload would work, i.e.:
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
chaiscript::utility::add_class<TerrainType>(*m, 
  "TerrainType", 
  { { static_cast<uint32_t>(TerrainType::SOIL), "SOIL" },
    {static_cast<uint32_t>(TerrainType::PASSABLE), "PASSABLE"},
    { static_cast<uint32_t>(TerrainType::STONE), "STONE" }
  }
);
Another problem was the definition of the == operator on the underlying type in these lines: https://github.com/ChaiScript/ChaiScript/blob/develop/include/chaiscript/utility/utility.hpp#L84-L85
The user needs to define this operator beforehand, although it might not be desirable.
Jason Turner
@lefticus
Nov 23 2016 15:26
Ah, I see, the underlying_type part, go ahead and submit a patch, if you don't mind
StanEpp
@StanEpp
Nov 23 2016 15:31
I'll gladly try to submit a patch by the end of the day
Jason Turner
@lefticus
Nov 23 2016 15:31
thanks
if for some reason you cannot, then open an issue at least on github so I can look at it at some point, but patch is preferred
Jason Turner
@lefticus
Nov 23 2016 15:37
patch / pull request I mean
StanEpp
@StanEpp
Nov 23 2016 15:39
Okay, either a pull request or an issue