Implementation guidelines for creating an expression that was later used to evaluate the contents of a map in C ++?

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

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

Further:

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

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

Any implementation suggestions would be appreciated.

See this post for my previous question regarding the implementation I tried: How to create overloaded operators for forcing pointers in C ++?

0
source share
2 answers

Using this expression syntax will make it very complex. The way I have always done this in the past is to have an abstract class of rules from which I get specific types of rules. Then I add them 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 does not allow directly for ands and ors, but you can get around this with a few "fake" rules called AndRule and OrRule.

+2
source

, , , . Boost:: lambda. ++ 0x - .

0

Source: https://habr.com/ru/post/1758749/


All Articles