Introduction |
CliPP is an object-oriented is a common language interface (CLI) to make it easy to integrate scripting facilities into a project by wrapping C++ classes and functions. The CLI was designed with the javascript scripting language in mind, and comes with a complete javascript parser. In theory, parsers for other scripting languages could be added relatively simple.
It is time for a simple example:
We have a simple C++ function that we want to make accessible from the scripting language
double square(double const& x)
{
return x*x;
}
In order to wrap this function, we need to do the following:
#include <boost/javascript.hpp> #include <boost/clipp.hpp>
using namespace boost::clipp; using namespace boost::javascript; double square(double const& x) { return x*x; } void main() { //Create a javascript parser javascript_parser parser; //The context is a storage class that stores all exposed functions and class definitions context* c=parser.get_context(); //This line exposes the square function to the javascript parser. function(c,"square",square); //Parse string parser.parse("var a=square(Math.PI);"); //Extract data from javascript double result=unwrap<double>(parser.parse("a;"))(); }
The following few pages will explain in detail how to expose classes and some finer details
Copyright © 2003-2005 Peder Holt
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.