From c776170a10e46f9fd5bd9a6477c3a123e80ac785 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 5 Aug 2015 13:19:49 +0200
Subject: [PATCH] upgrade raw composer to new dispatch visitor

---
 alib2raw/src/RawApi.cpp                 |  2 -
 alib2raw/src/RawApi.hpp                 |  5 --
 alib2raw/src/tree/TreeToRawComposer.cpp | 70 +++++++------------------
 alib2raw/src/tree/TreeToRawComposer.h   | 49 +++++++----------
 araw2/src/araw.cpp                      |  2 +-
 5 files changed, 38 insertions(+), 90 deletions(-)

diff --git a/alib2raw/src/RawApi.cpp b/alib2raw/src/RawApi.cpp
index fc57e691c4..b0abd0f94f 100644
--- a/alib2raw/src/RawApi.cpp
+++ b/alib2raw/src/RawApi.cpp
@@ -11,6 +11,4 @@ namespace alib {
 
 const tree::TreeFromRawParser FromRawParsers::treeParser;
 
-const tree::TreeToRawComposer ToRawComposers::treeComposer;
-
 } /* namespace alib */
diff --git a/alib2raw/src/RawApi.hpp b/alib2raw/src/RawApi.hpp
index 384c435803..4c6b2f15fc 100644
--- a/alib2raw/src/RawApi.hpp
+++ b/alib2raw/src/RawApi.hpp
@@ -19,11 +19,6 @@ public:
 
 };
 
-class ToRawComposers {
-public:
-	static const tree::TreeToRawComposer treeComposer;
-};
-
 } /* namespace alib */
 
 #endif /* RAW_API_HPP_ */
