1 #ifndef AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H 2 #define AUBO_SCOPE_KEYBOARD_INPUT_VALIDATOR_H 13 namespace aubo_scope {
36 virtual bool isValid(std::string value) = 0;
45 virtual std::string
getMessage(std::string value) = 0;
51 : min_value_(min_value), max_value_(max_value)
56 : range_func_(range_func)
65 if (value[0] ==
'-') {
66 if (value.size() > std::to_string(INT_MIN).size()) {
69 }
else if (value.size() > std::to_string(INT_MAX).size()) {
73 int64_t v = atoll(value.c_str());
74 return v >= min_value_ && v <= max_value_;
82 if (value[0] ==
'-') {
83 if (value.size() > std::to_string(INT_MIN).size()) {
84 return QObject::tr(
"Error: The value is between %1 and %2.")
89 }
else if (value.size() > std::to_string(INT_MAX).size()) {
90 return QObject::tr(
"Error: The value is between %1 and %2.")
96 int64_t v = atoll(value.c_str());
97 if (v < min_value_ || v > max_value_) {
98 return QObject::tr(
"Error: The value is between %1 and %2.")
110 auto [min_value, max_value] = range_func_();
111 min_value_ = min_value;
112 max_value_ = max_value;
115 if (min_value_ > max_value_) {
116 min_value_ ^= max_value_ ^= min_value_ ^= max_value_;
121 int min_value_{ INT_MIN };
122 int max_value_{ INT_MAX };
123 std::function<std::pair<int, int>()> range_func_{
nullptr };
129 : min_value_(min_value), max_value_(max_value)
134 std::function<std::pair<uint32_t, uint32_t>()> range_func)
135 : range_func_(range_func)
144 if (value.size() > std::to_string(UINT_MAX).size()) {
148 uint64_t v = QString::fromStdString(value).toULongLong();
149 return v >= min_value_ && v <= max_value_;
157 if (value.size() > std::to_string(UINT_MAX).size()) {
158 return QObject::tr(
"Error: The value is between %1 and %2.")
164 uint64_t v = QString::fromStdString(value).toULongLong();
165 if (v < min_value_ || v > max_value_) {
166 return QObject::tr(
"Error: The value is between %1 and %2.")
178 auto [min_value, max_value] = range_func_();
179 min_value_ = min_value;
180 max_value_ = max_value;
183 if (min_value_ > max_value_) {
184 min_value_ ^= max_value_ ^= min_value_ ^= max_value_;
189 uint32_t min_value_{ 0 };
190 uint32_t max_value_{ UINT_MAX };
191 std::function<std::pair<uint32_t, uint32_t>()> range_func_{
nullptr };
197 : min_value_(minValue), max_value_(maxValue)
202 : range_func_(range_func)
211 return QString::fromStdString(value).toDouble() >= min_value_ &&
212 QString::fromStdString(value).toDouble() <= max_value_;
220 if (QString::fromStdString(value).toDouble() < min_value_ ||
221 QString::fromStdString(value).toDouble() > max_value_) {
222 return QObject::tr(
"Error: The value is between %1 and %2.")
223 .arg(min_value_, 0,
'g', 15)
224 .arg(max_value_, 0,
'g', 15)
234 auto [min_value, max_value] = range_func_();
235 min_value_ = min_value;
236 max_value_ = max_value;
239 if (min_value_ > max_value_) {
240 double t = min_value_;
241 min_value_ = max_value_;
247 double min_value_{ DBL_MIN };
248 double max_value_{ DBL_MAX };
249 std::function<std::pair<double, double>()> range_func_{
nullptr };
255 : min_length_(min_length), max_length_(max_length)
260 : range_func_(range_func)
268 auto value_size = QString::fromStdString(value).length();
269 return value_size >= min_length_ && value_size <= max_length_;
276 auto value_size = QString::fromStdString(value).length();
277 if (value_size < min_length_ || value_size > max_length_) {
279 "Error: The value is between %1 and %2 in length.")
291 auto [min_length, max_length] = range_func_();
292 min_length_ = min_length;
293 max_length_ = max_length;
296 if (min_length_ > max_length_) {
297 min_length_ ^= max_length_ ^= min_length_ ^= max_length_;
302 size_t min_length_{ 0 };
303 size_t max_length_{ ULONG_MAX };
304 std::function<std::pair<size_t, size_t>()> range_func_{
nullptr };
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
bool isValid(std::string value) override
StringLengthValidator(size_t min_length, size_t max_length)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
bool isValid(std::string value) override
bool isValid(std::string value) override
UIntegerRangeValidator(uint32_t min_value, uint32_t max_value)
IntegerRangeValidator(std::function< std::pair< int, int >()> range_func)
UIntegerRangeValidator(std::function< std::pair< uint32_t, uint32_t >()> range_func)
bool isValid(std::string value) override
DoubleRangeValidator(double minValue, double maxValue)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
IntegerRangeValidator(int min_value, int max_value)
std::string getMessage(std::string value) override
Returns a meaningful message in case the value is not valid.
DoubleRangeValidator(std::function< std::pair< double, double >()> range_func)
StringLengthValidator(std::function< std::pair< size_t, size_t >()> range_func)