This commit is contained in:
codeboss 2024-11-16 21:37:42 +08:00
parent 213fdbf5aa
commit fe721162ac
5 changed files with 180 additions and 64 deletions

View File

@ -102,6 +102,8 @@
<ClInclude Include="data_types.h" />
<ClInclude Include="dispatch.h" />
<ClInclude Include="msgs_types.h" />
<ClInclude Include="validate_impl.h" />
<ClInclude Include="validation.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">

View File

@ -49,5 +49,11 @@
<ClInclude Include="msgs_types.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="validation.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="validate_impl.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,31 +1,52 @@
#pragma once
#include <QString>
#include <memory>
namespace datas {
class IDataArray;
class IDataObject {
public:
virtual ~IDataObject() = default;
virtual bool isValid() const = 0;
/**
* @brief .
*
* \return
*/
virtual bool isEmpty() const = 0;
/**
* @brief Array.
*
* \return
*/
virtual bool isArray() const = 0;
virtual IDataArray toArray() = 0;
/**
* @brief Array.
*
* \return
*/
virtual std::shared_ptr<IDataArray> toArray() = 0;
virtual bool getBool(const QString& key) const = 0;
virtual double getDouble(const QString& key) const = 0;
virtual int32_t getInt32(const QString& key) const = 0;
virtual int64_t getInt64(const QString& key) const = 0;
virtual QString getString(const QString& key) const = 0;
virtual IDataObject getObject(const QString& key) const = 0;
/**
* @brief .
*
* \param key
* \return
*/
virtual std::shared_ptr<IDataObject> getObject(const QString& key) const = 0;
virtual void setBool(const QString& key, bool value) = 0;
virtual void setDouble(const QString& key, double value) = 0;
virtual void setInt32(const QString& key, int32_t value) = 0;
virtual void setInt64(const QString& key, int64_t value) = 0;
virtual void setString(const QString& key, const QString& value) = 0;
virtual void setObject(const QString& key, IDataObject value) = 0;
virtual void setObject(const QString& key, std::shared_ptr<IDataObject> value) = 0;
virtual QString toText() const = 0;
virtual void loadFrom(const QString &text) = 0;
@ -37,9 +58,9 @@ namespace datas {
virtual bool isValid() const = 0;
virtual int32_t size() const = 0;
virtual IDataObject get(int index) const = 0;
virtual void insert(int index, IDataObject value) = 0;
virtual void append(IDataObject value) = 0;
virtual std::shared_ptr<IDataObject> get(int index) const = 0;
virtual void insert(int index, std::shared_ptr<IDataObject> value) = 0;
virtual void append(std::shared_ptr<IDataObject> value) = 0;
};

View File

@ -0,0 +1,95 @@
#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 IntegerLimit : 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:
IntegerLimit(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 ¡Ê { %1 }").arg(options.join(u8",")));
}
};
}

View File

