Merge pull request #1 from ChaiScript/feature_enum_utility_helper
Feature enum utility helper
This commit is contained in:
commit
b6287a194c
@ -15,6 +15,7 @@
|
||||
#include "../chaiscript.hpp"
|
||||
#include "../dispatchkit/proxy_functions.hpp"
|
||||
#include "../dispatchkit/type_info.hpp"
|
||||
#include "../dispatchkit/operators.hpp"
|
||||
|
||||
|
||||
namespace chaiscript
|
||||
@ -63,19 +64,30 @@ namespace chaiscript
|
||||
t_module.add(fun.first, fun.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename Enum, typename ModuleType>
|
||||
typename std::enable_if<std::is_enum<Enum>::value, void>::type
|
||||
add_class(ModuleType &t_module,
|
||||
const std::string &t_class_name,
|
||||
const std::vector<chaiscript::Proxy_Function> &t_constructors,
|
||||
const std::vector<std::pair<chaiscript::Boxed_Value, std::string>> &t_constants)
|
||||
const std::vector<std::pair<typename std::underlying_type<Enum>::type, std::string>> &t_constants)
|
||||
{
|
||||
t_module.add(chaiscript::user_type<Enum>(), t_class_name);
|
||||
|
||||
|
||||
t_module.add(chaiscript::constructor<Enum ()>(), t_class_name);
|
||||
t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
|
||||
|
||||
t_module.add([](){
|
||||
// add some comparison and assignment operators
|
||||
using namespace chaiscript::bootstrap::operators;
|
||||
return assign<Enum>(not_equal<Enum>(equal<Enum>()));
|
||||
}());
|
||||
|
||||
t_module.add(chaiscript::fun([](const Enum &e, const typename std::underlying_type<Enum>::type &i) { return e == i; }), "==");
|
||||
t_module.add(chaiscript::fun([](const typename std::underlying_type<Enum>::type &i, const Enum &e) { return i == e; }), "==");
|
||||
|
||||
for (const auto &constant : t_constants)
|
||||
{
|
||||
t_module.add_global_const(constant.first, constant.second);
|
||||
t_module.add_global_const(chaiscript::const_var(Enum(constant.first)), constant.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
// caught in other cpp files if chaiscript causes them
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(push)
|
||||
@ -517,14 +518,19 @@ TEST_CASE("Utility_Test utility class wrapper")
|
||||
|
||||
}
|
||||
|
||||
|
||||
enum Utility_Test_Numbers
|
||||
{
|
||||
ONE,
|
||||
ONE,
|
||||
TWO,
|
||||
THREE
|
||||
};
|
||||
|
||||
TEST_CASE("Utility_Test utility class wrapper")
|
||||
void do_something_with_enum_vector(const std::vector<Utility_Test_Numbers> &)
|
||||
{
|
||||
}
|
||||
|
||||
TEST_CASE("Utility_Test utility class wrapper for enum")
|
||||
{
|
||||
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
@ -533,12 +539,10 @@ TEST_CASE("Utility_Test utility class wrapper")
|
||||
|
||||
chaiscript::utility::add_class<Utility_Test_Numbers>(*m,
|
||||
"Utility_Test_Numbers",
|
||||
{
|
||||
},
|
||||
{ { const_var(ONE), "ONE" },
|
||||
{ const_var(TWO), "TWO" },
|
||||
{ const_var(THREE), "THREE" }
|
||||
|
||||
{ { ONE, "ONE" },
|
||||
{ TWO, "TWO" },
|
||||
{ THREE, "THREE" }
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
@ -550,6 +554,19 @@ TEST_CASE("Utility_Test utility class wrapper")
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("TWO ") == 1);
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("THREE ") == 2);
|
||||
|
||||
CHECK(chai.eval<bool>("ONE == 0"));
|
||||
|
||||
chai.add(chaiscript::fun(&do_something_with_enum_vector), "do_something_with_enum_vector");
|
||||
chai.add(chaiscript::vector_conversion<std::vector<Utility_Test_Numbers>>());
|
||||
CHECK_NOTHROW(chai.eval("var a = [ONE, TWO, THREE]"));
|
||||
CHECK_NOTHROW(chai.eval("do_something_with_enum_vector([ONE])"));
|
||||
CHECK_NOTHROW(chai.eval("[ONE]"));
|
||||
|
||||
CHECK(chai.eval<bool>("ONE == ONE"));
|
||||
CHECK(chai.eval<bool>("ONE != TWO"));
|
||||
CHECK_NOTHROW(chai.eval("var o = ONE; o = TWO"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user