Skip to content

Commit

Permalink
Added strategic casting to resolve warnings from Visual Stuiod 2017 a…
Browse files Browse the repository at this point in the history
…nd Keil ARM compiler.
  • Loading branch information
billvaglienti committed Dec 2, 2017
1 parent 2d08eea commit 847db2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion exampleprotocol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ interpretation
<Data name="numCols" inMemoryType="unsigned8"/>
<Data name="floatData" inMemoryType="float32" encodedType="float16" array="N3D" variableArray="numRows" array2d="20" variable2dArray="numCols"/>
<Data name="scaledData" inMemoryType="float32" encodedType="signed8" max="pi" array="20" variableArray="numRows" array2d="N3D" variable2dArray="numCols"/>
<Data name="intData" inMemoryType="signed8" array="N3D" variableArray="numRows" array2d="N3D" variable2dArray="numCols"/>
<Data name="intData" inMemoryType="signed16" encodedType="signed8" array="N3D" variableArray="numRows" array2d="N3D" variable2dArray="numCols"/>
<Data name="dates" struct="smallDate" array="10" variableArray="numRows" array2d="10" variable2dArray="numCols"/>
</Packet>

Expand Down
23 changes: 21 additions & 2 deletions protocolfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,19 @@ QString ProtocolField::getSetToValueString(bool isStructureMember, QString value
}
else
{
// Deal with casting doubles to floats
if(inMemoryType.isFloat && (inMemoryType.bits < 64))
{
// If the value contains a decimal then we assume it is a floating
// point constant, if it does not contain ".0f" then we assume it
// is a double precision constant
if(value.contains('.') && !value.endsWith('f'))
{
// Rather than presume to change the default constant we simply put a cast in front of it
value = "(float)" + value;
}
}

if(isArray())
{
if(isStructureMember)
Expand Down Expand Up @@ -3374,7 +3387,13 @@ QString ProtocolField::getEncodeStringForField(bool isBigEndian, bool isStructur
output += "[i]";
}
else
output += constantstring;
{
// It may be weird, but we do support it. Notice the cast in case the constant string is not the correct type
if(inMemoryType.isFloat && (inMemoryType.bits <= 32) && constantstring.contains('.') && !constantstring.endsWith('f'))
output += "(" + inMemoryType.toTypeString() + ")(" + constantstring + ")";
else
output += constantstring;
}

output += ", data, &byteindex";

Expand Down Expand Up @@ -3867,7 +3886,7 @@ QString ProtocolField::getDecodeStringForField(bool isBigEndian, bool isStructur
}

// Add a cast in case the encoded type is different from the in memory type
if(inMemoryType.isFloat || (inMemoryType.bits != encodedType.bits) || isIntegerScaling())
if(inMemoryType.isFloat || (inMemoryType.bits != encodedType.bits) || isIntegerScaling() || inMemoryType.isEnum)
{
// "int32ToBeBytes((int32_t)((user->value - min)*scale)" for example
function = "(" + typeName + ")" + function;
Expand Down
2 changes: 1 addition & 1 deletion protocolparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <iostream>

// The version of the protocol generator is set here
const QString ProtocolParser::genVersion = "2.8.c";
const QString ProtocolParser::genVersion = "2.8.d";

/*!
* \brief ProtocolParser::ProtocolParser
Expand Down

0 comments on commit 847db2b

Please sign in to comment.