From a87147a12dc726501c2e63ef2fc6473f7d1b9676 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Thu, 14 Sep 2017 17:41:11 +0200 Subject: [PATCH 1/4] Upgrade samples where it improves readability --- samples/example.cpp | 28 ++++++++++++++-------------- samples/inheritance.cpp | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/example.cpp b/samples/example.cpp index 9a0f0070..4810a127 100644 --- a/samples/example.cpp +++ b/samples/example.cpp @@ -51,11 +51,9 @@ struct System void do_callbacks(const std::string &inp) { log("Running Callbacks: " + inp); - for (std::map >::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 caller = chai.eval >("fun() { system.do_callbacks(\"From Functor\"); }"); + std::function caller = chai.eval >( + 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()), "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"); diff --git a/samples/inheritance.cpp b/samples/inheritance.cpp index aba619a4..1c9ddb63 100644 --- a/samples/inheritance.cpp +++ b/samples/inheritance.cpp @@ -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: From 0fa0def11270d9403f6fc83a946af31629d7c6ac Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Mon, 18 Sep 2017 15:00:52 +0200 Subject: [PATCH 2/4] Use range-based for --- include/chaiscript/dispatchkit/dispatchkit.hpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index d8bfb25b..2e71e543 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -1143,24 +1143,17 @@ namespace chaiscript { std::cout << "Registered Types: \n"; std::vector > types = get_types(); - for (std::vector >::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 > funcs = get_functions(); std::cout << "Functions: \n"; - for (std::vector >::const_iterator itr = funcs.begin(); - itr != funcs.end(); - ++itr) + for (auto const &func: get_functions()) { - dump_function(*itr); + dump_function(func); } std::cout << '\n'; } From 3e521d29522016e75960ba7da3af71c652c0675b Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Mon, 18 Sep 2017 17:00:04 +0200 Subject: [PATCH 3/4] Delete now useless local copy --- include/chaiscript/dispatchkit/dispatchkit.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 2e71e543..ae729819 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -1142,7 +1142,6 @@ namespace chaiscript void dump_system() const { std::cout << "Registered Types: \n"; - std::vector > types = get_types(); for (auto const &type: get_types()) { std::cout << type.first << ": " << type.second.bare_name() << '\n'; From c6237cc528c2d378dca9f74d8d455a88756b1fad Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 21 Sep 2017 08:55:32 -0600 Subject: [PATCH 4/4] Add `+= char` for string type --- include/chaiscript/dispatchkit/bootstrap_stl.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 7f409a3a..7785f9fb 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -706,6 +706,8 @@ namespace chaiscript m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_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([](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");