From f734bf5b8ce2d28bc00e1d0be52988891b8c5cb3 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 9 Apr 2014 21:09:32 +0200
Subject: [PATCH] Printing to ostream

---
 alib2/src/regexp/Alternation.cpp   |  8 ++++++++
 alib2/src/regexp/Alternation.h     |  5 +++++
 alib2/src/regexp/Concatenation.cpp | 12 ++++++++++--
 alib2/src/regexp/Concatenation.h   |  5 +++++
 alib2/src/regexp/Iteration.cpp     |  4 ++++
 alib2/src/regexp/Iteration.h       |  5 +++++
 alib2/src/regexp/RegExp.cpp        |  2 +-
 alib2/src/regexp/RegExpElement.cpp |  5 +++++
 alib2/src/regexp/RegExpElement.h   | 13 +++++++++++++
 alib2/src/regexp/RegExpEmpty.cpp   |  4 ++++
 alib2/src/regexp/RegExpEmpty.h     |  5 +++++
 alib2/src/regexp/RegExpEpsilon.cpp |  6 +++++-
 alib2/src/regexp/RegExpEpsilon.h   |  5 +++++
 alib2/src/regexp/RegExpSymbol.cpp  |  4 ++++
 alib2/src/regexp/RegExpSymbol.h    |  5 +++++
 15 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp
index 24377bd12e..e3fd7b1508 100644
--- a/alib2/src/regexp/Alternation.cpp
+++ b/alib2/src/regexp/Alternation.cpp
@@ -104,6 +104,14 @@ bool Alternation::operator==(const Alternation& other) const {
 	return true;
 }
 
