MXS-2346 Add config::ParamInteger and config::Integer
This commit is contained in:
@ -439,49 +439,44 @@ string ParamBool::to_string(value_type value) const
|
||||
}
|
||||
|
||||
/**
|
||||
* ParamCount
|
||||
* ParamNumber
|
||||
*/
|
||||
std::string ParamCount::type() const
|
||||
{
|
||||
return "count";
|
||||
}
|
||||
|
||||
std::string ParamCount::default_to_string() const
|
||||
std::string ParamNumber::default_to_string() const
|
||||
{
|
||||
return to_string(m_default_value);
|
||||
}
|
||||
|
||||
bool ParamCount::validate(const std::string& value_as_string, std::string* pMessage) const
|
||||
bool ParamNumber::validate(const std::string& value_as_string, std::string* pMessage) const
|
||||
{
|
||||
value_type value;
|
||||
return from_string(value_as_string, &value, pMessage);
|
||||
}
|
||||
|
||||
bool ParamCount::set(Type& value, const std::string& value_as_string) const
|
||||
bool ParamNumber::set(Type& value, const std::string& value_as_string) const
|
||||
{
|
||||
mxb_assert(&value.parameter() == this);
|
||||
|
||||
Count& count_value = static_cast<Count&>(value);
|
||||
Number& number_value = static_cast<Number&>(value);
|
||||
|
||||
value_type x;
|
||||
bool valid = from_string(value_as_string, &x);
|
||||
|
||||
if (valid)
|
||||
{
|
||||
count_value.set(x);
|
||||
number_value.set(x);
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
bool ParamCount::from_string(const std::string& value_as_string,
|
||||
value_type* pValue,
|
||||
std::string* pMessage) const
|
||||
bool ParamNumber::from_string(const std::string& value_as_string,
|
||||
value_type* pValue,
|
||||
std::string* pMessage) const
|
||||
{
|
||||
const char* zValue = value_as_string.c_str();
|
||||
char* zEnd;
|
||||
long l = strtol(zValue, &zEnd, 10);
|
||||
bool valid = (l >= 0 && zEnd != zValue && *zEnd == 0);
|
||||
bool valid = (l >= m_min_value && l <= m_max_value && zEnd != zValue && *zEnd == 0);
|
||||
|
||||
if (valid)
|
||||
{
|
||||
@ -489,18 +484,49 @@ bool ParamCount::from_string(const std::string& value_as_string,
|
||||
}
|
||||
else if (pMessage)
|
||||
{
|
||||
*pMessage = "Invalid count: ";
|
||||
if (!(zEnd != zValue && *zEnd == 0))
|
||||
{
|
||||
*pMessage = "Invalid ";
|
||||
}
|
||||
else if (!(l >= m_min_value))
|
||||
{
|
||||
*pMessage = "Too small a ";
|
||||
}
|
||||
else
|
||||
{
|
||||
mxb_assert(!(l <= m_max_value));
|
||||
*pMessage = "Too large a ";
|
||||
}
|
||||
|
||||
*pMessage += type();
|
||||
*pMessage += ": ";
|
||||
*pMessage += value_as_string;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
std::string ParamCount::to_string(value_type value) const
|
||||
std::string ParamNumber::to_string(value_type value) const
|
||||
{
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* ParamCount
|
||||
*/
|
||||
std::string ParamCount::type() const
|
||||
{
|
||||
return "count";
|
||||
}
|
||||
|
||||
/**
|
||||
* ParamInteger
|
||||
*/
|
||||
std::string ParamInteger::type() const
|
||||
{
|
||||
return "integer";
|
||||
}
|
||||
|
||||
/**
|
||||
* ParamPath
|
||||
*/
|
||||
|
||||
@ -46,6 +46,11 @@ param_count(&specification,
|
||||
"count_parameter",
|
||||
"Specifies the cardinality of something.");
|
||||
|
||||
config::ParamInteger
|
||||
param_integer(&specification,
|
||||
"integer_parameter",
|
||||
"Specifies a number.");
|
||||
|
||||
config::ParamDuration<std::chrono::seconds>
|
||||
param_duration_1(&specification,
|
||||
"duration_parameter_1",
|
||||
@ -274,6 +279,24 @@ int test_string(config::String& value)
|
||||
return test(value, entries, elements_in_array(entries));
|
||||
}
|
||||
|
||||
int test_integer(config::Integer& value)
|
||||
{
|
||||
static const TestEntry<config::Integer::value_type> entries[] =
|
||||
{
|
||||
{ "0", true, 0 },
|
||||
{ "-1", true, -1 },
|
||||
{ "1", true, 1 },
|
||||
{ "-2147483648", true, -2147483648 },
|
||||
{ "2147483647", true, 2147483647 },
|
||||
|
||||
{ "-2147483649", false },
|
||||
{ "2147483648", false },
|
||||
{ "0x10" , false },
|
||||
};
|
||||
|
||||
return test(value, entries, elements_in_array(entries));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
mxb::Log log;
|
||||
@ -314,5 +337,8 @@ int main()
|
||||
config::String value_string(&configuration, ¶m_string);
|
||||
nErrors += test_string(value_string);
|
||||
|
||||
config::Integer value_integer(&configuration, ¶m_integer);
|
||||
nErrors += test_integer(value_integer);
|
||||
|
||||
return nErrors ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user