PenetrateBase/PenetrateBasic/validate_basic.h

179 lines
5.4 KiB
C++

#pragma once
#include <QString>
#include <QList>
#include <functional>
#include "cmds_basic.h"
namespace validate {
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 ValidatorReceiver {
public:
virtual void append(Validator<T>* inst) { }
};
template<typename... args_type> struct ValidateTemplet;
template<> struct ValidateTemplet<> {
template<typename Ret>
bool validateFor(Inlet::SignatureImpl<Ret>& value_sequence) const {
return true;
}
};
template<typename head_type, typename... rest_type>
struct ValidateTemplet<head_type, rest_type...> : public ValidateTemplet<rest_type...> {
private:
QList<Validator<head_type>*> validator_list;
public:
void add(Validator<head_type>* vinst) {
validator_list << vinst;
}
bool check(head_type value) const {
for (auto u : validator_list)
if (!u->check(value))
return false;
return true;
}
template<typename Ret>
bool validateFor(Inlet::SignatureImpl<Ret, head_type, rest_type...>& value_sequence) const {
head_type head_value = value_sequence.getArgs0();
if (!check(head_value))
return false;
return ValidateTemplet<rest_type...>::validateFor(value_sequence);
}
};
template<int idx, typename... args_type> struct ElmPos;
template<typename head_type, typename... rest_type>
struct ElmPos<0, ValidateTemplet<head_type, rest_type...>> {
using value_type = head_type;
using templet_type = ValidateTemplet<head_type, rest_type...>;
};
template<int idx, typename head_type, typename... rest_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(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 SlicePosition;
template<typename S, typename T> class SliceValidator : public Validator<S> {
private:
std::function<T(S)> _proc_conv;
Validator<T> _sub_validator;
public:
SliceValidator(std::function<T(S)> proc, Validator<T> vinst)
: _proc_conv(proc), _sub_validator(vinst) { }
virtual bool check(S value) const {
auto t_value = _proc_conv(value);
return _sub_validator.check(t_value);
}
};
template<int n, typename... args_type> class ArgsPosition
: public ValidatorReceiver<typename ElmPos<n, ValidateTemplet<args_type...>>::value_type> {
public:
using param_type = typename ElmPos<n, ValidateTemplet<args_type...>>::value_type;
ArgsPosition(ValidateTemplet<args_type...>& target) :_templet_bind(target) { }
ArgsPosition<n, args_type...>& operator&(Validator<param_type>* vinst) {
append(vinst);
return *this;
}
template<typename slice_type>
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);
}
// ValidatorReceiver
virtual void append(Validator<param_type>* inst) {
__validator_insert_helper<n, args_type...>(_templet_bind, inst);
}
private:
ValidateTemplet<args_type...>& _templet_bind;
};
template<int n, typename s_type, typename t_type, typename... args_type>
class SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>> : public ValidatorReceiver<t_type> {
public:
SlicePosition(ValidatorReceiver<s_type>& host, std::function<t_type(s_type)> func)
:_conv_func(func), _prev_bind(host) { }
SlicePosition<n, s_type, t_type, ArgsPosition<n, args_type...>>& operator&(Validator<t_type>* vinst) {
append(vinst);
return *this;
}
template<typename slice_type>
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);
}
// 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;
ValidatorReceiver<s_type>& _prev_bind;
};
class IValidatorTemplet {
public:
virtual bool doValidate(Inlet::IRunbase* inst) const = 0;
};
template<void* func, typename... types> struct ValidateImpls;
template<void* func, typename return_type, typename... args_type>
struct ValidateImpls<func, return_type, args_type...> : public ValidateTemplet<args_type...>, public IValidatorTemplet {
quint64 address() const {
return (quint64) func;
}
template<int n> ArgsPosition<n, args_type...> pos() {
return ArgsPosition<n, args_type...>(*this);
}
// Ivalidatortemplet
virtual bool doValidate(Inlet::IRunbase* inst) const {
auto valid_data = dynamic_cast<Inlet::CmdsImpl<func, return_type, args_type...>*>(inst);
return ValidateTemplet<args_type...>::validateFor(valid_data->getArgSequence());
}
};
}