Created
February 19, 2012 13:02
-
-
Save biboudis/1863709 to your computer and use it in GitHub Desktop.
Just a Visitor pattern skeleton in C++. No gimmicks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Expressions.h" | |
void* SumExpression::Accept(Visitor& v) | |
{ | |
return (void*) v.VisitSum(this); | |
} | |
void* ConstantExpression::Accept(Visitor& v) | |
{ | |
return (void*) v.VisitConstant(this); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef EXPRESSIONS_H | |
#define EXPRESSIONS_H | |
#include "Visitors.h" | |
class Visitor; | |
class Expression | |
{ | |
public: | |
virtual ~Expression(){}; | |
virtual void* Accept(Visitor&) = 0; | |
protected: | |
Expression() {}; | |
}; | |
class ConstantExpression : public Expression | |
{ | |
public: | |
int constant; | |
ConstantExpression(int cons) { this->constant = cons;}; | |
virtual void* Accept(Visitor& v); | |
}; | |
class SumExpression : public Expression | |
{ | |
public: | |
Expression *left, *right; | |
SumExpression(Expression *left, Expression *right) { this->left=left;this->right=right;} ; | |
virtual void* Accept(Visitor& v); | |
}; | |
#endif /* EXPRESSIONS_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Visitors.h" | |
#include "Expressions.h" | |
using namespace std; | |
int main (void) | |
{ | |
EvaluateVisitor evaluateVisitor; | |
TermsCountVisitor termsCountVisitor; | |
//(1+2)+3 | |
SumExpression *e = | |
new SumExpression( | |
new SumExpression(new ConstantExpression(1), | |
new ConstantExpression(2)), | |
new ConstantExpression(3)); | |
int answer = (int) e->Accept(evaluateVisitor); | |
cout<<"The evaluation is: "<<answer<<endl; | |
int termscount = (int) e->Accept(termsCountVisitor); | |
cout<<"The number of terms is: "<<termscount<<endl; | |
return 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Visitors.h" | |
void* EvaluateVisitor::VisitConstant(ConstantExpression *e) { | |
return (void*) e->constant; | |
}; | |
void* EvaluateVisitor::VisitSum(SumExpression *e) { | |
return (void*) ((int)e->left->Accept((*this)) + (int)e->right->Accept(*this)); | |
}; | |
void* TermsCountVisitor::VisitConstant(ConstantExpression *e) { | |
return (void*) terms++; | |
}; | |
void* TermsCountVisitor::VisitSum(SumExpression *e) { | |
return (void*) ((int)e->left->Accept((*this)) + (int)e->right->Accept(*this)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VISITORS_H | |
#define VISITORS_H | |
#include "Expressions.h" | |
class ConstantExpression; | |
class SumExpression; | |
class Visitor | |
{ | |
public: | |
virtual void* VisitConstant(ConstantExpression*)=0; | |
virtual void* VisitSum(SumExpression*) = 0; | |
protected: | |
Visitor(){}; | |
}; | |
class EvaluateVisitor : public Visitor | |
{ | |
public: | |
EvaluateVisitor(){}; | |
void* VisitConstant(ConstantExpression *e); | |
void* VisitSum(SumExpression *e); | |
}; | |
class TermsCountVisitor : public Visitor | |
{ | |
private: | |
int terms; | |
public: | |
int GetTermsCount() { return this->terms;}; | |
TermsCountVisitor():terms(0) {}; | |
void* VisitConstant(ConstantExpression *e); | |
void* VisitSum(SumExpression *e); | |
}; | |
#endif /* VISITORS_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment