129 lines
4.3 KiB
C++
129 lines
4.3 KiB
C++
#pragma once
|
||
|
||
#include "validation.h"
|
||
|
||
namespace impls {
|
||
template<bool min_equal, bool max_equal, uint precision>
|
||
class DoubleLimit : public validate::Validator<double> {
|
||
private:
|
||
QString _name_store;
|
||
double _limit_max, _limit_min;
|
||
int64_t _limit_max_times = 0, _limit_min_times = 0;
|
||
|
||
public:
|
||
DoubleLimit(const QString& name, double min, double max)
|
||
: _name_store(name), _limit_max(max), _limit_min(min) {
|
||
_limit_min_times = static_cast<int64_t>(min * precision) - min_equal;
|
||
_limit_max_times = static_cast<int64_t>(max * precision) + max_equal;
|
||
}
|
||
|
||
|
||
virtual bool check(double value) const {
|
||
auto value_times = static_cast<int>(value * precision);
|
||
return _limit_min_times < value_times && value_times < _limit_max_times;
|
||
}
|
||
|
||
virtual void getDepict(datas::IDataObject& self_description) {
|
||
self_description.setString(u8"ValidatorName", _name_store);
|
||
self_description.setString(u8"ValueType", u8"Double");
|
||
self_description.setInt64(u8"Precision", precision);
|
||
self_description.setDouble(u8"MinValue", _limit_min);
|
||
self_description.setDouble(u8"MaxValue", _limit_max);
|
||
|
||
auto description = QString(u8"%1 %3 value %4 %2").arg(_limit_min).arg(_limit_max);
|
||
description = description.arg(min_equal ? u8"<=" : u8"<", max_equal ? u8"<=" : u8"<");
|
||
self_description.setString(u8"Description", description);
|
||
}
|
||
};
|
||
|
||
template<bool min_equal, bool max_equal>
|
||
class Int32Limit : public validate::Validator<int32_t> {
|
||
private:
|
||
QString _name_store;
|
||
int32_t _limit_max, _limit_min;
|
||
int32_t _limit_max_times = 0, _limit_min_times = 0;
|
||
|
||
public:
|
||
Int32Limit(const QString& name, int32_t min, int32_t max)
|
||
: _name_store(name), _limit_min(min), _limit_max(max) {
|
||
_limit_max_times = _limit_max + max_equal;
|
||
_limit_min_times = _limit_min - min_equal;
|
||
}
|
||
|
||
|
||
virtual bool check(int32_t value) const {
|
||
return _limit_min_times < value && value < _limit_max_times;
|
||
}
|
||
|
||
virtual void getDepict(datas::IDataObject& self_description) {
|
||
self_description.setString(u8"ValidatorName", _name_store);
|
||
self_description.setString(u8"ValueType", u8"Integer");
|
||
self_description.setInt32(u8"MinValue", _limit_min);
|
||
self_description.setInt32(u8"MaxValue", _limit_max);
|
||
|
||
auto description = QString(u8"%1 %3 value %4 %2").arg(_limit_min).arg(_limit_max);
|
||
description = description.arg(min_equal ? u8"<=" : u8"<", max_equal ? u8"<=" : u8"<");
|
||
self_description.setString(u8"Description", description);
|
||
}
|
||
};
|
||
|
||
|
||
template<bool min_equal, bool max_equal>
|
||
class Int64Limit : public validate::Validator<int64_t> {
|
||
private:
|
||
QString _name_store;
|
||
int64_t _limit_max, _limit_min;
|
||
int64_t _limit_max_times = 0, _limit_min_times = 0;
|
||
|
||
public:
|
||
Int64Limit(const QString& name, int64_t min, int64_t max)
|
||
: _name_store(name), _limit_min(min), _limit_max(max) {
|
||
_limit_max_times = _limit_max + max_equal;
|
||
_limit_min_times = _limit_min - min_equal;
|
||
}
|
||
|
||
|
||
virtual bool check(int64_t value) const {
|
||
return _limit_min_times < value && value < _limit_max_times;
|
||
}
|
||
|
||
virtual void getDepict(datas::IDataObject& self_description) {
|
||
self_description.setString(u8"ValidatorName", _name_store);
|
||
self_description.setString(u8"ValueType", u8"Integer");
|
||
self_description.setInt64(u8"MinValue", _limit_min);
|
||
self_description.setInt64(u8"MaxValue", _limit_max);
|
||
|
||
auto description = QString(u8"%1 %3 value %4 %2").arg(_limit_min).arg(_limit_max);
|
||
description = description.arg(min_equal ? u8"<=" : u8"<", max_equal ? u8"<=" : u8"<");
|
||
self_description.setString(u8"Description", description);
|
||
}
|
||
};
|
||
|
||
|
||
class EnumsValidator : public validate::Validator<int32_t> {
|
||
private:
|
||
QString _name_store;
|
||
QList<int32_t> _enum_options;
|
||
|
||
public:
|
||
EnumsValidator(const QString& name, const QList<int32_t>& list)
|
||
:_name_store(name), _enum_options(list) { }
|
||
|
||
virtual bool check(int32_t value) const {
|
||
return _enum_options.contains(value);
|
||
}
|
||
|
||
virtual void getDepict(datas::IDataObject& self_description) {
|
||
self_description.setString(u8"ValidatorName", _name_store);
|
||
self_description.setString(u8"ValueType", u8"Enum");
|
||
|
||
QStringList options;
|
||
std::transform(_enum_options.begin(), _enum_options.end(), std::back_inserter(options), [](int32_t v) {return QString::number(v); });
|
||
self_description.setString(u8"Values", options.join(u8","));
|
||
self_description.setString(u8"Description", QString("value <20><> { %1 }").arg(options.join(u8",")));
|
||
}
|
||
};
|
||
|
||
|
||
}
|