Merge branch 'develop' of github.com:ChaiScript/ChaiScript into develop

This commit is contained in:
Jason Turner 2017-10-02 09:53:23 -06:00
commit be225a9209
4 changed files with 21 additions and 27 deletions

View File

@ -707,6 +707,8 @@ namespace chaiscript
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
m.add(fun([](String *s, typename String::value_type c) -> decltype(auto) { return (*s += c); } ), "+=");
m.add(fun([](String *s) { s->clear(); } ), "clear");
m.add(fun([](const String *s) { return s->empty(); } ), "empty");
m.add(fun([](const String *s) { return s->size(); } ), "size");

View File

@ -1142,25 +1142,17 @@ namespace chaiscript
void dump_system() const
{
std::cout << "Registered Types: \n";
std::vector<std::pair<std::string, Type_Info> > types = get_types();
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
itr != types.end();
++itr)
for (auto const &type: get_types())
{
std::cout << itr->first << ": ";
std::cout << itr->second.bare_name();
std::cout << '\n';
std::cout << type.first << ": " << type.second.bare_name() << '\n';
}
std::cout << '\n';
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();
std::cout << "Functions: \n";
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
for (auto const &func: get_functions())
{
dump_function(*itr);
dump_function(func);
}
std::cout << '\n';
}

View File

@ -51,11 +51,9 @@ struct System
void do_callbacks(const std::string &inp)
{
log("Running Callbacks: " + inp);
for (std::map<std::string, std::function<std::string (const std::string &)> >::iterator itr = m_callbacks.begin();
itr != m_callbacks.end();
++itr)
for (auto & m_callback : m_callbacks)
{
log("Callback: " + itr->first, itr->second(inp));
log("Callback: " + m_callback.first, m_callback.second(inp));
}
}
};
@ -88,25 +86,25 @@ int main(int /*argc*/, char * /*argv*/[]) {
// The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application
// in the "add_callback" function of struct System the chaiscript function is converted into a
// std::function, so it can be handled and called easily and type-safely
chai.eval("system.add_callback(\"#1\", fun(x) { \"Callback1 \" + x });");
chai.eval(R"(system.add_callback("#1", fun(x) { "Callback1 " + x });)");
// Because we are sharing the "system" object with the chaiscript engine we have equal
// access to it both from within chaiscript and from C++ code
system.do_callbacks("TestString");
chai.eval("system.do_callbacks(\"TestString\");");
chai.eval(R"(system.do_callbacks("TestString");)");
// The log function is overloaded, therefore we have to give the C++ compiler a hint as to which
// version we want to register. One way to do this is to create a typedef of the function pointer
// then cast your function to that typedef.
typedef void (*PlainLog)(const std::string &);
typedef void (*ModuleLog)(const std::string &, const std::string &);
using PlainLog = void (*)(const std::string &);
using ModuleLog = void (*)(const std::string &, const std::string &);
chai.add(fun(PlainLog(&log)), "log");
chai.add(fun(ModuleLog(&log)), "log");
chai.eval("log(\"Test Message\")");
chai.eval(R"(log("Test Message"))");
// A shortcut to using eval is just to use the chai operator()
chai("log(\"Test Module\", \"Test Message\");");
chai(R"(log("Test Module", "Test Message");)");
//Finally, it is possible to register a lambda as a system function, in this
//way, we can, for instance add a bound member function to the system
@ -115,7 +113,9 @@ int main(int /*argc*/, char * /*argv*/[]) {
//Call bound version of do_callbacks
chai("do_callbacks()");
std::function<void ()> caller = chai.eval<std::function<void ()> >("fun() { system.do_callbacks(\"From Functor\"); }");
std::function<void ()> caller = chai.eval<std::function<void ()> >(
R"(fun() { system.do_callbacks("From Functor"); })"
);
caller();
@ -134,7 +134,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
std::cout << "scripti: " << scripti << '\n';
scripti *= 2;
std::cout << "scripti (updated): " << scripti << '\n';
chai("print(\"Scripti from chai: \" + to_string(scripti))");
chai(R"(print("Scripti from chai: " + to_string(scripti)))");
//To do: Add examples of handling Boxed_Values directly when needed
@ -146,7 +146,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
log("Functor test output", ss.str());
chai.add(var(std::shared_ptr<int>()), "nullvar");
chai("print(\"This should be true.\"); print(nullvar.is_var_null())");
chai(R"(print("This should be true."); print(nullvar.is_var_null()))");
// test the global const action
chai.add_global_const(const_var(1), "constvar");
@ -160,7 +160,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
// Test ability to register a function that excepts a shared_ptr version of a type
chai("take_shared_ptr(\"Hello World as a shared_ptr\");");
chai(R"(take_shared_ptr("Hello World as a shared_ptr");)");
chai.add(fun(&bound_log, std::string("Msg")), "BoundFun");

View File

@ -122,7 +122,7 @@ int main()
assert(myderived.getValue() == "1234");
chai.eval("myderived.setValue(\"new\")"); // set the value via chaiscript
chai.eval(R"(myderived.setValue("new"))"); // set the value via chaiscript
assert(myderived.getValue() == "new");
// call the other derived method via chaiscript and return the value to c++ land: