2024-11-16 11:55:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QList>
|
|
|
|
#include <iostream>
|
|
|
|
#include <functional>
|
2024-11-16 13:37:42 +00:00
|
|
|
#include "data_types.h"
|
2024-11-16 11:55:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace validate {
|
2024-11-16 13:37:42 +00:00
|
|
|
template<typename T> class Validator {
|
2024-11-16 11:55:32 +00:00
|
|
|
public:
|
|
|
|
virtual bool check(T value) const {
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-16 13:37:42 +00:00
|
|
|
virtual void getDepict(datas::IDataObject& self_description) { }
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
2024-11-16 13:37:42 +00:00
|
|
|
template<typename T> class ValidatorReceiver {
|
2024-11-16 11:55:32 +00:00
|
|
|
public:
|
2024-11-16 13:37:42 +00:00
|
|
|
virtual void append(Validator<T> inst) { }
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
template<typename... args_type> struct ValidateTemplet;
|
|
|
|
template<> struct ValidateTemplet<> { };
|
2024-11-16 11:55:32 +00:00
|
|
|
template<typename head_type, typename... rest_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
struct ValidateTemplet<head_type, rest_type...> : public ValidateTemplet<rest_type...> {
|
2024-11-16 11:55:32 +00:00
|
|
|
private:
|
2024-11-16 13:37:42 +00:00
|
|
|
QList<Validator<head_type>> validator_list;
|
2024-11-16 11:55:32 +00:00
|
|
|
|
|
|
|
public:
|
2024-11-16 13:37:42 +00:00
|
|
|
void add(Validator<head_type> vinst) {
|
2024-11-16 11:55:32 +00:00
|
|
|
validator_list << vinst;
|
|
|
|
}
|
|
|
|
bool check(head_type value) {
|
|
|
|
for (auto u : validator_list)
|
|
|
|
if (!u.check(value))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
template<int idx, typename... args_type> struct ElmPos;
|
2024-11-16 11:55:32 +00:00
|
|
|
|
|
|
|
template<typename head_type, typename... rest_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
struct ElmPos<0, ValidateTemplet<head_type, rest_type...>> {
|
2024-11-16 11:55:32 +00:00
|
|
|
using value_type = head_type;
|
2024-11-16 13:37:42 +00:00
|
|
|
using templet_type = ValidateTemplet<head_type, rest_type...>;
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
|
|
|
template<int idx, typename head_type, typename... rest_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
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;
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<int idx, typename head_type, typename... rest_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
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;
|
2024-11-16 11:55:32 +00:00
|
|
|
target.templet_type::add(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
template<int n, typename... args_type> class SlicePosition;
|
2024-11-16 11:55:32 +00:00
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
template<typename S, typename T> class SliceValidator : public Validator<S> {
|
2024-11-16 11:55:32 +00:00
|
|
|
private:
|
|
|
|
std::function<T(S)> _proc_conv;
|
2024-11-16 13:37:42 +00:00
|
|
|
Validator<T> _sub_validator;
|
2024-11-16 11:55:32 +00:00
|
|
|
|
|
|
|
public:
|
2024-11-16 13:37:42 +00:00
|
|
|
SliceValidator(std::function<T(S)> proc, Validator<T> vinst)
|
2024-11-16 11:55:32 +00:00
|
|
|
: _proc_conv(proc), _sub_validator(vinst) { }
|
|
|
|
|
|
|
|
virtual bool check(S value) const {
|
|
|
|
auto t_value = _proc_conv(value);
|
|
|
|
return _sub_validator.check(t_value);
|
|
|
|
}
|
|
|
|
};
|
2024-11-16 13:37:42 +00:00
|
|
|
template<int n, typename... args_type> class ArgsPosition
|
|
|
|
: public ValidatorReceiver<typename ElmPos<n, ValidateTemplet<args_type...>>::value_type> {
|
2024-11-16 11:55:32 +00:00
|
|
|
public:
|
2024-11-16 13:37:42 +00:00
|
|
|
using param_type = typename ElmPos<n, ValidateTemplet<args_type...>>::value_type;
|
2024-11-16 11:55:32 +00:00
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
ArgsPosition(ValidateTemplet<args_type...>& target) :_templet_bind(target) { }
|
2024-11-16 11:55:32 +00:00
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
ArgsPosition<n, args_type...>& operator&(Validator<param_type> vinst) {
|
2024-11-16 11:55:32 +00:00
|
|
|
append(vinst);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename slice_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
SlicePosition<n, param_type, slice_type, ArgsPosition<n, args_type...>>
|
|
|
|
slice(std::function<slice_type(param_type)> slice_fun) {
|
|
|
|
return SlicePosition<n, param_type, slice_type, ArgsPosition<n, args_type...>>(*this, slice_fun);
|
2024-11-16 11:55:32 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
// ValidatorReceiver
|
|
|
|
virtual void append(Validator<param_type> inst) {
|
2024-11-16 11:55:32 +00:00
|
|
|
__validator_insert_helper<n, args_type...>(_templet_bind, inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-11-16 13:37:42 +00:00
|
|
|
ValidateTemplet<args_type...>& _templet_bind;
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
|
|
|
template<int n, typename s_type, typename t_type, typename... args_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
class SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>> : public ValidatorReceiver<t_type> {
|
2024-11-16 11:55:32 +00:00
|
|
|
public:
|
2024-11-16 13:37:42 +00:00
|
|
|
SlicePosition(ValidatorReceiver<s_type>& host, std::function<t_type(s_type)> func)
|
2024-11-16 11:55:32 +00:00
|
|
|
:_conv_func(func), _prev_bind(host) { }
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>>& operator&(Validator<t_type> vinst) {
|
2024-11-16 11:55:32 +00:00
|
|
|
append(vinst);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename slice_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
SlicePosition<n, t_type, slice_type, ArgsPosition<n, args_type...>>
|
|
|
|
slice(std::function<slice_type(t_type)> slice_fun) {
|
|
|
|
return SlicePosition<n, t_type, slice_type, ArgsPosition<n, args_type...>>(*this, slice_fun);
|
2024-11-16 11:55:32 +00:00
|
|
|
}
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
// ValidatorReceiver
|
|
|
|
virtual void append(Validator<t_type> inst) {
|
|
|
|
SliceValidator<s_type, t_type> mid_validator(_conv_func, inst);
|
2024-11-16 11:55:32 +00:00
|
|
|
_prev_bind.append(mid_validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<t_type(s_type)> _conv_func;
|
2024-11-16 13:37:42 +00:00
|
|
|
ValidatorReceiver<s_type>& _prev_bind;
|
2024-11-16 11:55:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-16 13:37:42 +00:00
|
|
|
template<void* func, typename... types> struct ValidateHelper;
|
2024-11-16 11:55:32 +00:00
|
|
|
template<void* func, typename return_type, typename... args_type>
|
2024-11-16 13:37:42 +00:00
|
|
|
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);
|
2024-11-16 11:55:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|