Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 30de92d

Browse files
committed
Use convertTo to simplify code in IntegerParameterType
Remove convertValueFromString and replace it with converTo. Signed-off-by: David Wagner <[email protected]>
1 parent 2d3518f commit 30de92d

File tree

2 files changed

+17
-42
lines changed

2 files changed

+17
-42
lines changed

parameter/IntegerParameterType.cpp

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "ParameterAdaptation.h"
3737
#include "Utility.h"
3838
#include <errno.h>
39+
#include <convert.hpp>
3940

4041
#define base CParameterType
4142

@@ -147,14 +148,17 @@ bool CIntegerParameterType::toBlackboard(const string &strValue, uint32_t &uiVal
147148
// Get integer value from the string provided
148149
int64_t iData;
149150

150-
if (!convertValueFromString(strValue, iData, parameterAccessContext)) {
151-
152-
return false;
153-
}
154-
155151
// Check against Min / Max
156152
if (_bSigned) {
157153

154+
if (not convertTo(strValue, iData)) {
155+
string strError;
156+
strError = "Impossible to convert value " + strValue + " for " + getKind();
157+
158+
parameterAccessContext.setError(strError);
159+
return false;
160+
}
161+
158162
if (bValueProvidedAsHexa && isEncodable((uint64_t)iData, !bValueProvidedAsHexa)) {
159163

160164
// Sign extend
@@ -168,6 +172,14 @@ bool CIntegerParameterType::toBlackboard(const string &strValue, uint32_t &uiVal
168172
}
169173
} else {
170174

175+
if (not convertTo(strValue, (uint64_t &)iData)) {
176+
string strError;
177+
strError = "Impossible to convert value " + strValue + " for " + getKind();
178+
179+
parameterAccessContext.setError(strError);
180+
return false;
181+
}
182+
171183
if (!checkValueAgainstRange<uint64_t>(strValue, iData, _uiMin, _uiMax,
172184
parameterAccessContext, bValueProvidedAsHexa)) {
173185

@@ -362,39 +374,6 @@ int CIntegerParameterType::toPlainInteger(int iSizeOptimizedData) const
362374
return base::toPlainInteger(iSizeOptimizedData);
363375
}
364376

365-
// Convert value provided by the user as a string into an int64
366-
bool CIntegerParameterType::convertValueFromString(
367-
const string &strValue, int64_t &iData, CParameterAccessContext &parameterAccessContext) const
368-
{
369-
370-
// Reset errno to check if it is updated during the conversion (strtol/strtoul)
371-
errno = 0;
372-
char *pcStrEnd;
373-
374-
// Convert the input string
375-
if (_bSigned) {
376-
377-
iData = strtoll(strValue.c_str(), &pcStrEnd, 0);
378-
} else {
379-
380-
iData = strtoull(strValue.c_str(), &pcStrEnd, 0);
381-
}
382-
383-
// Conversion error when the input string does not contain only digits or the number is out of
384-
// range (int32_t type)
385-
if (errno || (*pcStrEnd != '\0')) {
386-
387-
string strError;
388-
strError = "Impossible to convert value " + strValue + " for " + getKind();
389-
390-
parameterAccessContext.setError(strError);
391-
392-
return false;
393-
}
394-
395-
return true;
396-
}
397-
398377
// Range checking
399378
template <typename type>
400379
bool CIntegerParameterType::checkValueAgainstRange(const string &strValue, type value,

parameter/IntegerParameterType.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ class CIntegerParameterType : public CParameterType
8686
// Returns true if children dynamic creation is to be dealt with
8787
bool childrenAreDynamic() const override;
8888

89-
// Conversion from std::string
90-
bool convertValueFromString(const std::string &strValue, int64_t &iData,
91-
CParameterAccessContext &parameterAccessContext) const;
92-
9389
// Range checking
9490
template <typename type>
9591
bool checkValueAgainstRange(const std::string &strValue, type value, type minValue,

0 commit comments

Comments
 (0)