From 9afabacde3d6553635fd1a874d6d3053b22778eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 4 Sep 2016 18:26:02 +0200 Subject: [PATCH] Switched to intrusive pointers for much easier maintenance. --- include/plasp/pddl/Action.h | 4 +- include/plasp/pddl/Expression.h | 54 +++++++++---- include/plasp/pddl/expressions/At.h | 2 +- include/plasp/pddl/expressions/Binary.h | 6 +- include/plasp/pddl/expressions/Constant.h | 12 +-- include/plasp/pddl/expressions/NAry.h | 6 +- include/plasp/pddl/expressions/Not.h | 2 +- include/plasp/pddl/expressions/Predicate.h | 4 +- .../plasp/pddl/expressions/PrimitiveType.h | 6 +- include/plasp/pddl/expressions/Reference.h | 75 ------------------- include/plasp/pddl/expressions/Type.h | 2 +- include/plasp/pddl/expressions/Variable.h | 12 +-- src/plasp/pddl/TranslatorASP.cpp | 4 +- src/plasp/pddl/expressions/Constant.cpp | 18 ++--- src/plasp/pddl/expressions/Imply.cpp | 4 +- src/plasp/pddl/expressions/Predicate.cpp | 12 +-- .../pddl/expressions/PredicateDeclaration.cpp | 2 +- src/plasp/pddl/expressions/PrimitiveType.cpp | 10 +-- src/plasp/pddl/expressions/Type.cpp | 2 +- src/plasp/pddl/expressions/Unsupported.cpp | 2 +- src/plasp/pddl/expressions/Variable.cpp | 39 +++++----- tests/TestPDDLNormalization.cpp | 16 ++-- 22 files changed, 118 insertions(+), 176 deletions(-) delete mode 100644 include/plasp/pddl/expressions/Reference.h diff --git a/include/plasp/pddl/Action.h b/include/plasp/pddl/Action.h index 56a0fee..35cef85 100644 --- a/include/plasp/pddl/Action.h +++ b/include/plasp/pddl/Action.h @@ -38,8 +38,8 @@ class Action std::string m_name; expressions::Variables m_parameters; - std::unique_ptr m_precondition; - std::unique_ptr m_effect; + ExpressionPointer m_precondition; + ExpressionPointer m_effect; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h index 4b58dca..7d17863 100644 --- a/include/plasp/pddl/Expression.h +++ b/include/plasp/pddl/Expression.h @@ -1,7 +1,7 @@ #ifndef __PLASP__PDDL__EXPRESSION_H #define __PLASP__PDDL__EXPRESSION_H -#include +#include #include @@ -23,55 +23,58 @@ class ExpressionVisitor; class Problem; class Expression; -using ExpressionPointer = std::unique_ptr; +using ExpressionPointer = boost::intrusive_ptr; using Expressions = std::vector; namespace expressions { class And; -using AndPointer = std::unique_ptr; +using AndPointer = boost::intrusive_ptr; class At; -using AtPointer = std::unique_ptr; +using AtPointer = boost::intrusive_ptr; class Constant; -using ConstantPointer = std::unique_ptr; +using ConstantPointer = boost::intrusive_ptr; using Constants = std::vector; +class Dummy; +using DummyPointer = boost::intrusive_ptr; + class Either; -using EitherPointer = std::unique_ptr; +using EitherPointer = boost::intrusive_ptr; class Imply; -using ImplyPointer = std::unique_ptr; +using ImplyPointer = boost::intrusive_ptr; class Not; -using NotPointer = std::unique_ptr; +using NotPointer = boost::intrusive_ptr; class Or; -using OrPointer = std::unique_ptr; +using OrPointer = boost::intrusive_ptr; class Predicate; -using PredicatePointer = std::unique_ptr; +using PredicatePointer = boost::intrusive_ptr; using Predicates = std::vector; class PredicateDeclaration; -using PredicateDeclarationPointer = std::unique_ptr; +using PredicateDeclarationPointer = boost::intrusive_ptr; using PredicateDeclarations = std::vector; class PrimitiveType; -using PrimitiveTypePointer = std::unique_ptr; +using PrimitiveTypePointer = boost::intrusive_ptr; using PrimitiveTypes = std::vector; template class Reference; template -using ReferencePointer = std::unique_ptr>; +using ReferencePointer = boost::intrusive_ptr>; class Unsupported; -using UnsupportedPointer = std::unique_ptr; +using UnsupportedPointer = boost::intrusive_ptr; class Variable; -using VariablePointer = std::unique_ptr; +using VariablePointer = boost::intrusive_ptr; using Variables = std::vector; } @@ -110,10 +113,31 @@ class Expression // * a new expression pointer to replace this one if required; this object is then empty // * nullptr otherwise; the object may or may not have changed virtual ExpressionPointer normalize() = 0; + + private: + friend void intrusive_ptr_add_ref(Expression *expression); + friend void intrusive_ptr_release(Expression *expression); + + size_t m_referenceCount = 0; }; //////////////////////////////////////////////////////////////////////////////////////////////////// +inline void intrusive_ptr_add_ref(Expression *expression) +{ + expression->m_referenceCount++; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline void intrusive_ptr_release(Expression *expression) +{ + if (--expression->m_referenceCount == 0) + delete expression; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + template class ExpressionCRTP: public Expression { diff --git a/include/plasp/pddl/expressions/At.h b/include/plasp/pddl/expressions/At.h index 7bc8ef4..68457ee 100644 --- a/include/plasp/pddl/expressions/At.h +++ b/include/plasp/pddl/expressions/At.h @@ -83,7 +83,7 @@ AtPointer At::parse(Context &context, ExpressionContext &expressionContext, return nullptr; } - auto expression = std::make_unique(At()); + auto expression = AtPointer(new At); expression->m_timePoint = timePoint; diff --git a/include/plasp/pddl/expressions/Binary.h b/include/plasp/pddl/expressions/Binary.h index 7ac7e59..543d935 100644 --- a/include/plasp/pddl/expressions/Binary.h +++ b/include/plasp/pddl/expressions/Binary.h @@ -24,7 +24,7 @@ class Binary: public ExpressionCRTP { public: template - static std::unique_ptr parse(Context &context, + static boost::intrusive_ptr parse(Context &context, ExpressionContext &expressionContext, ExpressionParser parseExpression); public: @@ -45,7 +45,7 @@ class Binary: public ExpressionCRTP template template -std::unique_ptr Binary::parse(Context &context, +boost::intrusive_ptr Binary::parse(Context &context, ExpressionContext &expressionContext, ExpressionParser parseExpression) { auto &parser = context.parser; @@ -59,7 +59,7 @@ std::unique_ptr Binary::parse(Context &context, return nullptr; } - auto expression = std::make_unique(); + auto expression = boost::intrusive_ptr(new Derived); // Assume that expression identifier (imply, exists, etc.) is already parsed // Parse arguments of the expression diff --git a/include/plasp/pddl/expressions/Constant.h b/include/plasp/pddl/expressions/Constant.h index 4f69f2e..b8d4a49 100644 --- a/include/plasp/pddl/expressions/Constant.h +++ b/include/plasp/pddl/expressions/Constant.h @@ -28,12 +28,12 @@ class Constant: public ExpressionCRTP static void parseTypedDeclaration(Context &context, Problem &problem); static void parseTypedDeclarations(Context &context, Problem &problem); - static Constant *parseAndFind(Context &context, const Domain &domain); - static Constant *parseAndFind(Context &context, const Problem &problem); + static ConstantPointer parseAndFind(Context &context, const Domain &domain); + static ConstantPointer parseAndFind(Context &context, const Problem &problem); public: const std::string &name() const; - const PrimitiveType *type() const; + PrimitiveTypePointer type() const; ExpressionPointer normalize() override; @@ -41,20 +41,20 @@ class Constant: public ExpressionCRTP static ConstantPointer parseDeclaration(Context &context); static void parseTypedDeclaration(Context &context, Domain &domain, Constants &constants); - static Constant *parseAndFind(const std::string &constantName, const Constants &constants); + static ConstantPointer parseAndFind(const std::string &constantName, const Constants &constants); Constant(); void setDirty(bool isDirty = true); bool isDirty() const; - void setType(const PrimitiveType *parentType); + void setType(PrimitiveTypePointer parentType); bool m_isDirty; std::string m_name; - const PrimitiveType *m_type; + PrimitiveTypePointer m_type; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/NAry.h b/include/plasp/pddl/expressions/NAry.h index c136f38..cc743c9 100644 --- a/include/plasp/pddl/expressions/NAry.h +++ b/include/plasp/pddl/expressions/NAry.h @@ -24,7 +24,7 @@ class NAry: public ExpressionCRTP { public: template - static std::unique_ptr parse(Context &context, + static boost::intrusive_ptr parse(Context &context, ExpressionContext &expressionContext, ExpressionParser parseExpression); public: @@ -43,7 +43,7 @@ class NAry: public ExpressionCRTP template template -std::unique_ptr NAry::parse(Context &context, +boost::intrusive_ptr NAry::parse(Context &context, ExpressionContext &expressionContext, ExpressionParser parseExpression) { auto &parser = context.parser; @@ -57,7 +57,7 @@ std::unique_ptr NAry::parse(Context &context, return nullptr; } - auto expression = std::make_unique(); + auto expression = boost::intrusive_ptr(new Derived); parser.skipWhiteSpace(); diff --git a/include/plasp/pddl/expressions/Not.h b/include/plasp/pddl/expressions/Not.h index a4af388..5e2d362 100644 --- a/include/plasp/pddl/expressions/Not.h +++ b/include/plasp/pddl/expressions/Not.h @@ -57,7 +57,7 @@ NotPointer Not::parse(Context &context, ExpressionContext &expressionContext, return nullptr; } - auto expression = std::make_unique(Not()); + auto expression = NotPointer(new Not); context.parser.skipWhiteSpace(); diff --git a/include/plasp/pddl/expressions/Predicate.h b/include/plasp/pddl/expressions/Predicate.h index c9f2af0..4b8468e 100644 --- a/include/plasp/pddl/expressions/Predicate.h +++ b/include/plasp/pddl/expressions/Predicate.h @@ -26,7 +26,7 @@ class Predicate: public ExpressionCRTP public: const std::string &name() const; - const std::vector &arguments() const; + const std::vector &arguments() const; bool isDeclared() const; @@ -40,7 +40,7 @@ class Predicate: public ExpressionCRTP bool m_isDeclared; std::string m_name; - std::vector m_arguments; + std::vector m_arguments; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/PrimitiveType.h b/include/plasp/pddl/expressions/PrimitiveType.h index 9cd26e9..4f3f476 100644 --- a/include/plasp/pddl/expressions/PrimitiveType.h +++ b/include/plasp/pddl/expressions/PrimitiveType.h @@ -26,14 +26,14 @@ class PrimitiveType: public ExpressionCRTP static void parseDeclaration(Context &context, Domain &domain); static void parseTypedDeclaration(Context &context, Domain &domain); - static PrimitiveType *parseAndFind(Context &context, Domain &domain); + static PrimitiveTypePointer parseAndFind(Context &context, Domain &domain); public: PrimitiveType(); PrimitiveType(std::string name); const std::string &name() const; - const std::vector &parentTypes() const; + const std::vector &parentTypes() const; ExpressionPointer normalize() override; @@ -45,7 +45,7 @@ class PrimitiveType: public ExpressionCRTP std::string m_name; - std::vector m_parentTypes; + std::vector m_parentTypes; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/Reference.h b/include/plasp/pddl/expressions/Reference.h deleted file mode 100644 index b7ca69a..0000000 --- a/include/plasp/pddl/expressions/Reference.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__REFERENCE_H -#define __PLASP__PDDL__EXPRESSIONS__REFERENCE_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Reference -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -class Reference: public ExpressionCRTP> -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Reference; - - public: - Reference(Type *value); - - Type *get(); - const Type *get() const; - - ExpressionPointer normalize(); - - protected: - Type *m_value = nullptr; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -Reference::Reference(Type *value) -: m_value{value} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -Type *Reference::get() -{ - return m_value; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const Type *Reference::get() const -{ - return m_value; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -ExpressionPointer Reference::normalize() -{ - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Type.h b/include/plasp/pddl/expressions/Type.h index fbd43c1..54c09cd 100644 --- a/include/plasp/pddl/expressions/Type.h +++ b/include/plasp/pddl/expressions/Type.h @@ -16,7 +16,7 @@ namespace expressions // //////////////////////////////////////////////////////////////////////////////////////////////////// -const Expression *parseExistingPrimitiveType(Context &context, +ExpressionPointer parseExistingPrimitiveType(Context &context, ExpressionContext &expressionContext); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/Variable.h b/include/plasp/pddl/expressions/Variable.h index ed6d95b..e61e6e1 100644 --- a/include/plasp/pddl/expressions/Variable.h +++ b/include/plasp/pddl/expressions/Variable.h @@ -24,20 +24,19 @@ class Variable: public ExpressionCRTP static void parseTypedDeclaration(Context &context, ExpressionContext &expressionContext); static void parseTypedDeclarations(Context &context, ExpressionContext &expressionContext); - static const Variable *parseAndFind(Context &context, + static VariablePointer parseAndFind(Context &context, const ExpressionContext &expressionContext); public: void setName(std::string name); const std::string &name() const; - const Expression *type() const; + void setType(ExpressionPointer type); + ExpressionPointer type() const; void setDirty(bool isDirty = true); bool isDirty() const; - void setType(const Expression *type); - ExpressionPointer normalize() override; private: @@ -50,10 +49,7 @@ class Variable: public ExpressionCRTP std::string m_name; - const Expression *m_type; - - // Stores "either" expression if necessary - ExpressionPointer m_eitherExpression; + ExpressionPointer m_type; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 183de3c..771eb34 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -313,7 +313,7 @@ void TranslatorASP::translateConstants(const std::string &heading, const express << utils::String(constant->name()) << "))." << std::endl; - const auto *type = constant->type(); + const auto type = constant->type(); if (type != nullptr) { @@ -366,7 +366,7 @@ void TranslatorASP::translateVariablesBody(const expressions::Variables &variabl if (variable.type()->expressionType() != Expression::Type::PrimitiveType) throw utils::TranslatorException("only primitive types supported currently"); - const auto &type = *dynamic_cast(variable.type()); + const auto &type = dynamic_cast(*variable.type()); m_outputStream << utils::RuleName("has") << "(" << utils::Variable(variable.name()) << ", " diff --git a/src/plasp/pddl/expressions/Constant.cpp b/src/plasp/pddl/expressions/Constant.cpp index e245433..7a9ca85 100644 --- a/src/plasp/pddl/expressions/Constant.cpp +++ b/src/plasp/pddl/expressions/Constant.cpp @@ -35,7 +35,7 @@ ConstantPointer Constant::parseDeclaration(Context &context) { context.parser.skipWhiteSpace(); - auto constant = std::make_unique(Constant()); + auto constant = ConstantPointer(new Constant); constant->m_name = context.parser.parseIdentifier(); @@ -75,7 +75,7 @@ void Constant::parseTypedDeclaration(Context &context, Domain &domain, Constants return; // If existing, parse and store parent type - auto *type = PrimitiveType::parseAndFind(context, domain); + auto type = PrimitiveType::parseAndFind(context, domain); // Assign parent type to all types that were previously flagged std::for_each(constants.begin(), constants.end(), @@ -145,7 +145,7 @@ void Constant::parseTypedDeclarations(Context &context, Problem &problem) //////////////////////////////////////////////////////////////////////////////////////////////////// -Constant *Constant::parseAndFind(Context &context, const Domain &domain) +ConstantPointer Constant::parseAndFind(Context &context, const Domain &domain) { auto &parser = context.parser; @@ -153,7 +153,7 @@ Constant *Constant::parseAndFind(Context &context, const Domain &domain) const auto constantName = parser.parseIdentifier(); - auto *constant = parseAndFind(constantName, domain.constants()); + auto constant = parseAndFind(constantName, domain.constants()); if (constant != nullptr) return constant; @@ -163,7 +163,7 @@ Constant *Constant::parseAndFind(Context &context, const Domain &domain) //////////////////////////////////////////////////////////////////////////////////////////////////// -Constant *Constant::parseAndFind(Context &context, const Problem &problem) +ConstantPointer Constant::parseAndFind(Context &context, const Problem &problem) { auto &parser = context.parser; @@ -171,7 +171,7 @@ Constant *Constant::parseAndFind(Context &context, const Problem &problem) const auto constantName = parser.parseIdentifier(); - auto *constant = parseAndFind(constantName, problem.domain().constants()); + auto constant = parseAndFind(constantName, problem.domain().constants()); if (constant) return constant; @@ -186,7 +186,7 @@ Constant *Constant::parseAndFind(Context &context, const Problem &problem) //////////////////////////////////////////////////////////////////////////////////////////////////// -Constant *Constant::parseAndFind(const std::string &constantName, const Constants &constants) +ConstantPointer Constant::parseAndFind(const std::string &constantName, const Constants &constants) { const auto match = std::find_if(constants.cbegin(), constants.cend(), [&](const auto &constant) @@ -225,14 +225,14 @@ const std::string &Constant::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void Constant::setType(const PrimitiveType *type) +void Constant::setType(PrimitiveTypePointer type) { m_type = type; } //////////////////////////////////////////////////////////////////////////////////////////////////// -const PrimitiveType *Constant::type() const +PrimitiveTypePointer Constant::type() const { return m_type; } diff --git a/src/plasp/pddl/expressions/Imply.cpp b/src/plasp/pddl/expressions/Imply.cpp index 1776116..965e371 100644 --- a/src/plasp/pddl/expressions/Imply.cpp +++ b/src/plasp/pddl/expressions/Imply.cpp @@ -26,10 +26,10 @@ ExpressionPointer Imply::normalize() BOOST_ASSERT(m_argumentStorage[0]); BOOST_ASSERT(m_argumentStorage[1]); - auto notArgument0 = std::make_unique(); + auto notArgument0 = NotPointer(new Not); notArgument0->setArgument(std::move(m_argumentStorage[0])); - auto orExpression = std::make_unique(); + auto orExpression = OrPointer(new Or); orExpression->addArgument(std::move(notArgument0)); orExpression->addArgument(std::move(m_argumentStorage[1])); diff --git a/src/plasp/pddl/expressions/Predicate.cpp b/src/plasp/pddl/expressions/Predicate.cpp index 59bf051..7b0b80f 100644 --- a/src/plasp/pddl/expressions/Predicate.cpp +++ b/src/plasp/pddl/expressions/Predicate.cpp @@ -54,7 +54,7 @@ PredicatePointer Predicate::parse(Context &context, ExpressionContext &expressio return nullptr; } - auto predicate = std::make_unique(Predicate()); + auto predicate = PredicatePointer(new Predicate); predicate->m_name = predicateName; @@ -66,13 +66,13 @@ PredicatePointer Predicate::parse(Context &context, ExpressionContext &expressio // Parse variables if (context.parser.currentCharacter() == '?') { - const auto *variable = Variable::parseAndFind(context, expressionContext); + const auto variable = Variable::parseAndFind(context, expressionContext); predicate->m_arguments.emplace_back(variable); } // Parse constants else { - const auto *constant = (expressionContext.problem == nullptr) + const auto constant = (expressionContext.problem == nullptr) ? Constant::parseAndFind(context, expressionContext.domain) : Constant::parseAndFind(context, *expressionContext.problem); predicate->m_arguments.emplace_back(constant); @@ -117,7 +117,7 @@ PredicatePointer Predicate::parse(Context &context, const Problem &problem) return nullptr; } - auto predicate = std::make_unique(Predicate()); + auto predicate = PredicatePointer(new Predicate); predicate->m_name = predicateName; @@ -129,7 +129,7 @@ PredicatePointer Predicate::parse(Context &context, const Problem &problem) throw utils::ParserException(parser.coordinate(), "variables not allowed in this context"); // Parse objects and constants - const auto *constant = Constant::parseAndFind(context, problem); + const auto constant = Constant::parseAndFind(context, problem); predicate->m_arguments.emplace_back(constant); } @@ -163,7 +163,7 @@ const std::string &Predicate::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &Predicate::arguments() const +const std::vector &Predicate::arguments() const { return m_arguments; } diff --git a/src/plasp/pddl/expressions/PredicateDeclaration.cpp b/src/plasp/pddl/expressions/PredicateDeclaration.cpp index a3d8f84..6823fe4 100644 --- a/src/plasp/pddl/expressions/PredicateDeclaration.cpp +++ b/src/plasp/pddl/expressions/PredicateDeclaration.cpp @@ -30,7 +30,7 @@ void PredicateDeclaration::parse(Context &context, Domain &domain) { context.parser.expect("("); - auto predicate = std::make_unique(PredicateDeclaration()); + auto predicate = PredicateDeclarationPointer(new PredicateDeclaration); predicate->m_name = context.parser.parseIdentifier(); diff --git a/src/plasp/pddl/expressions/PrimitiveType.cpp b/src/plasp/pddl/expressions/PrimitiveType.cpp index 9f23558..9bc88fe 100644 --- a/src/plasp/pddl/expressions/PrimitiveType.cpp +++ b/src/plasp/pddl/expressions/PrimitiveType.cpp @@ -61,7 +61,7 @@ void PrimitiveType::parseDeclaration(Context &context, Domain &domain) return; } - types.emplace_back(std::make_unique(typeName)); + types.emplace_back(PrimitiveTypePointer(new PrimitiveType(typeName))); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -82,7 +82,7 @@ void PrimitiveType::parseTypedDeclaration(Context &context, Domain &domain) domain.checkRequirement(Requirement::Type::Typing); // If existing, parse and store parent type - auto *parentType = parseAndFind(context, domain); + auto parentType = parseAndFind(context, domain); parentType->setDirty(false); @@ -100,7 +100,7 @@ void PrimitiveType::parseTypedDeclaration(Context &context, Domain &domain) //////////////////////////////////////////////////////////////////////////////////////////////////// -PrimitiveType *PrimitiveType::parseAndFind(Context &context, Domain &domain) +PrimitiveTypePointer PrimitiveType::parseAndFind(Context &context, Domain &domain) { auto &parser = context.parser; @@ -125,7 +125,7 @@ PrimitiveType *PrimitiveType::parseAndFind(Context &context, Domain &domain) if (typeName == "object" || typeName == "objects") { context.logger.logWarning(parser.coordinate(), "primitive type “" + typeName + "” should be declared"); - types.emplace_back(std::make_unique(typeName)); + types.emplace_back(PrimitiveTypePointer(new PrimitiveType(typeName))); } else throw utils::ParserException(parser.coordinate(), "type “" + typeName + "” used but never declared"); @@ -162,7 +162,7 @@ const std::string &PrimitiveType::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &PrimitiveType::parentTypes() const +const std::vector &PrimitiveType::parentTypes() const { return m_parentTypes; } diff --git a/src/plasp/pddl/expressions/Type.cpp b/src/plasp/pddl/expressions/Type.cpp index 49b81f0..e4cab3f 100644 --- a/src/plasp/pddl/expressions/Type.cpp +++ b/src/plasp/pddl/expressions/Type.cpp @@ -17,7 +17,7 @@ namespace expressions // //////////////////////////////////////////////////////////////////////////////////////////////////// -const Expression *parseExistingPrimitiveType(Context &context, ExpressionContext &expressionContext) +ExpressionPointer parseExistingPrimitiveType(Context &context, ExpressionContext &expressionContext) { return PrimitiveType::parseAndFind(context, expressionContext.domain); } diff --git a/src/plasp/pddl/expressions/Unsupported.cpp b/src/plasp/pddl/expressions/Unsupported.cpp index fdff330..3b38400 100644 --- a/src/plasp/pddl/expressions/Unsupported.cpp +++ b/src/plasp/pddl/expressions/Unsupported.cpp @@ -19,7 +19,7 @@ UnsupportedPointer Unsupported::parse(Context &context) { auto &parser = context.parser; - auto expression = std::make_unique(Unsupported()); + auto expression = UnsupportedPointer(new Unsupported); parser.expect("("); diff --git a/src/plasp/pddl/expressions/Variable.cpp b/src/plasp/pddl/expressions/Variable.cpp index 9e137e8..81255a7 100644 --- a/src/plasp/pddl/expressions/Variable.cpp +++ b/src/plasp/pddl/expressions/Variable.cpp @@ -41,7 +41,7 @@ void Variable::parseDeclaration(Context &context, Variables ¶meters) parser.expect("?"); - auto variable = std::make_unique(Variable()); + auto variable = VariablePointer(new Variable); variable->m_name = parser.parseIdentifier(); @@ -58,7 +58,7 @@ void Variable::parseDeclaration(Context &context, Variables ¶meters) // Flag variable for potentially upcoming type declaration variable->setDirty(); - parameters.emplace_back(std::move(variable)); + parameters.emplace_back(variable); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -80,7 +80,7 @@ void Variable::parseTypedDeclaration(Context &context, ExpressionContext &expres return; const auto setType = - [&](const auto *type) + [&](ExpressionPointer type) { // Set the argument type for all previously flagged arguments std::for_each(variables.begin(), variables.end(), @@ -96,17 +96,14 @@ void Variable::parseTypedDeclaration(Context &context, ExpressionContext &expres parser.skipWhiteSpace(); - // Parse argument of "either" type (always begins with opening parenthesis) - if ((variable->m_eitherExpression = Either::parse(context, expressionContext, parseExistingPrimitiveType))) - { - setType(variable->m_eitherExpression.get()); - return; - } + // Parse argument if it has "either" type (always begins with opening parenthesis) + variable->m_type = Either::parse(context, expressionContext, parseExistingPrimitiveType); - // Parse primitive type - const auto *type = PrimitiveType::parseAndFind(context, expressionContext.domain); + // Else, try parsing it as a primitive type + if (!variable->m_type) + variable->m_type = PrimitiveType::parseAndFind(context, expressionContext.domain); - setType(type); + setType(variable->m_type); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -138,7 +135,7 @@ void Variable::parseTypedDeclarations(Context &context, ExpressionContext &expre //////////////////////////////////////////////////////////////////////////////////////////////////// -const Variable *Variable::parseAndFind(Context &context, const ExpressionContext &expressionContext) +VariablePointer Variable::parseAndFind(Context &context, const ExpressionContext &expressionContext) { auto &parser = context.parser; @@ -178,7 +175,14 @@ const std::string &Variable::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const Expression *Variable::type() const +void Variable::setType(ExpressionPointer type) +{ + m_type = type; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +ExpressionPointer Variable::type() const { return m_type; } @@ -199,13 +203,6 @@ bool Variable::isDirty() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void Variable::setType(const Expression *type) -{ - m_type = type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - ExpressionPointer Variable::normalize() { return nullptr; diff --git a/tests/TestPDDLNormalization.cpp b/tests/TestPDDLNormalization.cpp index fb61426..add8b97 100644 --- a/tests/TestPDDLNormalization.cpp +++ b/tests/TestPDDLNormalization.cpp @@ -11,14 +11,14 @@ using namespace plasp::pddl; TEST(PDDLNormalizationTests, Implication) { - auto i = std::make_unique(); - auto d1 = std::make_unique(); + auto i = expressions::ImplyPointer(new expressions::Imply); + auto d1 = expressions::DummyPointer(new expressions::Dummy); const auto d1p = d1.get(); - auto d2 = std::make_unique(); + auto d2 = expressions::DummyPointer(new expressions::Dummy); const auto d2p = d2.get(); - i->setArgument<0>(std::move(d1)); - i->setArgument<1>(std::move(d2)); + i->setArgument<0>(d1); + i->setArgument<1>(d2); auto normalized = i->normalize(); @@ -40,9 +40,9 @@ TEST(PDDLNormalizationTests, Implication) TEST(PDDLNormalizationTests, DoubleNegation) { - auto n1 = std::make_unique(); - auto n2 = std::make_unique(); - auto d = std::make_unique(); + auto n1 = expressions::NotPointer(new expressions::Not); + auto n2 = expressions::NotPointer(new expressions::Not); + auto d = expressions::DummyPointer(new expressions::Dummy); const auto dp = d.get(); n2->setArgument(std::move(d));