MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / main

Function main

samples/example.cpp:66–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66int main(int /*argc*/, char * /*argv*/[]) {
67 using namespace chaiscript;
68
69 ChaiScript chai;
70
71
72 //Create a new system object and share it with the chaiscript engine
73 System system;
74 chai.add_global(var(&system), "system");
75
76 //Add a bound callback method
77 chai.add(fun(&System::add_callback, std::ref(system)), "add_callback_bound");
78
79 //Register the two methods of the System structure.
80 chai.add(fun(&System::add_callback), "add_callback");
81 chai.add(fun(&System::do_callbacks), "do_callbacks");
82
83 chai.add(fun(&take_shared_ptr), "take_shared_ptr");
84
85 // Let's use chaiscript to add a new lambda callback to our system.
86 // The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application
87 // in the "add_callback" function of struct System the chaiscript function is converted into a
88 // std::function, so it can be handled and called easily and type-safely
89 chai.eval(R"(system.add_callback("#1", fun(x) { "Callback1 " + x });)");
90
91 // Because we are sharing the "system" object with the chaiscript engine we have equal
92 // access to it both from within chaiscript and from C++ code
93 system.do_callbacks("TestString");
94 chai.eval(R"(system.do_callbacks("TestString");)");
95
96 // The log function is overloaded, therefore we have to give the C++ compiler a hint as to which
97 // version we want to register. One way to do this is to create a typedef of the function pointer
98 // then cast your function to that typedef.
99 using PlainLog = void (*)(const std::string &);
100 using ModuleLog = void (*)(const std::string &, const std::string &);
101 chai.add(fun(PlainLog(&log)), "log");
102 chai.add(fun(ModuleLog(&log)), "log");
103
104 chai.eval(R"(log("Test Message"))");
105
106 // A shortcut to using eval is just to use the chai operator()
107 chai(R"(log("Test Module", "Test Message");)");
108
109 //Finally, it is possible to register a lambda as a system function, in this
110 //way, we can, for instance add a bound member function to the system
111 chai.add(fun([&system](){ return system.do_callbacks("Bound Test"); }), "do_callbacks");
112
113 //Call bound version of do_callbacks
114 chai("do_callbacks()");
115
116 std::function<void ()> caller = chai.eval<std::function<void ()> >(
117 R"(fun() { system.do_callbacks("From Functor"); })"
118 );
119 caller();
120
121
122 //If we would like a type-safe return value from all call, we can use
123 //the templated version of eval:

Callers

nothing calls this directly

Calls 11

varFunction · 0.85
funFunction · 0.85
logFunction · 0.85
const_varFunction · 0.85
stringClass · 0.85
add_globalMethod · 0.80
do_callbacksMethod · 0.80
add_global_constMethod · 0.80
addMethod · 0.45
evalMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected