Switched to intrusive pointers for much easier maintenance.

This commit is contained in:
2016-09-04 18:26:02 +02:00
parent f10f4ac29c
commit 9afabacde3
22 changed files with 118 additions and 176 deletions

View File

@@ -38,8 +38,8 @@ class Action
std::string m_name;
expressions::Variables m_parameters;
std::unique_ptr<Expression> m_precondition;
std::unique_ptr<Expression> m_effect;
ExpressionPointer m_precondition;
ExpressionPointer m_effect;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -1,7 +1,7 @@
#ifndef __PLASP__PDDL__EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION_H
#include <memory>
#include <boost/intrusive_ptr.hpp>
#include <plasp/utils/Parser.h>
@@ -23,55 +23,58 @@ class ExpressionVisitor;
class Problem;
class Expression;
using ExpressionPointer = std::unique_ptr<Expression>;
using ExpressionPointer = boost::intrusive_ptr<Expression>;
using Expressions = std::vector<ExpressionPointer>;
namespace expressions
{
class And;
using AndPointer = std::unique_ptr<And>;
using AndPointer = boost::intrusive_ptr<And>;
class At;
using AtPointer = std::unique_ptr<At>;
using AtPointer = boost::intrusive_ptr<At>;
class Constant;
using ConstantPointer = std::unique_ptr<Constant>;
using ConstantPointer = boost::intrusive_ptr<Constant>;
using Constants = std::vector<ConstantPointer>;
class Dummy;
using DummyPointer = boost::intrusive_ptr<Dummy>;
class Either;
using EitherPointer = std::unique_ptr<Either>;
using EitherPointer = boost::intrusive_ptr<Either>;
class Imply;
using ImplyPointer = std::unique_ptr<Imply>;
using ImplyPointer = boost::intrusive_ptr<Imply>;
class Not;
using NotPointer = std::unique_ptr<Not>;
using NotPointer = boost::intrusive_ptr<Not>;
class Or;
using OrPointer = std::unique_ptr<Or>;
using OrPointer = boost::intrusive_ptr<Or>;
class Predicate;
using PredicatePointer = std::unique_ptr<Predicate>;
using PredicatePointer = boost::intrusive_ptr<Predicate>;
using Predicates = std::vector<PredicatePointer>;
class PredicateDeclaration;
using PredicateDeclarationPointer = std::unique_ptr<PredicateDeclaration>;
using PredicateDeclarationPointer = boost::intrusive_ptr<PredicateDeclaration>;
using PredicateDeclarations = std::vector<PredicateDeclarationPointer>;
class PrimitiveType;
using PrimitiveTypePointer = std::unique_ptr<PrimitiveType>;
using PrimitiveTypePointer = boost::intrusive_ptr<PrimitiveType>;
using PrimitiveTypes = std::vector<PrimitiveTypePointer>;
template<class Type>
class Reference;
template<class Type>
using ReferencePointer = std::unique_ptr<Reference<Type>>;
using ReferencePointer = boost::intrusive_ptr<Reference<Type>>;
class Unsupported;
using UnsupportedPointer = std::unique_ptr<Unsupported>;
using UnsupportedPointer = boost::intrusive_ptr<Unsupported>;
class Variable;
using VariablePointer = std::unique_ptr<Variable>;
using VariablePointer = boost::intrusive_ptr<Variable>;
using Variables = std::vector<VariablePointer>;
}
@@ -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 Derived>
class ExpressionCRTP: public Expression
{

View File

@@ -83,7 +83,7 @@ AtPointer At::parse(Context &context, ExpressionContext &expressionContext,
return nullptr;
}
auto expression = std::make_unique<At>(At());
auto expression = AtPointer(new At);
expression->m_timePoint = timePoint;

View File

@@ -24,7 +24,7 @@ class Binary: public ExpressionCRTP<Derived>
{
public:
template<typename ExpressionParser>
static std::unique_ptr<Derived> parse(Context &context,
static boost::intrusive_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
@@ -45,7 +45,7 @@ class Binary: public ExpressionCRTP<Derived>
template<class Derived>
template<typename ExpressionParser>
std::unique_ptr<Derived> Binary<Derived>::parse(Context &context,
boost::intrusive_ptr<Derived> Binary<Derived>::parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression)
{
auto &parser = context.parser;
@@ -59,7 +59,7 @@ std::unique_ptr<Derived> Binary<Derived>::parse(Context &context,
return nullptr;
}
auto expression = std::make_unique<Derived>();
auto expression = boost::intrusive_ptr<Derived>(new Derived);
// Assume that expression identifier (imply, exists, etc.) is already parsed
// Parse arguments of the expression

View File

@@ -28,12 +28,12 @@ class Constant: public ExpressionCRTP<Constant>
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<Constant>
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;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -24,7 +24,7 @@ class NAry: public ExpressionCRTP<Derived>
{
public:
template<typename ExpressionParser>
static std::unique_ptr<Derived> parse(Context &context,
static boost::intrusive_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
@@ -43,7 +43,7 @@ class NAry: public ExpressionCRTP<Derived>
template<class Derived>
template<typename ExpressionParser>
std::unique_ptr<Derived> NAry<Derived>::parse(Context &context,
boost::intrusive_ptr<Derived> NAry<Derived>::parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression)
{
auto &parser = context.parser;
@@ -57,7 +57,7 @@ std::unique_ptr<Derived> NAry<Derived>::parse(Context &context,
return nullptr;
}
auto expression = std::make_unique<Derived>();
auto expression = boost::intrusive_ptr<Derived>(new Derived);
parser.skipWhiteSpace();

View File

@@ -57,7 +57,7 @@ NotPointer Not::parse(Context &context, ExpressionContext &expressionContext,
return nullptr;
}
auto expression = std::make_unique<Not>(Not());
auto expression = NotPointer(new Not);
context.parser.skipWhiteSpace();

View File

@@ -26,7 +26,7 @@ class Predicate: public ExpressionCRTP<Predicate>
public:
const std::string &name() const;
const std::vector<const Expression *> &arguments() const;
const std::vector<ExpressionPointer> &arguments() const;
bool isDeclared() const;
@@ -40,7 +40,7 @@ class Predicate: public ExpressionCRTP<Predicate>
bool m_isDeclared;
std::string m_name;
std::vector<const Expression *> m_arguments;
std::vector<ExpressionPointer> m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -26,14 +26,14 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
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<const PrimitiveType *> &parentTypes() const;
const std::vector<PrimitiveTypePointer> &parentTypes() const;
ExpressionPointer normalize() override;
@@ -45,7 +45,7 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
std::string m_name;
std::vector<const PrimitiveType *> m_parentTypes;
std::vector<PrimitiveTypePointer> m_parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -1,75 +0,0 @@
#ifndef __PLASP__PDDL__EXPRESSIONS__REFERENCE_H
#define __PLASP__PDDL__EXPRESSIONS__REFERENCE_H
#include <plasp/pddl/Expression.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Reference
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
class Reference: public ExpressionCRTP<Reference<Type>>
{
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<class Type>
Reference<Type>::Reference(Type *value)
: m_value{value}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
Type *Reference<Type>::get()
{
return m_value;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
const Type *Reference<Type>::get() const
{
return m_value;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
ExpressionPointer Reference<Type>::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@@ -16,7 +16,7 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const Expression *parseExistingPrimitiveType(Context &context,
ExpressionPointer parseExistingPrimitiveType(Context &context,
ExpressionContext &expressionContext);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -24,20 +24,19 @@ class Variable: public ExpressionCRTP<Variable>
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<Variable>
std::string m_name;
const Expression *m_type;
// Stores "either" expression if necessary
ExpressionPointer m_eitherExpression;
ExpressionPointer m_type;
};
////////////////////////////////////////////////////////////////////////////////////////////////////