From 2b838d4b546bd32e16a2e626e49fc5e9b58aa09d Mon Sep 17 00:00:00 2001
From: Adam Wardynski <award@.(B-4.4.46a)>
Date: Fri, 19 Nov 2010 19:12:33 +0100
Subject: [PATCH] Refactoring the distinction of Equals for Position. The
 Equals<T> now uses helper class EqualityComparer<T> and distinction is in the
 helper class specialisation.

---
 libwccl/ops/equals.h | 87 +++++++++++++++++++-------------------------
 1 file changed, 38 insertions(+), 49 deletions(-)

diff --git a/libwccl/ops/equals.h b/libwccl/ops/equals.h
index 1c29644..3aea68b 100644
--- a/libwccl/ops/equals.h
+++ b/libwccl/ops/equals.h
@@ -9,57 +9,50 @@
 namespace Wccl {
 
 /**
- * Predicate that checks for equality of values
+ * Helper template class to factor out equality comparison
+ * (namely for special treatment of Position). By default,
+ * T::equals(const T& other) is used.
  */
 template <class T>
-class Equals : public Predicate {
+class EqualityComparer
+{
 public:
-	typedef boost::shared_ptr<Function<T> > ArgFunctionPtr;
-
-	Equals(const ArgFunctionPtr& arg1_expr, const ArgFunctionPtr& arg2_expr)
-		: arg1_expr_(arg1_expr), arg2_expr_(arg2_expr)
+	static boost::shared_ptr<const Value> apply(
+			const T& arg1,
+			const T& arg2,
+			const FunExecContext& context)
 	{
-		BOOST_ASSERT(arg1_expr_);
-		BOOST_ASSERT(arg2_expr_);
-	}
-
-	virtual std::string to_string(const Corpus2::Tagset& tagset) const {
-		return BinaryFunctionFormatter::to_string(tagset, *this, *arg1_expr_, *arg2_expr_);
-	}
-
-	virtual std::string to_raw_string() const {
-		return BinaryFunctionFormatter::to_raw_string(*this, *arg1_expr_, *arg2_expr_);
+		return Predicate::evaluate(arg1.equals(arg2), context);
 	}
+};
 
-	virtual const std::string raw_operator_name() const {
-		return "equals";
-	}
-
-protected:
-	const ArgFunctionPtr arg1_expr_;
-	const ArgFunctionPtr arg2_expr_;
-
-	/**
-	 * Take values of arguments from expressions and return True if they are equal,
-	 * False otherwise.
-	 */
-	virtual BaseRetValPtr apply_internal(const FunExecContext& context) const {
-		const boost::shared_ptr<const T>& arg1 = this->arg1_expr_->apply(context);
-		const boost::shared_ptr<const T>& arg2 = this->arg2_expr_->apply(context);
-		if(arg1->equals(*arg2)) {
-			return Predicate::True(context);
-		}
-		return Predicate::False(context);
+/**
+ * Helper template class to factor out equality comparison.
+ * This is specialization for Position that uses
+ * Position::equals(const Position& other, const SentenceContext& context)
+ */
+template <>
+class EqualityComparer<Position>
+{
+public:
+	static boost::shared_ptr<const Value> apply(
+			const Position& arg1,
+			const Position& arg2,
+			const FunExecContext& context)
+	{
+		return Predicate::evaluate(
+				arg1.equals(arg2, context.sentence_context()),
+				context);
 	}
 };
 
 /**
- * Predicate that checks for equality of Positions, given sentence context
+ * Predicate that checks for equality of values
  */
-template <>
-class Equals<Position> : public Predicate {
+template <class T>
+class Equals : public Predicate {
 public:
-	typedef boost::shared_ptr<Function<Position> > ArgFunctionPtr;
+	typedef boost::shared_ptr<Function<T> > ArgFunctionPtr;
 
 	Equals(const ArgFunctionPtr& arg1_expr, const ArgFunctionPtr& arg2_expr)
 		: arg1_expr_(arg1_expr), arg2_expr_(arg2_expr)
@@ -84,19 +77,15 @@ protected:
 	const ArgFunctionPtr arg1_expr_;
 	const ArgFunctionPtr arg2_expr_;
 
-	typedef FunctionBase::BaseRetValPtr BaseRetValPtr;
-
 	/**
-	 * Take values of arguments from expressions and return True if Positions
-	 * are equal, in given context, False otherwise.
+	 * Take values of arguments from expressions and return True if they are equal,
+	 * False otherwise.
 	 */
 	virtual BaseRetValPtr apply_internal(const FunExecContext& context) const {
-		const boost::shared_ptr<const Position>& arg1 = this->arg1_expr_->apply(context);
-		const boost::shared_ptr<const Position>& arg2 = this->arg2_expr_->apply(context);
-		if(arg1->equals(*arg2, context.sentence_context())) {
-			return Predicate::True(context);
-		}
-		return Predicate::False(context);
+		return EqualityComparer<T>::apply(
+			*(this->arg1_expr_->apply(context)),
+			*(this->arg2_expr_->apply(context)),
+			context);
 	}
 };
 
-- 
GitLab