From a87147a12dc726501c2e63ef2fc6473f7d1b9676 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Thu, 14 Sep 2017 17:41:11 +0200 Subject: [PATCH] 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: