Skip to content
Loic Oudot edited this page Jan 12, 2014 · 4 revisions

Templates are files used by java4cpp to customize types mappings between Java and C++ and to controls JNI code generation.

The java4cpp-templates project provide a sets of templates for a fully operational Java/C++ mappings.

Type mappings

base-templates.xml

This is the base mappings for all the class type object in Java. This file is mandatory in java4cpp.

Java typeC++ type
voidvoid
booleanbool
byteunsigned char
charchar
doubledouble
floatfloat
intint
longlong long
shortshort
arraysstd::vector
enumsenum + proxy
other classesproxy class

All Java classes, will be mapped in a proxy C++ class. For example, java.util.Map will be translated in java::util::Map.

Enums are converted in a header file that contains a C++ enum and a proxy C++ class that contains Enums methods. For example:

publicenumEnumeration {
ONE(10), TWO(20), THREE(30);
privateintvalue;
Enumeration(intvalue) {
this.value = value;
}
publicintgetValue() {
returnvalue;
}
}

will be translated in a C++ enum:

typedefenum {
NULL_VALUE = -1,
ONE,
TWO,
THREE
} EnumerationEnum;

and a C++ proxy:

classEnumeration
{
public:
...
explicitEnumeration(EnumerationEnum arg1);
virtual~Enumeration();
public:virtualintgetValue();
...
};

Arrays are transformed in std::vector, for example:

TypeJavaC++
primitivedouble[]std::vector<double>
enumsEnumeration[]std::vector<EnumerationEnum>
classesjava.lang.Double[]std::vector<java::lang::Double>
muti-dimsdouble[][]std::vector<std::vector<double> >

jnistring-templates.xml

Transforms java.lang.String into std::string by using UTF jni API (NewStringUTF, GetStringUTFChars). Works well if char in the C++ project is defined as unicode (wchar).

string-templates.xml

Transforms java.lang.String into std::string by using platform's default charset. Works well if char in the C++ project is defined in ISO-8859-1 (Windows) and other.

optional-templates.xml

Transforms primitive object in boost::optional. For example, java:lang::Double will be transformed in boost::optional<double>. Using boost::optional permit us to have a C++ syntax very similar to Java.

boost::optional<int> value;
// test nullabilityif( value ) { ... }
// get a value with defaultint result = value.get_value_or(0);
// sets a value
value = 42;
...

For a detailed semantics about boost::optional<> click here

JNI code generation

Java4cpp use freemarker templates to generate proxies source code. java4cpp-templates project includes theses files:

File nameUsage
header-templates.ftlUsed for generating .h file of a proxy
source-templates.ftlUsed for generating .cpp file of a proxy
enum-templates.ftlUsed for generating .h file for enums
execption-templates.ftlUsed for generating source code for exception translation
common.ftlSome freemarker utilities functions

Clone this wiki locally