mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 04:44:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -15,14 +15,17 @@
|
||||
* Context
|
||||
* contains information that's global to the interpreter
|
||||
*/
|
||||
class Context {
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
void set(std::string var, bool value) {
|
||||
vars.insert(std::pair<std::string, bool>(var, value));
|
||||
void set( const std::string& var, const bool value)
|
||||
{
|
||||
vars.insert( std::pair<std::string, bool>( var, value ) );
|
||||
}
|
||||
|
||||
bool get(std::string exp) {
|
||||
return vars[exp];
|
||||
|
||||
bool get( const std::string& exp )
|
||||
{
|
||||
return vars[ exp ];
|
||||
}
|
||||
// ...
|
||||
|
||||
@@ -36,10 +39,13 @@ private:
|
||||
* declares an abstract Interpret operation that is common to all nodes
|
||||
* in the abstract syntax tree
|
||||
*/
|
||||
class AbstractExpression {
|
||||
class AbstractExpression
|
||||
{
|
||||
public:
|
||||
virtual ~AbstractExpression() {}
|
||||
virtual bool interpret(Context *) {
|
||||
|
||||
virtual bool interpret( Context* const )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// ...
|
||||
@@ -51,18 +57,19 @@ public:
|
||||
* in the grammar (an instance is required for every terminal symbol
|
||||
* in a sentence)
|
||||
*/
|
||||
class TerminalExpression : public AbstractExpression {
|
||||
class TerminalExpression : public AbstractExpression
|
||||
{
|
||||
public:
|
||||
TerminalExpression(std::string val)
|
||||
: value(val) {}
|
||||
|
||||
TerminalExpression( const std::string& val ) : value( val ) {}
|
||||
|
||||
~TerminalExpression() {}
|
||||
|
||||
bool interpret(Context *context) {
|
||||
return context->get(value);
|
||||
|
||||
bool interpret( Context* const context )
|
||||
{
|
||||
return context->get( value );
|
||||
}
|
||||
// ...
|
||||
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
// ...
|
||||
@@ -73,21 +80,24 @@ private:
|
||||
* implements an Interpret operation for nonterminal symbols
|
||||
* in the grammar (one such class is required for every rule in the grammar)
|
||||
*/
|
||||
class NonterminalExpression : public AbstractExpression {
|
||||
class NonterminalExpression : public AbstractExpression
|
||||
{
|
||||
public:
|
||||
NonterminalExpression(AbstractExpression *left, AbstractExpression *right)
|
||||
: lop(left), rop(right) {}
|
||||
|
||||
~NonterminalExpression() {
|
||||
NonterminalExpression( AbstractExpression *left, AbstractExpression *right ) :
|
||||
lop( left ), rop( right ) {}
|
||||
|
||||
~NonterminalExpression()
|
||||
{
|
||||
delete lop;
|
||||
delete rop;
|
||||
}
|
||||
|
||||
bool interpret(Context *context) {
|
||||
return lop->interpret(context) && rop->interpret(context);
|
||||
|
||||
bool interpret( Context *const context )
|
||||
{
|
||||
return lop->interpret( context ) && rop->interpret( context );
|
||||
}
|
||||
// ...
|
||||
|
||||
|
||||
private:
|
||||
AbstractExpression *lop;
|
||||
AbstractExpression *rop;
|
||||
@@ -101,15 +111,15 @@ int main()
|
||||
// that corresponds to expression (A AND B)
|
||||
AbstractExpression *A = new TerminalExpression("A");
|
||||
AbstractExpression *B = new TerminalExpression("B");
|
||||
AbstractExpression *exp = new NonterminalExpression(A, B);
|
||||
|
||||
AbstractExpression *exp = new NonterminalExpression( A, B );
|
||||
|
||||
Context context;
|
||||
context.set("A", true);
|
||||
context.set("B", false);
|
||||
|
||||
std::cout << context.get("A") << " AND " << context.get("B");
|
||||
std::cout << " = " << exp->interpret(&context) << std::endl;
|
||||
|
||||
context.set( "A", true );
|
||||
context.set( "B", false );
|
||||
|
||||
std::cout << context.get( "A" ) << " AND " << context.get( "B" );
|
||||
std::cout << " = " << exp->interpret( &context ) << std::endl;
|
||||
|
||||
delete exp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user