From f0a461c1e6f772281def5f1aac78270e439bdebd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adam=20Wardy=C5=84ski?= <no@email>
Date: Sat, 6 Nov 2010 00:50:37 +0100
Subject: [PATCH] Helper formatters for string representation of unary and
 binary functions

---
 libwccl/CMakeLists.txt     |   3 +-
 libwccl/ops/formatters.cpp |  65 +++++++++++++++++++
 libwccl/ops/formatters.h   |  77 ++++++++++++++++++++++
 libwccl/ops/functions.h    | 127 -------------------------------------
 4 files changed, 144 insertions(+), 128 deletions(-)
 create mode 100644 libwccl/ops/formatters.cpp
 create mode 100644 libwccl/ops/formatters.h

diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt
index 00ed846..2543ebe 100644
--- a/libwccl/CMakeLists.txt
+++ b/libwccl/CMakeLists.txt
@@ -17,9 +17,10 @@ SET(libwccl_STAT_SRC
 	exception.cpp
 	main.cpp
 	ops/and.cpp
-	ops/or.cpp
+	ops/formatters.cpp
 	ops/logicalpredicate.cpp
     ops/nor.cpp
+	ops/or.cpp
 	ops/predicate.cpp
 	sentencecontext.cpp
 	values/bool.cpp
diff --git a/libwccl/ops/formatters.cpp b/libwccl/ops/formatters.cpp
new file mode 100644
index 0000000..4adf2da
--- /dev/null
+++ b/libwccl/ops/formatters.cpp
@@ -0,0 +1,65 @@
+#include <libwccl/ops/formatters.h>
+
+namespace Wccl {
+
+// ------ UnaryFunctionFormatter -----
+
+std::string UnaryFunctionFormatter::to_raw_string(
+	const FunctionBase& f,
+	const FunctionBase& arg_expr,
+	const char* open_bracket,
+	const char* close_bracket)
+{
+	std::string s(f.raw_operator_name());
+	s.append(open_bracket);
+	s.append(arg_expr.to_raw_string());
+	s.append(close_bracket);
+	return s;
+}
+
+std::string UnaryFunctionFormatter::to_string(
+	const Corpus2::Tagset& tagset,
+	const FunctionBase& f,
+	const FunctionBase& arg_expr,
+	const char* open_bracket,
+	const char* close_bracket)
+{
+	std::string s(f.operator_name(tagset));
+	s.append(open_bracket);
+	s.append(arg_expr.to_string(tagset));
+	s.append(close_bracket);
+	return s;
+}
+
+// ----- BinaryFunctionFormatter ------
+
+std::string BinaryFunctionFormatter::to_string(
+	const Corpus2::Tagset& tagset,
+	const FunctionBase& f,
+	const FunctionBase& arg1_expr,
+	const FunctionBase& arg2_expr)
+{
+	std::string s(f.operator_name(tagset));
+	s.append("(");
+	s.append(arg1_expr.to_string(tagset));
+	s.append(", ");
+	s.append(arg2_expr.to_string(tagset));
+	s.append(")");
+	return s;
+}
+
+std::string BinaryFunctionFormatter::to_raw_string(
+	const FunctionBase& f,
+	const FunctionBase& arg1_expr,
+	const FunctionBase& arg2_expr)
+{
+	std::string s(f.raw_operator_name());
+	s.append("(");
+	s.append(arg1_expr.to_raw_string());
+	s.append(", ");
+	s.append(arg2_expr.to_raw_string());
+	s.append(")");
+	return s;
+}
+
+} /* end ns Wccl */
diff --git a/libwccl/ops/formatters.h b/libwccl/ops/formatters.h
new file mode 100644
index 0000000..9779945
--- /dev/null
+++ b/libwccl/ops/formatters.h
@@ -0,0 +1,77 @@
+#ifndef FORMATTERS_H
+#define FORMATTERS_H
+
+#include <boost/shared_ptr.hpp>
+
+#include <libcorpus2/tagset.h>
+
+#include <libwccl/ops/functions.h>
+
+namespace Wccl {
+
+/**
+ * Class that formats unary functions as string
+ */
+struct UnaryFunctionFormatter
+{
+    /** 
+	 * String representation of an unary function.
+	 * It is in form of
+     * operator_name(argument_expression_string)
+     * although the open and close brackets can be changed
+     * (some operators use [])
+     */
+	static std::string to_string(
+		const Corpus2::Tagset& tagset,
+		const FunctionBase& f,
+		const FunctionBase& arg_expr,
+		const char* open_bracket = "(",
+		const char* close_bracket = ")");
+
+    /** 
+	 * Raw string representation of an unary function.
+	 * Does not require tagset, may contain internal info
+	 * and/or be incomplete. It is in form of
+     * raw_operator_name(raw_argument_expression_string)
+     * although the open and close brackets can be changed
+     * (some operators use [])
+     */
+	static std::string to_raw_string(
+		const FunctionBase& f,
+		const FunctionBase& arg_expr,
+		const char* open_bracket = "(",
+		const char* close_bracket = ")");
+};
+
+/**
+ * Class that formats binary functions as string
+ */
+struct BinaryFunctionFormatter
+{
+    /** 
+	 * String representation of a binary function.
+	 * It is in form of
+     * operator_name(arg1_expr_string, arg2_expr_string)
+     */
+	static std::string to_string(
+		const Corpus2::Tagset& tagset,
+		const FunctionBase& f,
+		const FunctionBase& arg1_expr,
+		const FunctionBase& arg2_expr);
+
+    /** 
+	 * Raw string representation of a binary function.
+	 * Does not require tagset, may contain internal info
+	 * and/or be incomplete. It is in form of
+     * raw_op_name(raw_arg1_expr_string, raw_arg2_expr_string)
+     */
+	static std::string to_raw_string(
+		const FunctionBase& f,
+		const FunctionBase& arg1_expr,
+		const FunctionBase& arg2_expr);
+};
+
+} /* end ns Wccl */
+
+
+#endif // FORMATTERS_H
diff --git a/libwccl/ops/functions.h b/libwccl/ops/functions.h
index 7e4779a..0c56fb6 100644
--- a/libwccl/ops/functions.h
+++ b/libwccl/ops/functions.h
@@ -51,133 +51,6 @@ public:
 	}
 };
 
