Removing visitor pattern and replacing it with CRTP.

This commit is contained in:
Patrick Lühne 2016-06-12 22:12:09 +02:00
parent 318bd8297c
commit e9d48bcb7d
25 changed files with 63 additions and 165 deletions

View File

@ -74,7 +74,38 @@ using Variables = std::vector<VariablePointer>;
class Expression
{
public:
virtual void accept(ExpressionVisitor &expressionVisitor) const = 0;
enum class Type
{
And,
Binary,
Constant,
Either,
Imply,
Not,
Or,
PredicateDeclaration,
Predicate,
PrimitiveType,
Reference,
Variable
};
public:
virtual ~Expression() = default;
virtual Type expressionType() const = 0;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class ExpressionCRTP: public Expression
{
public:
Type expressionType() const override final
{
return Derived::ExpressionType;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,35 +0,0 @@
#ifndef __PLASP__PDDL__EXPRESSION_VISITOR_H
#define __PLASP__PDDL__EXPRESSION_VISITOR_H
#include <plasp/pddl/Expression.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ExpressionVisitor
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Expression;
////////////////////////////////////////////////////////////////////////////////////////////////////
class ExpressionVisitor
{
public:
virtual void visit(const Expression &expression)
{
expression.accept(*this);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -19,10 +19,9 @@ namespace expressions
class And: public NAry<And>
{
public:
static const std::string Identifier;
static const Expression::Type ExpressionType = Expression::Type::And;
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -21,7 +21,7 @@ namespace expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class Binary: public Expression
class Binary: public ExpressionCRTP<Derived>
{
public:
template<typename ExpressionParser>

View File

@ -19,9 +19,11 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Constant: public Expression
class Constant: public ExpressionCRTP<Constant>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Constant;
static void parseTypedDeclaration(Context &context, Domain &domain);
static void parseTypedDeclarations(Context &context, Domain &domain);
static void parseTypedDeclaration(Context &context, Problem &problem);
@ -31,8 +33,6 @@ class Constant: public Expression
static Constant *parseAndFind(Context &context, const Problem &problem);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const PrimitiveType *type() const;

View File

@ -19,10 +19,9 @@ namespace expressions
class Either: public NAry<Either>
{
public:
static const std::string Identifier;
static const Expression::Type ExpressionType = Expression::Type::Either;
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -19,10 +19,9 @@ namespace expressions
class Imply: public Binary<Imply>
{
public:
static const std::string Identifier;
static const Expression::Type ExpressionType = Expression::Type::Imply;
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -21,7 +21,7 @@ namespace expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class NAry: public Expression
class NAry: public ExpressionCRTP<Derived>
{
public:
template<typename ExpressionParser>

View File

@ -18,16 +18,16 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Not: public Expression
class Not: public ExpressionCRTP<Not>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Not;
template<typename ExpressionParser>
static NotPointer parse(Context &context, ExpressionContext &expressionContext,
ExpressionParser parseExpression);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const Expression &argument() const;
private:

View File

@ -19,10 +19,9 @@ namespace expressions
class Or: public NAry<Or>
{
public:
static const std::string Identifier;
static const Expression::Type ExpressionType = Expression::Type::Or;
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -16,15 +16,15 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Predicate: public Expression
class Predicate: public ExpressionCRTP<Predicate>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Predicate;
static PredicatePointer parse(Context &context, ExpressionContext &expressionContext);
static PredicatePointer parse(Context &context, const Problem &problem);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const Expressions &arguments() const;

View File

@ -16,14 +16,14 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class PredicateDeclaration: public Expression
class PredicateDeclaration: public ExpressionCRTP<PredicateDeclaration>
{
public:
static const Expression::Type ExpressionType = Expression::Type::PredicateDeclaration;
static void parse(Context &context, Domain &domain);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const Variables &arguments() const;

View File

@ -19,9 +19,11 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class PrimitiveType: public Expression
class PrimitiveType: public ExpressionCRTP<PrimitiveType>
{
public:
static const Expression::Type ExpressionType = Expression::Type::PrimitiveType;
static void parseDeclaration(Context &context, Domain &domain);
static void parseTypedDeclaration(Context &context, Domain &domain);
@ -31,8 +33,6 @@ class PrimitiveType: public Expression
PrimitiveType();
PrimitiveType(std::string name);
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const std::vector<const PrimitiveType *> &parentTypes() const;

View File

@ -4,7 +4,6 @@
#include <boost/assert.hpp>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -21,12 +20,12 @@ namespace expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
class Reference: public Expression
class Reference: public ExpressionCRTP<Reference<Type>>
{
public:
Reference(const Type *value);
static const Expression::Type ExpressionType = Expression::Type::Reference;
void accept(ExpressionVisitor &expressionVisitor) const override;
Reference(const Type *value);
const Type *value() const;
@ -55,14 +54,6 @@ Reference<Type>::Reference(const Type *value)
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
void Reference<Type>::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*m_value);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
const Type *Reference<Type>::value() const
{

View File

@ -16,9 +16,11 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Variable: public Expression
class Variable: public ExpressionCRTP<Variable>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Variable;
static void parseTypedDeclaration(Context &context, ExpressionContext &expressionContext);
static void parseTypedDeclarations(Context &context, ExpressionContext &expressionContext);
@ -26,8 +28,6 @@ class Variable: public Expression
const ExpressionContext &expressionContext);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const Expression *type() const;

View File

@ -1,7 +1,5 @@
#include <plasp/pddl/expressions/And.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
@ -19,13 +17,6 @@ const std::string And::Identifier = "and";
////////////////////////////////////////////////////////////////////////////////////////////////////
void And::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -7,7 +7,6 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Problem.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
@ -201,13 +200,6 @@ Constant *Constant::parseAndFind(const std::string &constantName, const Constant
////////////////////////////////////////////////////////////////////////////////////////////////////
void Constant::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Constant::setDirty(bool isDirty)
{
m_isDirty = isDirty;

View File

@ -1,7 +1,5 @@
#include <plasp/pddl/expressions/Either.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
@ -19,13 +17,6 @@ const std::string Either::Identifier = "either";
////////////////////////////////////////////////////////////////////////////////////////////////////
void Either::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -1,7 +1,5 @@
#include <plasp/pddl/expressions/Imply.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
@ -19,13 +17,6 @@ const std::string Imply::Identifier = "imply";
////////////////////////////////////////////////////////////////////////////////////////////////////
void Imply::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -1,7 +1,5 @@
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
@ -15,13 +13,6 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void Not::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Expression &Not::argument() const
{
return *m_argument;

View File

@ -1,7 +1,5 @@
#include <plasp/pddl/expressions/Or.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
@ -19,13 +17,6 @@ const std::string Or::Identifier = "or";
////////////////////////////////////////////////////////////////////////////////////////////////////
void Or::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -3,7 +3,6 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/Problem.h>
#include <plasp/pddl/expressions/Constant.h>
@ -148,13 +147,6 @@ PredicatePointer Predicate::parse(Context &context, const Problem &problem)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::setDeclared()
{
m_isDeclared = true;

View File

@ -3,7 +3,6 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/Reference.h>
@ -54,13 +53,6 @@ void PredicateDeclaration::parse(Context &context, Domain &domain)
////////////////////////////////////////////////////////////////////////////////////////////////////
void PredicateDeclaration::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void PredicateDeclaration::setDeclared()
{
m_isDeclared = true;

View File

@ -7,7 +7,6 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
@ -140,13 +139,6 @@ PrimitiveType *PrimitiveType::parseAndFind(Context &context, Domain &domain)
////////////////////////////////////////////////////////////////////////////////////////////////////
void PrimitiveType::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void PrimitiveType::setDirty(bool isDirty)
{
m_isDirty = isDirty;

View File

@ -7,7 +7,6 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Domain.h>
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Either.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
@ -162,13 +161,6 @@ const Variable *Variable::parseAndFind(Context &context, const ExpressionContext
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Variable::name() const
{
return m_name;