Custom Formatting
The HydraExpress project file allows you to control the way in which a binding formats primitive types. This customization is handled through the mappings element of the HydraExpress project file.
When the binding marshals a primitive type, the marshal function invokes the code specified in the
toString element for that type. As described in
“Creating Customized Mappings” , the contents of the element appear in the generated binding, with a variable that contains the value to be formatted substituted for the token
$value$. The code contained in the
toString element must return a standard
std::string.
For example, the project file below includes a mappings element that provides a custom mapping for the XML Schema type xsd:float. The data binding invokes the static function FormatFloat on class FloatFormatterUtils when marshaling a value of type xsd:float.
<rwsf-codegen-project>
<options>
<option name='project-name' value='myMapping'/>
<mappings>
<type xsdType="xsd:float" cppType="float"/>
<typeinfo cppType="float" isPrimitive="true"
include="FloatFormatterUtils.h">
<fromString>atof($value$).c_str()</fromString>
<toString>
FloatFormatterUtils::FormatFloat($value$)
</toString>
<defaultValue>$value$</defaultValue>
<initialValue>0.0</initialValue>
</typeinfo>
</mappings>
...
</rwsf-codegen-project>
The mapping includes all of the pertinent information for the type, and completely overrides existing settings. In the above example, the toString element specifies the call to the FormatFloat function, and the include attribute of typeinfo identifies the header file for the class that defines the function.
Function FormatFloat uses the standard sprintf to produce a string in a specific format, as shown below:
// FloatFormatterUtils.h
#include <stdlib.h>
class FloatFormatterUtils {
public:
static std::string FormatFloat(float aFloat) {
char buf[40];
sprintf(buf, "%4.2g", aFloat);
return buf;
}
};
When using customized mappings, you may wish to create a special HydraExpress project file that specifies only mappings, which you can use as an additional argument to the code generator.
Note that, if you provide more than one HydraExpress project file to the code generator, the values in the last project file override any previously-set values in the event of conflicts. For more information on creating custom HydraExpress project files, see “Creating a Customized Project File for Special Generation Options,” in the HydraExpress Web Service Development Guide.