Refactored expressions thanks to intrusive pointers.

This commit is contained in:
2016-09-04 18:48:46 +02:00
parent 9afabacde3
commit 7aa20a5820
15 changed files with 56 additions and 111 deletions

View File

@@ -34,17 +34,15 @@ class At: public ExpressionCRTP<At>
size_t timePoint() const;
void setArgument(const Expression *argument);
void setArgument(ExpressionPointer &&argument);
const Expression *argument() const;
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
protected:
size_t m_timePoint;
const Expression *m_argument;
ExpressionPointer m_argumentStorage;
ExpressionPointer m_argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -28,17 +28,13 @@ class Binary: public ExpressionCRTP<Derived>
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
template<size_t i>
void setArgument(const Expression *argument);
template<size_t i>
void setArgument(ExpressionPointer &&argument);
const std::array<const Expression *, 2> &arguments() const;
void setArgument(size_t i, ExpressionPointer argument);
const std::array<ExpressionPointer, 2> &arguments() const;
ExpressionPointer normalize() override;
protected:
std::array<const Expression *, 2> m_arguments;
std::array<ExpressionPointer, 2> m_argumentStorage;
std::array<ExpressionPointer, 2> m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -63,8 +59,8 @@ boost::intrusive_ptr<Derived> Binary<Derived>::parse(Context &context,
// Assume that expression identifier (imply, exists, etc.) is already parsed
// Parse arguments of the expression
expression->Binary<Derived>::setArgument<0>(parseExpression(context, expressionContext));
expression->Binary<Derived>::setArgument<1>(parseExpression(context, expressionContext));
expression->Binary<Derived>::setArgument(0, parseExpression(context, expressionContext));
expression->Binary<Derived>::setArgument(1, parseExpression(context, expressionContext));
parser.expect<std::string>(")");
@@ -74,31 +70,17 @@ boost::intrusive_ptr<Derived> Binary<Derived>::parse(Context &context,
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
template<size_t i>
void Binary<Derived>::setArgument(const Expression *expression)
void Binary<Derived>::setArgument(size_t i, ExpressionPointer expression)
{
static_assert(i <= 2, "Index out of range");
BOOST_ASSERT_MSG(i <= 2, "Index out of range");
m_argumentStorage[i] = nullptr;
m_arguments[i] = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
template<size_t i>
void Binary<Derived>::setArgument(ExpressionPointer &&expression)
{
static_assert(i <= 2, "Index out of range");
m_argumentStorage[i] = std::move(expression);
m_arguments[i] = m_argumentStorage[i].get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
const std::array<const Expression *, 2> &Binary<Derived>::arguments() const
const std::array<ExpressionPointer, 2> &Binary<Derived>::arguments() const
{
return m_arguments;
}
@@ -108,18 +90,17 @@ const std::array<const Expression *, 2> &Binary<Derived>::arguments() const
template<class Derived>
inline ExpressionPointer Binary<Derived>::normalize()
{
for (size_t i = 0; i < m_argumentStorage.size(); i++)
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_argumentStorage[i]);
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_argumentStorage[i]->normalize();
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_argumentStorage[i] = std::move(normalizedArgument);
m_arguments[i] = m_argumentStorage[i].get();
m_arguments[i] = std::move(normalizedArgument);
}
return nullptr;

View File

@@ -28,15 +28,13 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
void addArgument(const Expression *argument);
void addArgument(ExpressionPointer &&argument);
const std::vector<const Expression *> &arguments() const;
void addArgument(ExpressionPointer argument);
const Expressions &arguments() const;
ExpressionPointer normalize() override;
protected:
std::vector<const Expression *> m_arguments;
Expressions m_argumentStorage;
Expressions m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -81,7 +79,7 @@ boost::intrusive_ptr<Derived> NAry<Derived>::parse(Context &context,
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::addArgument(const Expression *argument)
void NAry<Derived>::addArgument(ExpressionPointer argument)
{
if (!argument)
return;
@@ -92,19 +90,7 @@ void NAry<Derived>::addArgument(const Expression *argument)
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::addArgument(ExpressionPointer &&argument)
{
if (!argument)
return;
m_argumentStorage.emplace_back(std::move(argument));
m_arguments.emplace_back(m_argumentStorage.back().get());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
const std::vector<const Expression *> &NAry<Derived>::arguments() const
const Expressions &NAry<Derived>::arguments() const
{
return m_arguments;
}
@@ -114,18 +100,17 @@ const std::vector<const Expression *> &NAry<Derived>::arguments() const
template<class Derived>
inline ExpressionPointer NAry<Derived>::normalize()
{
for (size_t i = 0; i < m_argumentStorage.size(); i++)
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_argumentStorage[i]);
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_argumentStorage[i]->normalize();
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_argumentStorage[i] = std::move(normalizedArgument);
m_arguments[i] = m_argumentStorage[i].get();
m_arguments[i] = std::move(normalizedArgument);
}
return nullptr;

View File

@@ -29,15 +29,13 @@ class Not: public ExpressionCRTP<Not>
public:
Not();
void setArgument(const Expression *argument);
void setArgument(ExpressionPointer &&argument);
const Expression *argument() const;
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
protected:
const Expression *m_argument;
ExpressionPointer m_argumentStorage;
ExpressionPointer m_argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

@@ -33,7 +33,7 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
PrimitiveType(std::string name);
const std::string &name() const;
const std::vector<PrimitiveTypePointer> &parentTypes() const;
const PrimitiveTypes &parentTypes() const;
ExpressionPointer normalize() override;
@@ -45,7 +45,7 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
std::string m_name;
std::vector<PrimitiveTypePointer> m_parentTypes;
PrimitiveTypes m_parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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