串联cmd和validator
This commit is contained in:
parent
acfa763c62
commit
fec92a6702
|
@ -104,6 +104,7 @@
|
||||||
<ClInclude Include="msgs_types.h" />
|
<ClInclude Include="msgs_types.h" />
|
||||||
<ClInclude Include="validate_impl.h" />
|
<ClInclude Include="validate_impl.h" />
|
||||||
<ClInclude Include="validation.h" />
|
<ClInclude Include="validation.h" />
|
||||||
|
<ClInclude Include="validation_basic.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
|
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
|
||||||
|
|
|
@ -55,5 +55,8 @@
|
||||||
<ClInclude Include="validate_impl.h">
|
<ClInclude Include="validate_impl.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="validation_basic.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -3,7 +3,7 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include "cmds_basic.h"
|
#include "cmds_basic.h"
|
||||||
#include "validation.h"
|
#include "validation_basic.h"
|
||||||
|
|
||||||
namespace dispatch {
|
namespace dispatch {
|
||||||
class IOutput {
|
class IOutput {
|
||||||
|
|
|
@ -4,12 +4,15 @@
|
||||||
#include "cmds_basic.h"
|
#include "cmds_basic.h"
|
||||||
#include "dispatch.h"
|
#include "dispatch.h"
|
||||||
#include "cmds.h"
|
#include "cmds.h"
|
||||||
|
#include "validation.h"
|
||||||
|
|
||||||
int vp(int c) {
|
int vp(int c) {
|
||||||
qDebug() << c;
|
qDebug() << c;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verify::T<int(int)>::Validation<vp> vp_check;
|
||||||
|
|
||||||
namespace xproc {
|
namespace xproc {
|
||||||
int xmp(int arg) {
|
int xmp(int arg) {
|
||||||
qDebug() << "xmp";
|
qDebug() << "xmp";
|
||||||
|
@ -19,8 +22,6 @@ namespace xproc {
|
||||||
|
|
||||||
cmds::T<int(int)>::Cmd<vp> entry("hello");
|
cmds::T<int(int)>::Cmd<vp> entry("hello");
|
||||||
cmds::T<int(int)>::Cmd<xproc::xmp> xvv("sfaf");
|
cmds::T<int(int)>::Cmd<xproc::xmp> xvv("sfaf");
|
||||||
#include "validation.h"
|
|
||||||
validate::ValidateHelper<(void*)vp, int, int> helper;
|
|
||||||
|
|
||||||
|
|
||||||
using namespace Inlet;
|
using namespace Inlet;
|
||||||
|
|
|
@ -1,182 +1,27 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QString>
|
#include "validation_basic.h"
|
||||||
#include <QList>
|
#include "dispatch.h"
|
||||||
#include <iostream>
|
|
||||||
#include <functional>
|
|
||||||
#include "data_types.h"
|
|
||||||
#include "cmds_basic.h"
|
|
||||||
|
|
||||||
|
namespace verify {
|
||||||
|
template<typename... Types> struct T;
|
||||||
|
|
||||||
namespace validate {
|
template<typename Ret, typename... Args>
|
||||||
template<typename T> class Validator {
|
struct T<Ret(Args...)> {
|
||||||
public:
|
template<Inlet::Callable<Ret, Args...> func>
|
||||||
virtual bool check(T value) const {
|
struct Validation {
|
||||||
return false;
|
private:
|
||||||
}
|
validate::ValidateImpls<(void*)func, Ret, Args...> _helper;
|
||||||
virtual void getDepict(datas::IDataObject& self_description) { }
|
|
||||||
|
public:
|
||||||
|
Validation() {
|
||||||
|
auto core = dispatch::Dispatch::unique();
|
||||||
|
core->setCmdValidator(_helper.address(), & _helper);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<int n> validate::ArgsPosition<n, Args...> pos() {
|
||||||
|
return _helper.pos<n>();
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
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 ValidateHelper;
|
|
||||||
template<void* func, typename return_type, typename... args_type>
|
|
||||||
struct ValidateHelper<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());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,178 @@
|
||||||
|
#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());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue