m2etis  0.4
MatchVisitor.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_PUBSUB_FILTER_MATCHVISITOR_H__
23 #define __M2ETIS_PUBSUB_FILTER_MATCHVISITOR_H__
24 
25 #include <stack>
26 
28 
29 namespace m2etis {
30 namespace pubsub {
31 namespace filter {
32 
33  template <typename EventType>
34  class MatchVisitor : public FilterVisitor<EventType> {
35  private:
36  MatchVisitor() {}
37 
38  public:
39  explicit MatchVisitor(const EventType & event) : event_(event) {}
40 
41  virtual void Visit(const FilterExp<EventType> *) override {}
42 
43  virtual void Visit(const AndExp<EventType> *) override {
44  bool operand1 = operand_stack_.top();
45  operand_stack_.pop();
46  bool operand2 = operand_stack_.top();
47  operand_stack_.pop();
48  operand_stack_.push(operand1 && operand2);
49  }
50 
51  virtual void Visit(const OrExp<EventType> *) override {
52  // operand_stack_.push(operand_stack_.pop() || operand_stack_.pop());
53  bool operand1 = operand_stack_.top();
54  operand_stack_.pop();
55  bool operand2 = operand_stack_.top();
56  operand_stack_.pop();
57  operand_stack_.push(operand1 || operand2);
58  }
59 
60  virtual void Visit(const Predicate<EventType> * predicate) override {
61  operand_stack_.push(predicate->match(event_));
62  }
63 
64 
65  bool get_result() const {
66  return operand_stack_.empty() ? 0 : operand_stack_.top();
67  }
68 
69  void set_event(const EventType & event) { event_ = event; }
70 
71  private:
72  std::stack<bool> operand_stack_; // stack to store operands (=results of 2 children) of match
73  EventType event_; // event to be matched against
74 }; // class MatchVisitor
75 
76 } /* namespace filter */
77 } /* namespace pubsub */
78 } /* namespace m2etis */
79 
80 #endif /* __M2ETIS_PUBSUB_FILTER_MATCHVISITOR_H__ */
81 
virtual void Visit(const Predicate< EventType > *predicate) override
Definition: MatchVisitor.h:60
virtual void Visit(const AndExp< EventType > *) override
Definition: MatchVisitor.h:43
MatchVisitor(const EventType &event)
Definition: MatchVisitor.h:39
virtual void Visit(const OrExp< EventType > *) override
Definition: MatchVisitor.h:51
virtual void Visit(const FilterExp< EventType > *) override
Definition: MatchVisitor.h:41
virtual bool match(const EventType &) const =0
void set_event(const EventType &event)
Definition: MatchVisitor.h:69