用于创建表达式的实现建议以后用于评估地图在c ++中的内容? [英] Implementation suggestions for creating an expression later used to evaluate the contents of a map in c++?

查看:117
本文介绍了用于创建表达式的实现建议以后用于评估地图在c ++中的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是为地图创建一个验证器对象。例如我想使用它:

 
MyValidator my_validator =(IsEmpty(key name 1)&& DoesExist键名2))
|| HasNElements(key name 3,num)

后来:

 
if(my_validator.validate(some_map)){
// do something
}

在这种情况下, my_validator.validate(some_map)将返回true如果 some_map [键名1] 空和 some_map [键名称2] 存在,或如果 some_map [键名称3] 有3



有关实现的任何实现建议,请参阅此帖子。

尝试: http://stackoverflow.com / questions / 3440984 / how-do-i-create-overloaded-operators-for-boost-pointers-in-c

解决方案

使用该表达式语法将使其非常困​​难。我过去总是这样做的方式是有一个抽象的规则类,从中导出具体的规则类型。然后将这些添加到验证器:

 验证器v; 
v.add(new NotValueRule(foo));
v.add(new NotIntRule));
v.add(new BetweenRule(a,z));

,然后调用验证器的validate()函数。这不允许直接fors和ors,但你可以绕过一些称为AndRule和OrRule的假规则。


My goal is to create a "validator" object for maps. An example of how I'd like to use it:

MyValidator my_validator = (IsEmpty("key name 1") && DoesExist("key name 2"))
                           || HasNElements("key name 3", num)

Later:

if(my_validator.validate(some_map)) {
// do something
}

In this case, my_validator.validate(some_map) would return true if some_map["key name 1"] was empty and some_map["key name 2"] exists, or if some_map["key name 3"] had 3 elements.

Any implementation suggestions would be appreciated.

See this post for my prior question regarding an implementation I was attempting: http://stackoverflow.com/questions/3440984/how-do-i-create-overloaded-operators-for-boost-pointers-in-c

解决方案

Using that expression syntax is going to make it very difficult. The way I've always done this in the past is to have an abstract Rule class from which I derive concrete rule types. I then add these to the validator:

Validator v;
v.add( new NotValueRule( "foo" ) );
v.add( new NotIntRule ) );
v.add( new BetweenRule( "a", "z" ) );

and then call the validate() function on the validator. This doesn't allow directly for ands and ors, but you can get round that with a couple of "fake" rules called AndRule and OrRule.

这篇关于用于创建表达式的实现建议以后用于评估地图在c ++中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