Converters

In some cases, you want to supply a user-defined conversion. An example of this is in the definition of the javascript string object:

 

Say you want to expose a point class to javascript, you would define the class as:

    void string::init(context* c)
    {
	class_<string,object,std::string> cls("String",c);
	//Type conversion
	cls.converter(operator const char*);
	cls.converter(arguments<const char*>());
    }

We tell the system that string can be implicitly converted to const char* and equally important, that const char* can be used implicitly to construct a new string object. (The syntax for specifying converters may change)

Wrapping

You have two sorts of conversion: One is the implicit conversion:

    //A function takes a length argument and displays it with the correct unit
    void print(const Length& l);
    //Given that Length has a constructor converter from double, the following is legal JavaScript
    print(5.5);

This is the form of conversion that the converter function is meant for.
In the other case, you have the following:
 

    //A function takes a length argument and displays it with the correct unit
    double square(const double& arg);
    //When exposing this to javascript, we need to use a wrapper type, as primitives can not be
    //represented directly in CliPP.
    var a=square(5.6);

We do not want a to be a Length variable, even though Length has a constructor converter from double
This is the reason for the wrap syntax:

    void number::init(context* c)
    {
	class_<number,object> cls("Number",c);
	//Type wrapping
	cls.wrap(type<double>());
    }

The above statement signals that all double values returned by the system shall be translated to a number object. The above statement will only compile if double is convertible to number (via a constructor) and vice versa, that number is convertible to double (via a implicit type converting function)

You can also supply a third argument to the class_ template definition that will be automatically wrapped in the above manner.