-/**
- * Abstract base class template for functional WCCL operators returning
- * a value of given type, while taking a single argument of selected type.
- * The argument is actually realised as a functional expression that returns
- * value of the specified type, per the unary function requirements.
- */
-template<class TArg, class TRet>
-class UnaryFunction : public Function<TRet> {
-public:
-	/**
-	 * Shared pointer type of expression that returns argument for
-	 * our unary function application
-	 */
-	typedef boost::shared_ptr<Function<TArg> > ArgExprPtr;
-
-	UnaryFunction(const ArgExprPtr& arg_expr)
-		: arg_expr_(arg_expr)
-	{
-		BOOST_ASSERT(arg_expr_);
-	}
-
-	/**
-	 * String representation of the unary function, realised by default
-	 * as "operator_name(argument_string)" (using open and close
-	 * brackets supplied by open_bracket() and close_bracket())
-	 */
-	virtual std::string to_string(const Corpus2::Tagset& tagset) const {
-		std::string returnValue(this->operator_name(tagset));
-		returnValue.append(open_bracket());
-		returnValue.append(arg_expr_->to_string(tagset));
-		returnValue.append(close_bracket());
-		return returnValue;
-	}
-	/**
-	 * String representation of the unary function, that does not
-	 * require tagset. May be incomplete and/or contain internal info.
-	 * Realised by default as "raw_operator_name(raw_argument_string)"
-	 * (using open and close brackets supplied by open_bracket()
-	 * and close_bracket())
-	 */
-	virtual std::string to_raw_string() const {
-		std::string returnValue(this->raw_operator_name());
-		returnValue.append(open_bracket());
-		returnValue.append(arg_expr_->to_raw_string());
-		returnValue.append(close_bracket());
-		return returnValue;
-	}
-protected:
-	const ArgExprPtr arg_expr_;
-
-	/**
-	 * Opening bracket to use in string representation.
-	 * By default it is "(", but some operators use "["
-	 */
-	virtual const char* open_bracket() {
-		return "(";
-	}
-	/**
-	 * Closing bracket to use in string representation.
-	 * By default it is ")", but some operators use "]"
-	 */
-	virtual const char* close_bracket() {
-		return ")";
-	}
-};
-
-/**
- * Abstract base class template for functional WCCL operators returning
- * a value of given type, while taking two arguments of selected types.
- * The arguments are actually realised as functions that return
- * value of a specified type, per the binary function requirements.
- */
-template<class TArg1, class TArg2, class TRet>
-class BinaryFunction : public Function<TRet> {
-public:
-	/**
-	 * Shared pointer type of expression that returns first argument for
-	 * our binary function application
-	 */
-	typedef boost::shared_ptr<Function<TArg1> > Arg1ExprPtr;
-	/**
-	 * Shared pointer type of expression that returns second argument for
-	 * our binary function application
-	 */
-	typedef boost::shared_ptr<Function<TArg2> > Arg2ExprPtr;
-
-	BinaryFunction(const Arg1ExprPtr& arg1_expr, const Arg2ExprPtr& arg2_expr)
-		: arg1_expr_(arg1_expr), arg2_expr_(arg2_expr)
-	{
-		BOOST_ASSERT(arg1_expr_);
-		BOOST_ASSERT(arg2_expr_);
-	}
-
-	/**
-	 * String representation of the binary function, realised by default
-	 * as "operator_name(arg1_string, arg2_string)"
-	 */
-	virtual std::string to_string(const Corpus2::Tagset& tagset) const {
-		std::string returnValue(this->operator_name(tagset));
-		returnValue.append("(");
-		returnValue.append(arg1_expr_->to_string(tagset));
-		returnValue.append(", ");
-		returnValue.append(arg2_expr_->to_string(tagset));
-		returnValue.append(")");
-		return returnValue;
-	}
-
-	/**
-	 * String representation of the binary function, that does not
-	 * require tagset. May be incomplete and/or contain internal info.
-	 * Realised by default as "raw_operator_name(raw_arg1_string, raw_arg2_string)"
-	 */
-	virtual std::string to_raw_string() const {
-		std::string returnValue(this->raw_operator_name());
-		returnValue.append("(");
-		returnValue.append(arg1_expr_->to_raw_string());
-		returnValue.append(", ");
-		returnValue.append(arg2_expr_->to_raw_string());
-		returnValue.append(")");
-		return returnValue;
-	}
-protected:
-	const Arg1ExprPtr arg1_expr_;
-	const Arg2ExprPtr arg2_expr_;
-
-};
-
 } /* end ns Wccl */
 
 #endif // FUNCTIONS_H
-- 
GitLab