diff options
author | David Crocker <dcrocker@eschertech.com> | 2022-07-04 11:14:49 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2022-07-04 11:14:49 +0300 |
commit | 4aaab38a855c0ec32d4cb8d701a61aaa596c59dc (patch) | |
tree | b6807992f7451ee070fc2b469eee6a0f74039a9f /src/ObjectModel | |
parent | 4f6a54a735a993c450656a1180d27323668186cf (diff) |
Improved memory safety of ExpressionValue by removing SetType function
Diffstat (limited to 'src/ObjectModel')
-rw-r--r-- | src/ObjectModel/ObjectModel.cpp | 28 | ||||
-rw-r--r-- | src/ObjectModel/ObjectModel.h | 24 |
2 files changed, 34 insertions, 18 deletions
diff --git a/src/ObjectModel/ObjectModel.cpp b/src/ObjectModel/ObjectModel.cpp index 5bffcd3c..e7f86716 100644 --- a/src/ObjectModel/ObjectModel.cpp +++ b/src/ObjectModel/ObjectModel.cpp @@ -193,6 +193,34 @@ void ExpressionValue::Release() noexcept } } +void ExpressionValue::SetBool(bool b) noexcept +{ + Release(); + type = (uint32_t)TypeCode::Bool; + bVal = b; +} + +void ExpressionValue::SetInt(int32_t i) noexcept +{ + Release(); + type = (uint32_t)TypeCode::Int32; + iVal = i; +} + +void ExpressionValue::SetFloat(float f, uint32_t digits) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = digits; } + +void ExpressionValue::SetDriverId(DriverId did) noexcept +{ + Release(); + type = (uint32_t)TypeCode::DriverId_tc; +#if SUPPORT_CAN_EXPANSION + param = did.boardAddress; +#else + param = 0; +#endif + uVal = did.localDriver; +} + #if SUPPORT_CAN_EXPANSION // Given that this is a CanExpansionBoardDetails value, extract the part requested according to the parameter and append it to the string diff --git a/src/ObjectModel/ObjectModel.h b/src/ObjectModel/ObjectModel.h index 146e8f49..3d05ac8c 100644 --- a/src/ObjectModel/ObjectModel.h +++ b/src/ObjectModel/ObjectModel.h @@ -153,33 +153,21 @@ struct ExpressionValue void Release() noexcept; // release any associated storage TypeCode GetType() const noexcept { return (TypeCode)type; } - void SetType(TypeCode t) noexcept { type = (uint32_t)t; } bool IsStringType() const noexcept { return type == (uint32_t)TypeCode::CString || type == (uint32_t)TypeCode::HeapString; } - void SetBool(bool b) noexcept { Release(); type = (uint32_t)TypeCode::Bool; bVal = b; } - void SetChar(char c) noexcept { Release(); type = (uint32_t)TypeCode::Char; cVal = c; } - void SetInt(int32_t i) noexcept { Release(); type = (uint32_t)TypeCode::Int32; iVal = i; } - void SetFloat(float f) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = MaxFloatDigitsDisplayedAfterPoint; } - void SetFloat(float f, uint32_t digits) noexcept { Release(); type = (uint32_t)TypeCode::Float; fVal = f; param = digits; } + void SetBool(bool b) noexcept; + void SetInt(int32_t i) noexcept; + void SetFloat(float f, uint32_t digits) noexcept; + void SetFloat(float f) noexcept { SetFloat(f, MaxFloatDigitsDisplayedAfterPoint); } void SetCString(const char *_ecv_array s) noexcept { Release(); type = (uint32_t)TypeCode::CString; sVal = s; } - void SetDriverId(DriverId did) noexcept - { - Release(); - type = (uint32_t)TypeCode::DriverId_tc; -#if SUPPORT_CAN_EXPANSION - param = did.boardAddress; -#else - param = 0; -#endif - uVal = did.localDriver; - } + void SetDriverId(DriverId did) noexcept; void SetStringHandle(StringHandle sh) noexcept { Release(); type = (uint32_t)TypeCode::HeapString; shVal = sh; } void SetNull(std::nullptr_t dummy) noexcept { Release(); type = (uint32_t)TypeCode::None; } void SetDateTime(time_t t) noexcept { Release(); type = (uint32_t)TypeCode::DateTime_tc; Set56BitValue(t); } // Store a 56-bit value - void Set56BitValue(uint64_t v) { Release(); param = (uint32_t)(v >> 32) & 0x00FFFFFFu; uVal = (uint32_t)v; } + void Set56BitValue(uint64_t v) { param = (uint32_t)(v >> 32) & 0x00FFFFFFu; uVal = (uint32_t)v; } // Extract a 56-bit value that we have stored. Used to retrieve date/times and large bitmaps. uint64_t Get56BitValue() const noexcept { return ((uint64_t)param << 32) | uVal; } |