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_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([](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([](String *s) { s->clear(); } ), "clear");
m.add(fun([](const String *s) { return s->empty(); } ), "empty"); m.add(fun([](const String *s) { return s->empty(); } ), "empty");
m.add(fun([](const String *s) { return s->size(); } ), "size"); m.add(fun([](const String *s) { return s->size(); } ), "size");

View File

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

View File

@ -51,11 +51,9 @@ struct System
void do_callbacks(const std::string &inp) void do_callbacks(const std::string &inp)
{ {
log("Running Callbacks: " + inp); log("Running Callbacks: " + inp);
for (std::map<std::string, std::function<std::string (const std::string &)> >::iterator itr = m_callbacks.begin(); for (auto & m_callback : m_callbacks)
itr != m_callbacks.end();
++itr)
{ {
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 // 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 // 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 // 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 // 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 // access to it both from within chaiscript and from C++ code
system.do_callbacks("TestString"); 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 // 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 // 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. // then cast your function to that typedef.
typedef void (*PlainLog)(const std::string &); using PlainLog = void (*)(const std::string &);
typedef void (*ModuleLog)(const std::string &, const std::string &); using ModuleLog = void (*)(const std::string &, const std::string &);
chai.add(fun(PlainLog(&log)), "log"); chai.add(fun(PlainLog(&log)), "log");
chai.add(fun(ModuleLog(&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() // 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 //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 //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 //Call bound version of do_callbacks
chai("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(); caller();
@ -134,7 +134,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
std::cout << "scripti: " << scripti << '\n'; std::cout << "scripti: " << scripti << '\n';
scripti *= 2; scripti *= 2;
std::cout << "scripti (updated): " << scripti << '\n'; 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 //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()); log("Functor test output", ss.str());
chai.add(var(std::shared_ptr<int>()), "nullvar"); 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 // test the global const action
chai.add_global_const(const_var(1), "constvar"); 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 // 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"); chai.add(fun(&bound_log, std::string("Msg")), "BoundFun");

View File

@ -122,7 +122,7 @@ int main()
assert(myderived.getValue() == "1234"); 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"); assert(myderived.getValue() == "new");
// call the other derived method via chaiscript and return the value to c++ land: // call the other derived method via chaiscript and return the value to c++ land: