Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added template specialization in chaiscript::utility::add_class<Enum> to register bulk constants. #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 5, 2016

Conversation

vrennert
Copy link
Contributor

@vrennert vrennert commented Jan 2, 2016

I hope you will consider adding this addition to the helper, so people can use them exactly like the user-type / fun combination.

@vrennert
Copy link
Contributor Author

vrennert commented Jan 2, 2016

For some reason the build systems break but i don't see where and how it is related to my changes.

@lefticus
Copy link
Member

lefticus commented Jan 2, 2016

I agree that the build failures are not related to your code. Both travis and appveyor have been giving me a hard time lately.

That said though, can you please add an example for how to use this code with a small test, similar to this one:

TEST_CASE("Utility_Test utility class wrapper")
{
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
using namespace chaiscript;
/// \todo fix overload resolution for fun<>
chaiscript::utility::add_class<Utility_Test>(*m,
"Utility_Test",
{ constructor<Utility_Test ()>(),
constructor<Utility_Test (const Utility_Test &)>() },
{ {fun(&Utility_Test::function), "function"},
{fun(&Utility_Test::function2), "function2"},
{fun(&Utility_Test::function3), "function3"},
{fun(static_cast<std::string(Utility_Test::*)(double)>(&Utility_Test::functionOverload)), "functionOverload" },
{fun(static_cast<std::string(Utility_Test::*)(int)>(&Utility_Test::functionOverload)), "functionOverload" },
{fun(static_cast<Utility_Test & (Utility_Test::*)(const Utility_Test &)>(&Utility_Test::operator=)), "=" }
}
);
chaiscript::ChaiScript chai;
chai.add(m);
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
CHECK(chai.eval<std::string>("auto t2 = Utility_Test(); t2.functionOverload(1); ") == "int");
CHECK(chai.eval<std::string>("auto t3 = Utility_Test(); t3.functionOverload(1.1); ") == "double");
chai.eval("t = Utility_Test();");
}

Thanks

@vrennert
Copy link
Contributor Author

vrennert commented Jan 3, 2016

The script execution does work now with enums inside the shorthand vector and passing it to user defined functions, however for completeness we should look at why returning from the script does construct the correct vector but it seems to be empty in every case i have tried.

chai.eval<std::vector<Utility_Test_Numbers>>("return [ONE, TWO]");

I have also tried to separately boxed_cast the result which ends up throwing chaiscript::exception::bad_boxed_cast

auto intermediate = chai.eval("return [ONE, TWO]");
auto targetVec = chaiscript::boxed_cast<std::vector<Utility_Test_Numbers>>(intermediate);

@lefticus
Copy link
Member

lefticus commented Jan 4, 2016

There are two issues.

  1. return should only be valid for returning from functions. I'm surprised that code is working at all for you. Instead just do chai.eval("[ONE, TWO"]) to return the value back into C++. (The return value of any statement is the last value evaluated).
  2. There is a memory error related to object lifetimes that I have fixed on a separate branch. I've just merged that branch into my copy of this branch. I've submitted a new pull request back to you to try.

lefticus added a commit that referenced this pull request Jan 5, 2016
Added template specialization in chaiscript::utility::add_class<Enum> to register bulk constants.
@lefticus lefticus merged commit 21048b9 into ChaiScript:develop Jan 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants