From 847db2b22abe9440cf6126fd949c67179def4ba5 Mon Sep 17 00:00:00 2001 From: Bill Vaglienti Date: Sat, 2 Dec 2017 15:21:10 -0800 Subject: [PATCH] Added strategic casting to resolve warnings from Visual Stuiod 2017 and Keil ARM compiler. --- exampleprotocol.xml | 2 +- protocolfield.cpp | 23 +++++++++++++++++++++-- protocolparser.cpp | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/exampleprotocol.xml b/exampleprotocol.xml index 7cd4939..b79934c 100644 --- a/exampleprotocol.xml +++ b/exampleprotocol.xml @@ -155,7 +155,7 @@ interpretation - + diff --git a/protocolfield.cpp b/protocolfield.cpp index 78bacc8..5e77760 100644 --- a/protocolfield.cpp +++ b/protocolfield.cpp @@ -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) @@ -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"; @@ -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; diff --git a/protocolparser.cpp b/protocolparser.cpp index 9e26a4d..4b3229d 100644 --- a/protocolparser.cpp +++ b/protocolparser.cpp @@ -17,7 +17,7 @@ #include // 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