Skip to content

Commit a87147a

Browse files
committed
Upgrade samples where it improves readability
1 parent 2662395 commit a87147a

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

samples/example.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ struct System
5151
void do_callbacks(const std::string &inp)
5252
{
5353
log("Running Callbacks: " + inp);
54-
for (std::map<std::string, std::function<std::string (const std::string &)> >::iterator itr = m_callbacks.begin();
55-
itr != m_callbacks.end();
56-
++itr)
54+
for (auto & m_callback : m_callbacks)
5755
{
58-
log("Callback: " + itr->first, itr->second(inp));
56+
log("Callback: " + m_callback.first, m_callback.second(inp));
5957
}
6058
}
6159
};
@@ -88,25 +86,25 @@ int main(int /*argc*/, char * /*argv*/[]) {
8886
// The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application
8987
// in the "add_callback" function of struct System the chaiscript function is converted into a
9088
// std::function, so it can be handled and called easily and type-safely
91-
chai.eval("system.add_callback(\"#1\", fun(x) { \"Callback1 \" + x });");
89+
chai.eval(R"(system.add_callback("#1", fun(x) { "Callback1 " + x });)");
9290

9391
// Because we are sharing the "system" object with the chaiscript engine we have equal
9492
// access to it both from within chaiscript and from C++ code
9593
system.do_callbacks("TestString");
96-
chai.eval("system.do_callbacks(\"TestString\");");
94+
chai.eval(R"(system.do_callbacks("TestString");)");
9795

9896
// The log function is overloaded, therefore we have to give the C++ compiler a hint as to which
9997
// version we want to register. One way to do this is to create a typedef of the function pointer
10098
// then cast your function to that typedef.
101-
typedef void (*PlainLog)(const std::string &);
102-
typedef void (*ModuleLog)(const std::string &, const std::string &);
99+
using PlainLog = void (*)(const std::string &);
100+
using ModuleLog = void (*)(const std::string &, const std::string &);
103101
chai.add(fun(PlainLog(&log)), "log");
104102
chai.add(fun(ModuleLog(&log)), "log");
105103

106-
chai.eval("log(\"Test Message\")");
104+
chai.eval(R"(log("Test Message"))");
107105

108106
// A shortcut to using eval is just to use the chai operator()
109-
chai("log(\"Test Module\", \"Test Message\");");
107+
chai(R"(log("Test Module", "Test Message");)");
110108

111109
//Finally, it is possible to register a lambda as a system function, in this
112110
//way, we can, for instance add a bound member function to the system
@@ -115,7 +113,9 @@ int main(int /*argc*/, char * /*argv*/[]) {
115113
//Call bound version of do_callbacks
116114
chai("do_callbacks()");
117115

118-
std::function<void ()> caller = chai.eval<std::function<void ()> >("fun() { system.do_callbacks(\"From Functor\"); }");
116+
std::function<void ()> caller = chai.eval<std::function<void ()> >(
117+
R"(fun() { system.do_callbacks("From Functor"); })"
118+
);
119119
caller();
120120

121121

@@ -134,7 +134,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
134134
std::cout << "scripti: " << scripti << '\n';
135135
scripti *= 2;
136136
std::cout << "scripti (updated): " << scripti << '\n';
137-
chai("print(\"Scripti from chai: \" + to_string(scripti))");
137+
chai(R"(print("Scripti from chai: " + to_string(scripti)))");
138138

139139
//To do: Add examples of handling Boxed_Values directly when needed
140140

@@ -146,7 +146,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
146146
log("Functor test output", ss.str());
147147

148148
chai.add(var(std::shared_ptr<int>()), "nullvar");
149-
chai("print(\"This should be true.\"); print(nullvar.is_var_null())");
149+
chai(R"(print("This should be true."); print(nullvar.is_var_null()))");
150150

151151
// test the global const action
152152
chai.add_global_const(const_var(1), "constvar");
@@ -160,7 +160,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
160160

161161

162162
// Test ability to register a function that excepts a shared_ptr version of a type
163-
chai("take_shared_ptr(\"Hello World as a shared_ptr\");");
163+
chai(R"(take_shared_ptr("Hello World as a shared_ptr");)");
164164

165165
chai.add(fun(&bound_log, std::string("Msg")), "BoundFun");
166166

samples/inheritance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ int main()
122122
assert(myderived.getValue() == "1234");
123123

124124

125-
chai.eval("myderived.setValue(\"new\")"); // set the value via chaiscript
125+
chai.eval(R"(myderived.setValue("new"))"); // set the value via chaiscript
126126
assert(myderived.getValue() == "new");
127127

128128
// call the other derived method via chaiscript and return the value to c++ land:

0 commit comments

Comments
 (0)