Add helper to get int from variable

This commit is contained in:
WarmUpTill 2023-06-07 14:13:12 +02:00 committed by WarmUpTill
parent 4b0a631987
commit 6cc224aeec
4 changed files with 17 additions and 0 deletions

View File

@ -44,4 +44,14 @@ std::optional<double> GetDouble(const std::string &str)
return {};
}
std::optional<int> GetInt(const std::string &str)
{
char *end = nullptr;
int value = std::strtol(str.c_str(), &end, 10);
if (end != str.c_str() && *end == '\0') {
return value;
}
return {};
}
} // namespace advss

View File

@ -9,5 +9,6 @@ std::variant<double, std::string>
EvalMathExpression(const std::string &expression);
bool IsValidNumber(const std::string &);
std::optional<double> GetDouble(const std::string &);
std::optional<int> GetInt(const std::string &);
} // namespace advss

View File

@ -53,6 +53,11 @@ std::optional<double> Variable::DoubleValue() const
return GetDouble(_value);
}
std::optional<int> Variable::IntValue() const
{
return GetInt(_value);
}
void Variable::SetValue(const std::string &val)
{
_value = val;

View File

@ -20,6 +20,7 @@ public:
void Save(obs_data_t *obj) const;
std::string Value() const { return _value; }
std::optional<double> DoubleValue() const;
std::optional<int> IntValue() const;
void SetValue(const std::string &val);
void SetValue(double);
static std::shared_ptr<Item> Create()