Add support for more Port types
Goal
Until now we assumed that each Port has the type Q, but EMAM supports other basic types:
- Q for rational numbers, converted to double
- N for natural numbers, converted to int
- Z for whole numbers, also converted to int
- B for boolean values, converted to bool
- (C for complex, you can ignore this)
You can get the Type-String of a port with port.getTypeReference().getName()
.
Example code in Freemarker:
<#list model.getOutgoingPorts() as port>
<#switch port.getTypeReference().getName()>
<#case "Q">
// double
<#break>
<#case "N">
//int
<#break>
<#case "Z">
//int
<#break>
<#default>
//error
</#switch>
</#list>
Suggestion
Write/parse the values in standard String form
- Q -> 1.5
- N -> 1
- Z -> -1
- B -> false
Alternative
You can convert the whole number types(Z,N) by rounding a double:
//Subscriber
*port_ = (int) round(value);
//Publisher
string value = to_string(1.0 * component->${pub.getName()});
Make sure that the pointer to port_ has the right type(int* in this case)
You can convert the boolean type(B) by checking a threshold:
//Subscriber
*port_ = (value > 1.0e-10);
//Publisher
string value = to_string(component->${pub.getName()} ? 1.0 : 0.0);
Make sure that the pointer to port_ has the right type(bool* in this case)
Alternative:
Deliverables
Adapt your templates, so that all generated publish and callback methods support the Port Types Q, B, Z, and N. You might have to generated multiple Callback classes(e.g. CallbackQ.cpp/.hpp, CallbackN.cpp/.hpp,...) or think of an other approach.