diff --git a/alib2raw/src/tree/TreeToRawComposer.cpp b/alib2raw/src/tree/TreeToRawComposer.cpp
index 8f4ca13712..14ead6bc6c 100644
--- a/alib2raw/src/tree/TreeToRawComposer.cpp
+++ b/alib2raw/src/tree/TreeToRawComposer.cpp
@@ -11,33 +11,37 @@
 
 namespace tree {
 
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const TreeBase& tree) const {
-	tree.Accept((void*) &out, tree::TreeToRawComposer::TREE_TO_RAW_COMPOSER);
+void TreeToRawComposer::compose(std::deque<sax::Token>& out, const Tree& tree) {
+	getInstance().dispatch(out, tree.getData());
 }
 
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const Tree& tree) const {
-	tree.getData().Accept((void*) &out, tree::TreeToRawComposer::TREE_TO_RAW_COMPOSER);
-}
-
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const RankedTree& tree) const {
+void TreeToRawComposer::compose(std::deque<sax::Token>& out, const RankedTree& tree) {
 	composeNode(out, tree.getRoot());
 }
 
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const PrefixRankedTree& tree) const {
+TreeToRawComposer::RegistratorWrapper<void, RankedTree> TreeToRawComposerRankedTree = TreeToRawComposer::RegistratorWrapper<void, RankedTree>(TreeToRawComposer::getInstance(), TreeToRawComposer::compose);
+
+void TreeToRawComposer::compose(std::deque<sax::Token>& out, const PrefixRankedTree& tree) {
 	unsigned i = 0;
 	composePrefixRankedNotation(out, i, tree.getContent());
 }
 
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const PrefixRankedBarTree& tree) const {
+TreeToRawComposer::RegistratorWrapper<void, PrefixRankedTree> TreeToRawComposerPrefixRankedTree = TreeToRawComposer::RegistratorWrapper<void, PrefixRankedTree>(TreeToRawComposer::getInstance(), TreeToRawComposer::compose);
+
+void TreeToRawComposer::compose(std::deque<sax::Token>& out, const PrefixRankedBarTree& tree) {
 	unsigned i = 0;
 	composePrefixRankedBarNotation(out, i, tree.getContent());
 }
 
-void TreeToRawComposer::compose(std::deque<sax::Token>& out, const UnrankedTree& tree) const {
+TreeToRawComposer::RegistratorWrapper<void, PrefixRankedBarTree> TreeToRawComposerPrefixRankedBarTree = TreeToRawComposer::RegistratorWrapper<void, PrefixRankedBarTree>(TreeToRawComposer::getInstance(), TreeToRawComposer::compose);
+
+void TreeToRawComposer::compose(std::deque<sax::Token>& out, const UnrankedTree& tree) {
 	composeNode(out, tree.getRoot());
 }
 
-void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const RankedNode& node) const {
+TreeToRawComposer::RegistratorWrapper<void, UnrankedTree> TreeToRawComposerUnrankedTree = TreeToRawComposer::RegistratorWrapper<void, UnrankedTree>(TreeToRawComposer::getInstance(), TreeToRawComposer::compose);
+
+void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const RankedNode& node) {
 	if(node.getChildren().size() == 0) {
 		out.emplace_back(sax::Token((std::string) node.getSymbol(), sax::Token::TokenType::CHARACTER));
 	} else {
@@ -50,7 +54,7 @@ void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const RankedNod
 	}
 }
 
-void TreeToRawComposer::composePrefixRankedNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) const {
+void TreeToRawComposer::composePrefixRankedNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) {
 	unsigned size = notation[index].getRank().getData();
 	unsigned root = index;
 	if(size == 0) {
@@ -68,7 +72,7 @@ void TreeToRawComposer::composePrefixRankedNotation(std::deque<sax::Token>& out,
 	}
 }
 
-void TreeToRawComposer::composePrefixRankedBarNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) const {
+void TreeToRawComposer::composePrefixRankedBarNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) {
 	unsigned size = notation[index].getRank().getData();
 	unsigned root = index;
 	if(size == 0) {
@@ -87,7 +91,7 @@ void TreeToRawComposer::composePrefixRankedBarNotation(std::deque<sax::Token>& o
 	}
 }
 
-void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const UnrankedNode& node) const {
+void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const UnrankedNode& node) {
 	if(node.getChildren().size() == 0) {
 
 	} else {
@@ -100,43 +104,5 @@ void TreeToRawComposer::composeNode(std::deque<sax::Token>& out, const UnrankedN
 	}
 }
 
-void TreeToRawComposer::Visit(void* userData, const RankedTree& tree) const {
-	std::deque<sax::Token> &res = *((std::deque<sax::Token>*) userData);
-	this->compose(res, tree);
-}
-
-void TreeToRawComposer::Visit(void*, const RankedPattern&) const {
-	throw tree::TreeException("Cant compose RankedPattern to raw representation");
-}
-
-void TreeToRawComposer::Visit(void* userData, const PrefixRankedTree& tree) const {
-	std::deque<sax::Token> &res = *((std::deque<sax::Token>*) userData);
-	this->compose(res, tree);
-}
-
-void TreeToRawComposer::Visit(void* userData, const PrefixRankedBarTree& tree) const {
-	std::deque<sax::Token> &res = *((std::deque<sax::Token>*) userData);
-	this->compose(res, tree);
-}
-
-void TreeToRawComposer::Visit(void*, const PrefixRankedPattern&) const {
-	throw tree::TreeException("Cant compose PrefixRankedPattern to raw representation");
-}
-
-void TreeToRawComposer::Visit(void*, const PrefixRankedBarPattern&) const {
-	throw tree::TreeException("Cant compose PrefixRankedBarPattern to raw representation");
-}
-
-void TreeToRawComposer::Visit(void* userData, const UnrankedTree& tree) const {
-	std::deque<sax::Token> &res = *((std::deque<sax::Token>*) userData);
-	this->compose(res, tree);
-}
-
-void TreeToRawComposer::Visit(void*, const UnrankedPattern&) const {
-	throw tree::TreeException("Cant compose UnrankedPattern to raw representation");
-}
-
-const TreeToRawComposer TreeToRawComposer::TREE_TO_RAW_COMPOSER;
-
 } /* namespace automaton */
 
diff --git a/alib2raw/src/tree/TreeToRawComposer.h b/alib2raw/src/tree/TreeToRawComposer.h
index fbf1354311..f017f79c0d 100644
--- a/alib2raw/src/tree/TreeToRawComposer.h
+++ b/alib2raw/src/tree/TreeToRawComposer.h
@@ -9,7 +9,10 @@
 #define TREE_TO_RAW_COMPOSER_H_
 
 #include <deque>
