Skip to content

Commit 4e8169b

Browse files
authored
refactor fmi3 data types casting (#1572)
1 parent db63301 commit 4e8169b

2 files changed

Lines changed: 99 additions & 109 deletions

File tree

src/OMSimulatorLib/ComponentFMU3CS.cpp

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -997,77 +997,80 @@ oms_status_enu_t oms::ComponentFMU3CS::getInteger(const fmi3ValueReference& vr,
997997
{
998998
CallClock callClock(clock);
999999

1000-
// Temporary variables for different types
1001-
int64_t value64;
1002-
int32_t value32;
1003-
int16_t value16;
1004-
int8_t value8;
1005-
uint64_t valueU64;
1006-
uint32_t valueU32;
1007-
uint16_t valueU16;
1008-
uint8_t valueU8;
1009-
10101000
switch (numericType)
10111001
{
10121002
case oms_signal_numeric_type_INT64:
10131003
{
1014-
if (fmi3OK != fmi3_getInt64(fmu, &vr, 1, &value64, 1))
1004+
int64_t v64;
1005+
if (fmi3OK != fmi3_getInt64(fmu, &vr, 1, &v64, 1))
10151006
return oms_status_error;
1016-
if (value64 < INT_MIN || value64 > INT_MAX)
1017-
return oms_status_error; // Value out of range for int
1018-
value = static_cast<int>(value64); // Cast to int
1007+
if (v64 < INT_MIN || v64 > INT_MAX)
1008+
return oms_status_error; // out of int range
1009+
value = static_cast<int>(v64);
10191010
break;
10201011
}
10211012
case oms_signal_numeric_type_INT32:
10221013
{
1023-
if (fmi3OK != fmi3_getInt32(fmu, &vr, 1, &value, 1))
1014+
int32_t v32;
1015+
if (fmi3OK != fmi3_getInt32(fmu, &vr, 1, &v32, 1))
10241016
return oms_status_error;
1017+
value = static_cast<int>(v32); // safe
10251018
break;
10261019
}
10271020
case oms_signal_numeric_type_INT16:
10281021
{
1029-
if (fmi3OK != fmi3_getInt16(fmu, &vr, 1, &value16, 1))
1022+
int16_t v16;
1023+
if (fmi3OK != fmi3_getInt16(fmu, &vr, 1, &v16, 1))
10301024
return oms_status_error;
1031-
value = static_cast<int>(value16);
1025+
value = static_cast<int>(v16); // safe
10321026
break;
10331027
}
10341028
case oms_signal_numeric_type_INT8:
10351029
{
1036-
if (fmi3OK != fmi3_getInt8(fmu, &vr, 1, &value8, 1))
1030+
int8_t v8;
1031+
if (fmi3OK != fmi3_getInt8(fmu, &vr, 1, &v8, 1))
10371032
return oms_status_error;
1038-
value = static_cast<int>(value8);
1033+
value = static_cast<int>(v8); // safe
10391034
break;
10401035
}
10411036
case oms_signal_numeric_type_UINT64:
10421037
{
1043-
if (fmi3OK != fmi3_getUInt64(fmu, &vr, 1, &valueU64, 1))
1038+
uint64_t vU64;
1039+
if (fmi3OK != fmi3_getUInt64(fmu, &vr, 1, &vU64, 1))
10441040
return oms_status_error;
1045-
value = static_cast<int>(valueU64);
1041+
if (vU64 > static_cast<uint64_t>(INT_MAX))
1042+
return oms_status_error; // cannot fit in int
1043+
value = static_cast<int>(vU64);
10461044
break;
10471045
}
10481046
case oms_signal_numeric_type_UINT32:
10491047
{
1050-
if (fmi3OK != fmi3_getUInt32(fmu, &vr, 1, &valueU32, 1))
1048+
uint32_t vU32;
1049+
if (fmi3OK != fmi3_getUInt32(fmu, &vr, 1, &vU32, 1))
10511050
return oms_status_error;
1052-
value = static_cast<int>(valueU32);
1051+
if (vU32 > static_cast<uint32_t>(INT_MAX))
1052+
return oms_status_error; // cannot fit in int
1053+
value = static_cast<int>(vU32);
10531054
break;
10541055
}
10551056
case oms_signal_numeric_type_UINT16:
10561057
{
1057-
if (fmi3OK != fmi3_getUInt16(fmu, &vr, 1, &valueU16, 1))
1058+
uint16_t vU16;
1059+
if (fmi3OK != fmi3_getUInt16(fmu, &vr, 1, &vU16, 1))
10581060
return oms_status_error;
1059-
value = static_cast<int>(valueU16);
1061+
value = static_cast<int>(vU16); // safe
10601062
break;
10611063
}
10621064
case oms_signal_numeric_type_UINT8:
10631065
{
1064-
if (fmi3OK != fmi3_getUInt8(fmu, &vr, 1, &valueU8, 1))
1066+
uint8_t vU8;
1067+
if (fmi3OK != fmi3_getUInt8(fmu, &vr, 1, &vU8, 1))
10651068
return oms_status_error;
1066-
value = static_cast<int>(valueU8);
1069+
value = static_cast<int>(vU8); // safe
10671070
break;
10681071
}
1069-
default :
1070-
return logError("Unsupported Numeric Type");
1072+
default:
1073+
return logError("Unsupported Numeric Type for var");
10711074
}
10721075
return oms_status_ok;
10731076
}
@@ -1631,75 +1634,67 @@ oms_status_enu_t oms::ComponentFMU3CS::setInteger(const ComRef& cref, int value)
16311634
else
16321635
{
16331636
fmi3ValueReference vr = allVariables[j].getValueReferenceFMI3();
1634-
int64_t value64;
1635-
int32_t value32;
1636-
int16_t value16;
1637-
int8_t value8;
1638-
uint64_t valueU64;
1639-
uint32_t valueU32;
1640-
uint16_t valueU16;
1641-
uint8_t valueU8;
16421637
switch (allVariables[j].getNumericType())
16431638
{
16441639
case oms_signal_numeric_type_INT64:
16451640
{
1646-
value64 = static_cast<int>(value); // Cast to int
1647-
if (fmi3OK != fmi3_setInt64(fmu, &vr, 1, &value64, 1))
1641+
int64_t v = static_cast<int64_t>(value);
1642+
if (fmi3OK != fmi3_setInt64(fmu, &vr, 1, &v, 1))
16481643
return oms_status_error;
16491644
break;
16501645
}
16511646
case oms_signal_numeric_type_INT32:
16521647
{
1653-
if (fmi3OK != fmi3_setInt32(fmu, &vr, 1, &value, 1))
1648+
int32_t v = static_cast<int32_t>(value);
1649+
if (fmi3OK != fmi3_setInt32(fmu, &vr, 1, &v, 1))
16541650
return oms_status_error;
16551651
break;
16561652
}
16571653
case oms_signal_numeric_type_INT16:
16581654
{
1659-
value16 = static_cast<int>(value);
1660-
if (fmi3OK != fmi3_setInt16(fmu, &vr, 1, &value16, 1))
1655+
int16_t v = static_cast<int16_t>(value);
1656+
if (fmi3OK != fmi3_setInt16(fmu, &vr, 1, &v, 1))
16611657
return oms_status_error;
16621658
break;
16631659
}
1664-
16651660
case oms_signal_numeric_type_INT8:
16661661
{
1667-
value8 = static_cast<int>(value);
1668-
if (fmi3OK != fmi3_setInt8(fmu, &vr, 1, &value8, 1))
1662+
int8_t v = static_cast<int8_t>(value);
1663+
if (fmi3OK != fmi3_setInt8(fmu, &vr, 1, &v, 1))
16691664
return oms_status_error;
16701665
break;
16711666
}
16721667
case oms_signal_numeric_type_UINT64:
16731668
{
1674-
valueU64 = static_cast<int>(value);
1675-
if (fmi3OK != fmi3_setUInt64(fmu, &vr, 1, &valueU64, 1))
1669+
uint64_t v = static_cast<uint64_t>(value);
1670+
if (fmi3OK != fmi3_setUInt64(fmu, &vr, 1, &v, 1))
16761671
return oms_status_error;
16771672
break;
16781673
}
16791674
case oms_signal_numeric_type_UINT32:
16801675
{
1681-
valueU32 = static_cast<int>(value);
1682-
if (fmi3OK != fmi3_setUInt32(fmu, &vr, 1, &valueU32, 1))
1676+
uint32_t v = static_cast<uint32_t>(value);
1677+
if (fmi3OK != fmi3_setUInt32(fmu, &vr, 1, &v, 1))
16831678
return oms_status_error;
16841679
break;
16851680
}
16861681
case oms_signal_numeric_type_UINT16:
16871682
{
1688-
valueU16 = static_cast<int>(value);
1689-
if (fmi3OK != fmi3_setUInt16(fmu, &vr, 1, &valueU16, 1))
1683+
uint16_t v = static_cast<uint16_t>(value);
1684+
if (fmi3OK != fmi3_setUInt16(fmu, &vr, 1, &v, 1))
16901685
return oms_status_error;
16911686
break;
16921687
}
16931688
case oms_signal_numeric_type_UINT8:
16941689
{
1695-
valueU8 = static_cast<int>(value);
1696-
if (fmi3OK != fmi3_setUInt8(fmu, &vr, 1, &valueU8, 1))
1690+
uint8_t v = static_cast<uint8_t>(value);
1691+
if (fmi3OK != fmi3_setUInt8(fmu, &vr, 1, &v, 1))
16971692
return oms_status_error;
16981693
break;
16991694
}
17001695
default:
17011696
return logError("Unsupported Numeric Type for var: \"" + std::string(cref.c_str()) + "\"");
1702-
}
1697+
}
17031698
}
17041699

17051700
return oms_status_ok;

0 commit comments

Comments
 (0)