Browse Source

[trac979] Implementation of the logic checks

Michal 'vorner' Vaner 14 years ago
parent
commit
8c5e626892
2 changed files with 32 additions and 14 deletions
  1. 26 4
      src/lib/acl/logic_check.h
  2. 6 10
      src/lib/acl/tests/logic_check_test.cc

+ 26 - 4
src/lib/acl/logic_check.h

@@ -38,11 +38,33 @@ public:
 };
 };
 
 
 template<typename Mode, typename Context>
 template<typename Mode, typename Context>
-class LogicOperator : public CompoundCheck<Context> {
+class LogicOperator : public CompoundCheck<Context>, boost::noncopyable {
 public:
 public:
-    void addSubexpression(boost::shared_ptr<Check<Context> > expr);
-    virtual typename CompoundCheck<Context>::Checks getSubexpressions() const;
-    virtual bool matches(const Context& context) const;
+    ~ LogicOperator() {
+        for (typename CompoundCheck<Context>::Checks::iterator
+                 i(checks_.begin());
+             i != checks_.end(); ++i) {
+            delete *i;
+        }
+    }
+    void addSubexpression(const Check<Context>* expr) {
+        checks_.push_back(expr);
+    }
+    virtual typename CompoundCheck<Context>::Checks getSubexpressions() const {
+        return (checks_);
+    }
+    virtual bool matches(const Context& context) const {
+        for (typename CompoundCheck<Context>::Checks::const_iterator
+                 i(checks_.begin());
+             i != checks_.end(); ++i) {
+            if (Mode::terminate((*i)->matches(context))) {
+                return (!Mode::start());
+            }
+        }
+        return (Mode::start());
+    }
+private:
+    typename CompoundCheck<Context>::Checks checks_;
 };
 };
 
 
 template<typename Mode, typename Context, typename Action = BasicAction>
 template<typename Mode, typename Context, typename Action = BasicAction>

+ 6 - 10
src/lib/acl/tests/logic_check_test.cc

@@ -30,7 +30,7 @@ TEST(LogicOperators, AnyOfSpec) {
 TEST(LogicOperators, AllOfSpec) {
 TEST(LogicOperators, AllOfSpec) {
     EXPECT_TRUE(AllOfSpec::start());
     EXPECT_TRUE(AllOfSpec::start());
     EXPECT_TRUE(AllOfSpec::terminate(false));
     EXPECT_TRUE(AllOfSpec::terminate(false));
-    EXPECT_FALSE(AnyOfSpec::terminate(true));
+    EXPECT_FALSE(AllOfSpec::terminate(true));
 }
 }
 
 
 // Generic test of one check
 // Generic test of one check
@@ -46,20 +46,16 @@ testCheck(bool emptyResult) {
     EXPECT_EQ(emptyResult, oper.matches(log));
     EXPECT_EQ(emptyResult, oper.matches(log));
     log.checkFirst(0);
     log.checkFirst(0);
     // Fill it with some subexpressions
     // Fill it with some subexpressions
-    oper.addSubexpression(shared_ptr<ConstCheck>(new ConstCheck(emptyResult,
-                                                                0)));
-    oper.addSubexpression(shared_ptr<ConstCheck>(new ConstCheck(emptyResult,
-                                                                1)));
-    oper.addSubexpression(shared_ptr<ConstCheck>(new ConstCheck(!emptyResult,
-                                                                2)));
-    oper.addSubexpression(shared_ptr<ConstCheck>(new ConstCheck(!emptyResult,
-                                                                3)));
+    oper.addSubexpression(new ConstCheck(emptyResult, 0));
+    oper.addSubexpression(new ConstCheck(emptyResult, 1));
+    oper.addSubexpression(new ConstCheck(!emptyResult, 2));
+    oper.addSubexpression(new ConstCheck(!emptyResult, 3));
     // They are listed there
     // They are listed there
     EXPECT_EQ(4, oper.getSubexpressions().size());
     EXPECT_EQ(4, oper.getSubexpressions().size());
     // Now, the last one kills it, but the first ones will run, the fourth
     // Now, the last one kills it, but the first ones will run, the fourth
     // won't
     // won't
     EXPECT_EQ(!emptyResult, oper.matches(log));
     EXPECT_EQ(!emptyResult, oper.matches(log));
-    log.checkFirst(4);
+    log.checkFirst(3);
 }
 }
 
 
 TEST(LogicOperators, AllOf) {
 TEST(LogicOperators, AllOf) {