+void Alternation::operator>>(ostream& out) const {
+	out << "(Alternation";
+	for(const auto& e : getElements())
+		out << " " << *e;
+
+	out << ")";
+}
+
 bool Alternation::containsEmptyString() const {
 	for(const auto& e : getElements())
 		if(e->containsEmptyString())
diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h
index 930dce0e97..a5e7b56b67 100644
--- a/alib2/src/regexp/Alternation.h
+++ b/alib2/src/regexp/Alternation.h
@@ -51,6 +51,11 @@ public:
 	virtual bool operator<(const Alternation&) const;
 	virtual bool operator==(const Alternation&) const;
 	
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
+
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
 	 */
diff --git a/alib2/src/regexp/Concatenation.cpp b/alib2/src/regexp/Concatenation.cpp
index b678958ac9..e2553fc756 100644
--- a/alib2/src/regexp/Concatenation.cpp
+++ b/alib2/src/regexp/Concatenation.cpp
@@ -76,7 +76,7 @@ bool Concatenation::operator<(const Concatenation& other) const {
 	auto thisIter = this->elements.begin();
 	auto otherIter = other.elements.begin();
 	for(; thisIter != this->elements.end(); thisIter++, otherIter++) {
-	  if(**thisIter != **otherIter) break;
+		if(**thisIter != **otherIter) break;
 	}
 	if(thisIter == this->elements.end()) return false;
 
@@ -89,12 +89,20 @@ bool Concatenation::operator==(const Concatenation& other) const {
 	auto thisIter = this->elements.begin();
 	auto otherIter = other.elements.begin();
 	for(; thisIter != this->elements.end(); thisIter++, otherIter++) {
-	  if(**thisIter != **otherIter) return false;
+		if(**thisIter != **otherIter) return false;
 	}
 
 	return true;
 }
 
+void Concatenation::operator>>(ostream& out) const {
+	out << "(Concatenation";
+	for(const auto& e : getElements())
+		out << " " << *e;
+
+	out << ")";
+}
+
 void Concatenation::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const {
 	for(const auto& child : this->elements)
 		child->getAlphabet(alphabet);
diff --git a/alib2/src/regexp/Concatenation.h b/alib2/src/regexp/Concatenation.h
index 6b8b10a04a..03a3115fc5 100644
--- a/alib2/src/regexp/Concatenation.h
+++ b/alib2/src/regexp/Concatenation.h
@@ -50,6 +50,11 @@ public:
 	virtual bool operator<(const Concatenation&) const;
 	virtual bool operator==(const Concatenation&) const;
 
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
+
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
 	 */
diff --git a/alib2/src/regexp/Iteration.cpp b/alib2/src/regexp/Iteration.cpp
index 888985fa09..6bd9f31625 100644
--- a/alib2/src/regexp/Iteration.cpp
+++ b/alib2/src/regexp/Iteration.cpp
@@ -93,6 +93,10 @@ bool Iteration::operator==(const Iteration& other) const {
 	return *(this->element) == *(other.element);
 }
 
+void Iteration::operator>>(ostream& out) const {
+	out << "(RegExpIteration " << *element << ")";
+}
+
 void Iteration::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const {
 	element->getAlphabet( alphabet );
 }
diff --git a/alib2/src/regexp/Iteration.h b/alib2/src/regexp/Iteration.h
index 168158461e..097e249b17 100644
--- a/alib2/src/regexp/Iteration.h
+++ b/alib2/src/regexp/Iteration.h
@@ -58,6 +58,11 @@ public:
 	virtual bool operator<(const Iteration&) const;
 	virtual bool operator==(const Iteration&) const;
 
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
+
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
 	 */
diff --git a/alib2/src/regexp/RegExp.cpp b/alib2/src/regexp/RegExp.cpp
index cc899dc63b..6d0ea276b7 100644
--- a/alib2/src/regexp/RegExp.cpp
+++ b/alib2/src/regexp/RegExp.cpp
@@ -90,7 +90,7 @@ bool RegExp::containsEmptyString() const {
 }
 
 ostream& operator <<(ostream& out, const RegExp& regexp) {
-	out << "(" << "RegExp" << " " << "Element:" << regexp.regExp << ")";
+	out << "(RegExp " << *(regexp.regExp) << ")";
 	return out;
 }
 
diff --git a/alib2/src/regexp/RegExpElement.cpp b/alib2/src/regexp/RegExpElement.cpp
index fc07c4c1f0..1f4bee9740 100644
--- a/alib2/src/regexp/RegExpElement.cpp
+++ b/alib2/src/regexp/RegExpElement.cpp
@@ -75,4 +75,9 @@ bool RegExpElement::operator==(const RegExpEmpty& other) const {
 	return false;
 }
 
+ostream& operator<<(ostream& out, const RegExpElement& regexp) {
+	regexp >> out;
+	return out;
+}
+
 } /* namespace regexp */
diff --git a/alib2/src/regexp/RegExpElement.h b/alib2/src/regexp/RegExpElement.h
index 4809f67cf1..ed283d9125 100644
--- a/alib2/src/regexp/RegExpElement.h
+++ b/alib2/src/regexp/RegExpElement.h
@@ -59,6 +59,19 @@ public:
 	virtual bool operator==(const RegExpSymbol&) const;
 	virtual bool operator==(const RegExpEpsilon&) const;
 	virtual bool operator==(const RegExpEmpty&) const;
+	
+	/**
+	 * Prints XML representation of the RegExp to the output stream.
+	 * @param out output stream to which print the RegExp
+	 */
+	virtual void operator>>(ostream& out) const = 0;
+	
+	/**
+	 * Prints XML representation of the RegExp to the output stream.
+	 * @param out output stream to which print the RegExp
+	 * @param regexp RegExp to print
+	 */
+	friend ostream& operator<<(ostream& out, const RegExpElement& regexp);
 
 	/**
 	 * @return true if this subtree of regexp matches empty string (epsilon)
diff --git a/alib2/src/regexp/RegExpEmpty.cpp b/alib2/src/regexp/RegExpEmpty.cpp
index ed76ea14b1..83d0c1f248 100644
--- a/alib2/src/regexp/RegExpEmpty.cpp
+++ b/alib2/src/regexp/RegExpEmpty.cpp
@@ -53,6 +53,10 @@ bool RegExpEmpty::operator==(const RegExpEmpty&) const {
 	return true;
 }
 
+void RegExpEmpty::operator>>(ostream& out) const {
+	out << "(RegExpEmpty)";
+}
+
 void RegExpEmpty::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const {
 
 }
diff --git a/alib2/src/regexp/RegExpEmpty.h b/alib2/src/regexp/RegExpEmpty.h
index 927842634f..150ebda6af 100644
--- a/alib2/src/regexp/RegExpEmpty.h
+++ b/alib2/src/regexp/RegExpEmpty.h
@@ -37,6 +37,11 @@ public:
 	virtual bool operator<(const RegExpEpsilon&) const;
 	virtual bool operator==(const RegExpEmpty&) const;
 	
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
+
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
 	 */
diff --git a/alib2/src/regexp/RegExpEpsilon.cpp b/alib2/src/regexp/RegExpEpsilon.cpp
index f82c17a4c5..18940c9d3d 100644
--- a/alib2/src/regexp/RegExpEpsilon.cpp
+++ b/alib2/src/regexp/RegExpEpsilon.cpp
@@ -49,7 +49,11 @@ bool RegExpEpsilon::operator==(const RegExpEpsilon&) const {
 	  return true;
 }
 
-void RegExpElement::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const {
+void RegExpEpsilon::operator>>(ostream& out) const {
+	out << "(RegExpEpsilon)";
+}
+
+void RegExpEpsilon::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const {
 
 }
 
diff --git a/alib2/src/regexp/RegExpEpsilon.h b/alib2/src/regexp/RegExpEpsilon.h
index d51af0821a..57c64dccdb 100644
--- a/alib2/src/regexp/RegExpEpsilon.h
+++ b/alib2/src/regexp/RegExpEpsilon.h
@@ -38,6 +38,11 @@ public:
 	virtual bool operator<(const RegExpSymbol&) const;
 	virtual bool operator==(const RegExpEpsilon&) const;
 	
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
+	
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
 	 */
diff --git a/alib2/src/regexp/RegExpSymbol.cpp b/alib2/src/regexp/RegExpSymbol.cpp
index 140eee9f52..9f091a5b70 100644
--- a/alib2/src/regexp/RegExpSymbol.cpp
+++ b/alib2/src/regexp/RegExpSymbol.cpp
@@ -54,6 +54,10 @@ bool RegExpSymbol::operator==(const RegExpSymbol& other) const {
 	return this->symbol == other.symbol;
 }
 
+void RegExpSymbol::operator>>(ostream& out) const {
+	out << "(RegExpSymbol " << symbol << ")";
+}
+
 bool RegExpSymbol::containsEmptyString() const {
 	return false;
 }
diff --git a/alib2/src/regexp/RegExpSymbol.h b/alib2/src/regexp/RegExpSymbol.h
index 83d79f6bae..5325b49a13 100644
--- a/alib2/src/regexp/RegExpSymbol.h
+++ b/alib2/src/regexp/RegExpSymbol.h
@@ -40,6 +40,11 @@ public:
 	virtual bool operator<(const Iteration&) const;
 	virtual bool operator<(const RegExpSymbol&) const;
 	virtual bool operator==(const RegExpSymbol&) const;
+	
+	/**
+	 * @copydoc RegExpElement::operator>>() const
+	 */
+	virtual void operator>>(ostream& out) const;
 
 	/**
 	 * @copydoc RegExpElement::getAlphabet() const
-- 
GitLab