From d7fd1d9a5962cbceca7b1090258abe9ae4f9bca9 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 20 May 2016 13:03:46 +0200
Subject: [PATCH] some fixes

---
 alib2common/src/base/CommonBase.hpp        |  2 +-
 alib2data/src/tree/ranked/RankedNode.h     | 21 +++++---
 alib2data/src/tree/unranked/UnrankedNode.h | 21 +++++---
 alib2data/test-src/tree/TreeTest.cpp       | 56 +++++++++++-----------
 alib2std/src/extensions/memory.hpp         | 18 ++++---
 5 files changed, 69 insertions(+), 49 deletions(-)

diff --git a/alib2common/src/base/CommonBase.hpp b/alib2common/src/base/CommonBase.hpp
index 00c0ac416f..7b7abdc714 100644
--- a/alib2common/src/base/CommonBase.hpp
+++ b/alib2common/src/base/CommonBase.hpp
@@ -68,7 +68,7 @@ public:
 namespace std {
 
 template < class T >
-struct compare < T, typename std::enable_if < std::is_base_of < alib::CommonBaseBase, T >::value >::type > {
+struct compare < T, typename std::enable_if < std::is_base_of < alib::CommonBaseBase, typename std::decay < T >::type >::value >::type > {
 	int operator()(const T& first, const T& second) const {
 		return first.compare(second);
 	}
diff --git a/alib2data/src/tree/ranked/RankedNode.h b/alib2data/src/tree/ranked/RankedNode.h
index 1015db1e0d..93482c6655 100644
--- a/alib2data/src/tree/ranked/RankedNode.h
+++ b/alib2data/src/tree/ranked/RankedNode.h
@@ -16,6 +16,9 @@
 namespace tree {
 
 class UnrankedNode;
+class RankedTree;
+class RankedPattern;
+class RankedNonlinearPattern;
 
 /**
  * Represents a node in the ranked tree. Contains name of the symbol.
@@ -33,6 +36,11 @@ protected:
 	 */
 	const std::set < alphabet::RankedSymbol > * alphabet;
 
+	/**
+	 * @copydoc RankedNode::attachTree()
+	 */
+	bool attachAlphabet ( const std::set < alphabet::RankedSymbol > * tree );
+
 public:
 	explicit RankedNode ( alphabet::RankedSymbol symbol, std::vector < std::smart_ptr < RankedNode > > children );
 
@@ -48,9 +56,9 @@ public:
 	UnrankedNode asUnranked ( ) const;
 
 	/**
-	 * @copydoc RankedNode::attachTree()
+	 * @copydoc RankedNode::testSymbol() const
 	 */
-	bool attachAlphabet ( const std::set < alphabet::RankedSymbol > * tree );
+	bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
 
 	/**
 	 * @copydoc RankedNode::computeMinimalAlphabet()
@@ -62,11 +70,6 @@ public:
 	 */
 	std::set < alphabet::RankedSymbol > computeMinimalAlphabet ( ) const;
 
-	/**
-	 * @copydoc RankedNode::testSymbol() const
-	 */
-	bool testSymbol ( const alphabet::RankedSymbol & symbol ) const;
-
 	/**
 	 * @return children
 	 */
@@ -102,6 +105,10 @@ public:
 	explicit operator std::string ( ) const;
 
 	void nicePrint ( std::ostream &, const std::string & = "", const bool = true ) const;
+
+	friend class RankedTree;
+	friend class RankedPattern;
+	friend class RankedNonlinearPattern;
 };
 
 } /* namespace tree */
diff --git a/alib2data/src/tree/unranked/UnrankedNode.h b/alib2data/src/tree/unranked/UnrankedNode.h
index efc1bca0d5..e514240bed 100644
--- a/alib2data/src/tree/unranked/UnrankedNode.h
+++ b/alib2data/src/tree/unranked/UnrankedNode.h
@@ -16,6 +16,9 @@
 namespace tree {
 
 class RankedNode;
+class UnrankedTree;
+class UnrankedPattern;
+class NonlinearUnrankedPattern;
 
 /**
  * Represents a node in the ranked tree. Contains name of the symbol.
@@ -33,6 +36,11 @@ protected:
 	 */
 	const std::set < alphabet::Symbol > * alphabet;
 
+	/**
+	 * @copydoc UnrankedNode::attachTree()
+	 */
+	bool attachAlphabet ( const std::set < alphabet::Symbol > * tree );
+
 public:
 	explicit UnrankedNode ( alphabet::Symbol symbol, std::vector < std::smart_ptr < UnrankedNode > > children );
 
@@ -48,9 +56,9 @@ public:
 	RankedNode asRanked ( ) const;
 
 	/**
-	 * @copydoc UnrankedNode::attachTree()
+	 * @copydoc UnrankedNode::testSymbol() const
 	 */
-	bool attachAlphabet ( const std::set < alphabet::Symbol > * tree );
+	bool testSymbol ( const alphabet::Symbol & symbol ) const;
 
 	/**
 	 * @copydoc UnrankedNode::computeMinimalAlphabet()
@@ -62,11 +70,6 @@ public:
 	 */
 	std::set < alphabet::Symbol > computeMinimalAlphabet ( ) const;
 
-	/**
-	 * @copydoc UnrankedNode::testSymbol() const
-	 */
-	bool testSymbol ( const alphabet::Symbol & symbol ) const;
-
 	/**
 	 * @return children
 	 */
@@ -104,6 +107,10 @@ public:
 	explicit operator std::string ( ) const;
 
 	void nicePrint ( std::ostream &, const std::string & = "", const bool = true ) const;
+
+	friend class UnrankedTree;
+	friend class UnrankedPattern;
+	friend class UnrankedNonlinearPattern;
 };
 
 } /* namespace tree */
diff --git a/alib2data/test-src/tree/TreeTest.cpp b/alib2data/test-src/tree/TreeTest.cpp
index c50c549532..a405e75aec 100644
--- a/alib2data/test-src/tree/TreeTest.cpp
+++ b/alib2data/test-src/tree/TreeTest.cpp
@@ -233,10 +233,10 @@ void TreeTest::testUnrankedTreeParser() {
 
 	const std::set<alphabet::Symbol> alphabet {a, b, c};
 
-	tree::UnrankedNode * node3 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node4 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node2 = new tree::UnrankedNode(b, {node3});
-	tree::UnrankedNode node1(a, {node2, node4});
+	tree::UnrankedNode node3(c, {});
+	tree::UnrankedNode node4(c, {});
+	tree::UnrankedNode node2(b, {std::make_smart<tree::UnrankedNode>(std::move(node3))});
+	tree::UnrankedNode node1(a, {std::make_smart<tree::UnrankedNode>(std::move(node2)), std::make_smart<tree::UnrankedNode>(std::move(node4))});
 
 	tree::UnrankedTree tree(alphabet, std::move(node1));
 
@@ -284,15 +284,15 @@ void TreeTest::testUnrankedTreeCompare() {
 
 	const std::set<alphabet::Symbol> alphabet {a, b, c};
 
-	tree::UnrankedNode * node3 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node4 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node2 = new tree::UnrankedNode(b, {node3});
-	tree::UnrankedNode node1(a, {node2, node4});
+	tree::UnrankedNode node3(c, {});
+	tree::UnrankedNode node4(c, {});
+	tree::UnrankedNode node2(b, {std::make_smart<tree::UnrankedNode>(std::move(node3))});
+	tree::UnrankedNode node1(a, {std::make_smart<tree::UnrankedNode>(std::move(node2)), std::make_smart<tree::UnrankedNode>(std::move(node4))});
 
-	tree::UnrankedNode * node6 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node8 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node7 = new tree::UnrankedNode(b, {node8});
-	tree::UnrankedNode node5(a, {node6, node7});
+	tree::UnrankedNode node6(c, {});
+	tree::UnrankedNode node8(c, {});
+	tree::UnrankedNode node7(b, {std::make_smart<tree::UnrankedNode>(std::move(node8))});
+	tree::UnrankedNode node5(a, {std::make_smart<tree::UnrankedNode>(std::move(node6)), std::make_smart<tree::UnrankedNode>(std::move(node7))});
 
 	tree::UnrankedTree tree1(alphabet, std::move(node1));
 	tree::UnrankedTree tree2(alphabet, std::move(node5));
@@ -312,17 +312,17 @@ void TreeTest::testUnrankedTreeSymbolValidityCheck() {
 
 	const std::set<alphabet::Symbol> alphabet {a, b, c, e};
 
-	tree::UnrankedNode * node3 = new tree::UnrankedNode(d, {});
-	tree::UnrankedNode * node4 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node2 = new tree::UnrankedNode(b, {node3});
-	tree::UnrankedNode node1(a, {node2, node4});
+	tree::UnrankedNode node3(d, {});
+	tree::UnrankedNode node4(c, {});
+	tree::UnrankedNode node2(b, {std::make_smart<tree::UnrankedNode>(std::move(node3))});
+	tree::UnrankedNode node1(a, {std::make_smart<tree::UnrankedNode>(std::move(node2)), std::make_smart<tree::UnrankedNode>(std::move(node4))});
 
 	CPPUNIT_ASSERT_THROW(tree::UnrankedTree(alphabet, std::move(node1)), tree::TreeException);
 
-	tree::UnrankedNode * node6 = new tree::UnrankedNode(e, {});
-	tree::UnrankedNode * node8 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node7 = new tree::UnrankedNode(b, {node8});
-	tree::UnrankedNode node5(a, {node6, node7});
+	tree::UnrankedNode node6(e, {});
+	tree::UnrankedNode node8(c, {});
+	tree::UnrankedNode node7(b, {std::make_smart<tree::UnrankedNode>(std::move(node8))});
+	tree::UnrankedNode node5(a, {std::make_smart<tree::UnrankedNode>(std::move(node6)), std::make_smart<tree::UnrankedNode>(std::move(node7))});
 
 	tree::UnrankedTree * tree = NULL;
 	CPPUNIT_ASSERT_NO_THROW(tree = new tree::UnrankedTree(alphabet, std::move(node5)));
@@ -342,15 +342,15 @@ void TreeTest::testUnrankedTreeSubtreeSwitch() {
 	const std::set<alphabet::Symbol> alphabet1 {a, b, c, d};
 	const std::set<alphabet::Symbol> alphabet2 {a, b, c, d};
 
-	tree::UnrankedNode * node3 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node4 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node2 = new tree::UnrankedNode(b, {node3});
-	tree::UnrankedNode node1(a, {node2, node4});
+	tree::UnrankedNode node3(c, {});
+	tree::UnrankedNode node4(c, {});
+	tree::UnrankedNode node2(b, {std::make_smart<tree::UnrankedNode>(std::move(node3))});
+	tree::UnrankedNode node1(a, {std::make_smart<tree::UnrankedNode>(std::move(node2)), std::make_smart<tree::UnrankedNode>(std::move(node4))});
 
-	tree::UnrankedNode * node6 = new tree::UnrankedNode(d, {});
-	tree::UnrankedNode * node8 = new tree::UnrankedNode(c, {});
-	tree::UnrankedNode * node7 = new tree::UnrankedNode(b, {node8});
-	tree::UnrankedNode node5(a, {node6, node7});
+	tree::UnrankedNode node6(d, {});
+	tree::UnrankedNode node8(c, {});
+	tree::UnrankedNode node7(b, {std::make_smart<tree::UnrankedNode>(std::move(node8))});
+	tree::UnrankedNode node5(a, {std::make_smart<tree::UnrankedNode>(std::move(node6)), std::make_smart<tree::UnrankedNode>(std::move(node7))});
 
 	tree::UnrankedTree tree1(alphabet1, std::move(node1));
 	tree::UnrankedTree tree1Copy (tree1);
diff --git a/alib2std/src/extensions/memory.hpp b/alib2std/src/extensions/memory.hpp
index cd49a08947..f936105879 100644
--- a/alib2std/src/extensions/memory.hpp
+++ b/alib2std/src/extensions/memory.hpp
@@ -20,11 +20,11 @@ private:
 template < class T >
 class cow_shared_ptr < T, typename std::enable_if < std::is_base_of < cow_shared_ptr_base, T >::value >::type > {
 public:
-	cow_shared_ptr ( ) {
+	explicit cow_shared_ptr ( ) {
 		attach ( NULL );
 	}
 
-	cow_shared_ptr ( T * data ) {
+	explicit cow_shared_ptr ( T * data ) {
 		if ( data ) data->m_UseCount = 0;
 
 		attach ( data );
@@ -145,11 +145,11 @@ class cow_shared_ptr < T, typename std::enable_if < !std::is_base_of < cow_share
 	};
 
 public:
-	cow_shared_ptr ( ) {
+	explicit cow_shared_ptr ( ) {
 		attach ( NULL );
 	}
 
-	cow_shared_ptr ( T * data ) {
+	explicit cow_shared_ptr ( T * data ) {
 		attach ( data ? new cow_shared_ptr_data ( data ) : NULL );
 	}
 
@@ -257,10 +257,10 @@ class smart_ptr {
 	T * m_Data;
 
 public:
-	smart_ptr ( ) : m_Data ( NULL ) {
+	explicit smart_ptr ( ) : m_Data ( NULL ) {
 	}
 
-	smart_ptr ( T * data ) : m_Data ( data ) {
+	explicit smart_ptr ( T * data ) : m_Data ( data ) {
 	}
 
 	smart_ptr ( const smart_ptr < T > & other ) : m_Data ( std::clone ( other.m_Data ) ) {
@@ -313,6 +313,12 @@ public:
 		return m_Data;
 	}
 
+	T * release ( ) {
+		T * res = m_Data;
+		m_Data = nullptr;
+		return res;
+	}
+
 	explicit operator bool( ) const {
 		return m_Data;
 	}
-- 
GitLab