@ -4,37 +4,34 @@
#include <QList>
#include <iostream>
#include <functional>
#include "data_types.h"
namespace validate {
template<typename T> class validator {
template<typename T> class Validator {
public:
virtual bool check(T value) const {
return false;
}
virtual void getDepict(datas::IDataObject& self_description) { }
};
template<typename T> class validator_receiver {
template<typename T> class ValidatorReceiver {
public:
virtual void append(validator<T> inst) { }
virtual void append(Validator<T> inst) { }
};
template<typename... args_type> struct check_delegation;
template<> struct check_delegation<> {
void prest() {
std::cout << "ends" << std::endl;
}
};
template<typename... args_type> struct ValidateTemplet;
template<> struct ValidateTemplet<> { };
template<typename head_type, typename... rest_type>
struct check_delegation<head_type, rest_type...> : public check_delegation<rest_type...> {
struct ValidateTemplet<head_type, rest_type...> : public ValidateTemplet<rest_type...> {
private:
QList<validator<head_type>> validator_list;
QList<Validator<head_type>> validator_list;
public:
void add(validator<head_type> vinst) {
void add(Validator<head_type> vinst) {
validator_list << vinst;
}
bool check(head_type value) {
@ -43,48 +40,43 @@ namespace validate {
return false;
return true;
}
void prest() {
std::cout << "[-]" << validator_list.size() << std::endl;
check_delegation<rest_type...>::prest();
}
};
template<int idx, typename... args_type> struct elm_pos;
template<int idx, typename... args_type> struct ElmPos;
template<typename head_type, typename... rest_type>
struct elm_pos<0, check_delegation<head_type, rest_type...>> {
struct ElmPos<0, ValidateTemplet<head_type, rest_type...>> {
using value_type = head_type;
using templet_type = check_delegation<head_type, rest_type...>;
using templet_type = ValidateTemplet<head_type, rest_type...>;
};
template<int idx, typename head_type, typename... rest_type>
struct elm_pos<idx, check_delegation<head_type, rest_type...>> {
using value_type = typename elm_pos<idx - 1, check_delegation<rest_type...>>::value_type;
using templet_type = typename elm_pos<idx - 1, check_delegation<rest_type...>>::templet_type;
struct ElmPos<idx, ValidateTemplet<head_type, rest_type...>> {
using value_type = typename ElmPos<idx - 1, ValidateTemplet<rest_type...>>::value_type;
using templet_type = typename ElmPos<idx - 1, ValidateTemplet<rest_type...>>::templet_type;
};
template<int idx, typename head_type, typename... rest_type>
void __validator_insert_helper(check_delegation<head_type, rest_type...>& target,
validator<typename elm_pos<idx, check_delegation<head_type, rest_type...>>::value_type> inst) {
using templet_type = typename elm_pos<idx, check_delegation<head_type, rest_type...>>::templet_type;
void __validator_insert_helper(ValidateTemplet<head_type, rest_type...>& target,
Validator<typename ElmPos<idx, ValidateTemplet<head_type, rest_type...>>::value_type> inst) {
using templet_type = typename ElmPos<idx, ValidateTemplet<head_type, rest_type...>>::templet_type;
target.templet_type::add(inst);
}
template<int n, typename... args_type> class slice_position;
template<int n, typename... args_type> class SlicePosition;
template<typename S, typename T> class slice_validator : public validator<S> {
template<typename S, typename T> class SliceValidator : public Validator<S> {
private:
std::function<T(S)> _proc_conv;
validator<T> _sub_validator;
Validator<T> _sub_validator;
public:
slice_validator(std::function<T(S)> proc, validator<T> vinst)
SliceValidator(std::function<T(S)> proc, Validator<T> vinst)
: _proc_conv(proc), _sub_validator(vinst) { }
virtual bool check(S value) const {
@ -92,68 +84,68 @@ namespace validate {
return _sub_validator.check(t_value);
}
};
template<int n, typename... args_type> class args_position
: public validator_receiver<typename elm_pos<n, check_delegation<args_type...>>::value_type> {
template<int n, typename... args_type> class ArgsPosition
: public ValidatorReceiver<typename ElmPos<n, ValidateTemplet<args_type...>>::value_type> {
public:
using param_type = typename elm_pos<n, check_delegation<args_type...>>::value_type;
using param_type = typename ElmPos<n, ValidateTemplet<args_type...>>::value_type;
args_position(check_delegation<args_type...>& target) :_templet_bind(target) { }
ArgsPosition(ValidateTemplet<args_type...>& target) :_templet_bind(target) { }
args_position<n, args_type...>& operator&(validator<param_type> vinst) {
ArgsPosition<n, args_type...>& operator&(Validator<param_type> vinst) {
append(vinst);
return *this;
}
template<typename slice_type>
slice_position<n, param_type, slice_type, args_position<n, args_type...>>
SlicePosition<n, param_type, slice_type, ArgsPosition<n, args_type...>>
slice(std::function<slice_type(param_type)> slice_fun) {
return slice_position<n, param_type, slice_type, args_position<n, args_type...>>(*this, slice_fun);
return SlicePosition<n, param_type, slice_type, ArgsPosition<n, args_type...>>(*this, slice_fun);
}
// validator_receiver
virtual void append(validator<param_type> inst) {
// ValidatorReceiver
virtual void append(Validator<param_type> inst) {
__validator_insert_helper<n, args_type...>(_templet_bind, inst);
}
private:
check_delegation<args_type...>& _templet_bind;
ValidateTemplet<args_type...>& _templet_bind;
};
template<int n, typename s_type, typename t_type, typename... args_type>
class slice_position<n, s_type, t_type, args_position<n, args_type...>> : public validator_receiver<t_type> {
class SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>> : public ValidatorReceiver<t_type> {
public:
slice_position(validator_receiver<s_type>& host, std::function<t_type(s_type)> func)
SlicePosition(ValidatorReceiver<s_type>& host, std::function<t_type(s_type)> func)
:_conv_func(func), _prev_bind(host) { }
slice_position<n, s_type, t_type, args_position<n, args_type...>>& operator&(validator<t_type> vinst) {
SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>>& operator&(Validator<t_type> vinst) {
append(vinst);
return *this;
}
template<typename slice_type>
slice_position<n, t_type, slice_type, args_position<n, args_type...>>
SlicePosition<n, t_type, slice_type, ArgsPosition<n, args_type...>>
slice(std::function<slice_type(t_type)> slice_fun) {
return slice_position<n, t_type, slice_type, args_position<n, args_type...>>(*this, slice_fun);
return SlicePosition<n, t_type, slice_type, ArgsPosition<n, args_type...>>(*this, slice_fun);
}
// validator_receiver
virtual void append(validator<t_type> inst) {
slice_validator<s_type, t_type> mid_validator(_conv_func, inst);
// ValidatorReceiver
virtual void append(Validator<t_type> inst) {
SliceValidator<s_type, t_type> mid_validator(_conv_func, inst);
_prev_bind.append(mid_validator);
}
private:
std::function<t_type(s_type)> _conv_func;
validator_receiver<s_type>& _prev_bind;
ValidatorReceiver<s_type>& _prev_bind;
};
template<void* func, typename... types> struct validate_helper;
template<void* func, typename... types> struct ValidateHelper;
template<void* func, typename return_type, typename... args_type>
struct validate_helper<func, return_type, args_type...> : public check_delegation<args_type...> {
template<int n> args_position<n, args_type...> pos() {
return args_position<n, args_type...>(*this);
struct ValidateHelper<func, return_type, args_type...> : public ValidateTemplet<args_type...> {
template<int n> ArgsPosition<n, args_type...> pos() {
return ArgsPosition<n, args_type...>(*this);
}
};