+#include <common/multipleDispatch.hpp>
 #include "tree/Tree.h"
+#include "tree/TreeFeatures.h"
+#include "alphabet/SymbolFeatures.h"
 #include "sax/Token.h"
 #include <vector>
 
@@ -18,40 +21,26 @@ namespace tree {
 /**
  * This class contains methods to print XML representation of tree to the output stream.
  */
-class TreeToRawComposer : public VisitableTreeBase::const_visitor_type {
+class TreeToRawComposer : public std::SingleDispatchFirstStaticParam<void, std::deque<sax::Token>, TreeBase> {
 public:
-	TreeToRawComposer() {}
+	static void compose(std::deque<sax::Token>& out, const Tree& tree);
 
-	void Visit(void*, const RankedTree& tree) const;
-	void Visit(void*, const RankedPattern& tree) const;
-	void Visit(void*, const PrefixRankedTree& tree) const;
-	void Visit(void*, const PrefixRankedBarTree& tree) const;
-	void Visit(void*, const PrefixRankedPattern& tree) const;
-	void Visit(void*, const PrefixRankedBarPattern& tree) const;
-	void Visit(void*, const UnrankedTree& tree) const;
-	void Visit(void*, const UnrankedPattern& tree) const;
-
-	void compose(std::deque<sax::Token>& out, const Tree& tree) const;
-
-	void compose(std::deque<sax::Token>& out, const RankedTree& tree) const;
-	void compose(std::deque<sax::Token>& out, const PrefixRankedTree& tree) const;
-	void compose(std::deque<sax::Token>& out, const PrefixRankedBarTree& tree) const;
-	void compose(std::deque<sax::Token>& out, const UnrankedTree& tree) const;
+	static void compose(std::deque<sax::Token>& out, const RankedTree& tree);
+	static void compose(std::deque<sax::Token>& out, const PrefixRankedTree& tree);
+	static void compose(std::deque<sax::Token>& out, const PrefixRankedBarTree& tree);
+	static void compose(std::deque<sax::Token>& out, const UnrankedTree& tree);
 
 private:
-	/**
-	 * Prints XML representation of Tree to the output stream.
-	 * @param tree tree to print
-	 * @return list of xml tokens representing the tree
-	 */
-	void compose(std::deque<sax::Token>& out, const TreeBase& tree) const;
-
-	void composeNode(std::deque<sax::Token>& out, const RankedNode& node) const;
-	void composePrefixRankedNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) const;
-	void composePrefixRankedBarNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation) const;
-	void composeNode(std::deque<sax::Token>& out, const UnrankedNode& node) const;
-
-	static const TreeToRawComposer TREE_TO_RAW_COMPOSER;
+	static void composeNode(std::deque<sax::Token>& out, const RankedNode& node);
+	static void composePrefixRankedNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation);
+	static void composePrefixRankedBarNotation(std::deque<sax::Token>& out, unsigned& index, const std::vector<alphabet::RankedSymbol>& notation);
+	static void composeNode(std::deque<sax::Token>& out, const UnrankedNode& node);
+
+public:
+	static TreeToRawComposer& getInstance() {
+		static TreeToRawComposer res;
+		return res;
+	}
 };
 
 } /* namespace tree */
diff --git a/araw2/src/araw.cpp b/araw2/src/araw.cpp
index dca118b4e5..85f41ce5ba 100644
--- a/araw2/src/araw.cpp
+++ b/araw2/src/araw.cpp
@@ -87,7 +87,7 @@ int main(int argc, char** argv) {
 			std::chrono::measurements::start("Output write", std::chrono::measurements::Type::AUXILARY);
 
 			std::deque<sax::Token> tokens2;
-			alib::ToRawComposers::treeComposer.compose(tokens2, tree);
+			tree::TreeToRawComposer::compose(tokens2, tree);
 			sax::SaxComposeInterface::printStdout(tokens2);
 		} else {
 			throw exception::AlibException("Invalid input format specified");
-- 
GitLab