From 68df78a2a6defcdb7f6677e8abb31bb558ed7674 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 16 May 2012 15:55:03 -0600 Subject: [PATCH] Add examples for using C++ lambdas with chaiscript. #32 --- CMakeLists.txt | 4 ++++ unittests/cpp_lambda_test.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 unittests/cpp_lambda_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d448095..8d60975 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,10 @@ if(BUILD_TESTING) target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME short_comparison_test COMMAND short_comparison_test) + add_executable(cpp_lambda_test unittests/cpp_lambda_test.cpp) + target_link_libraries(cpp_lambda_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + add_test(NAME cpp_lambda_test COMMAND cpp_lambda_test) + add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) diff --git a/unittests/cpp_lambda_test.cpp b/unittests/cpp_lambda_test.cpp new file mode 100644 index 0000000..ec5a0ff --- /dev/null +++ b/unittests/cpp_lambda_test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +int main() +{ + + // We cannot deduce the type of a lambda expression, you must either wrap it + // in an std::function or provide the signature + + + chaiscript::ChaiScript chai(chaiscript::Std_Lib::library()); + + // provide the signature + chai.add(chaiscript::fun([] { return "hello"; } ), "f1"); + + // wrap + chai.add(chaiscript::fun(std::function([] { return "world"; } )), "f2"); + + if (chai.eval("f1()") == "hello" + && chai.eval("f2()") == "world") + { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } + + + +}