From: rtrimana <rtrimana@uci.edu>
Date: Mon, 13 Feb 2017 16:36:32 +0000 (-0800)
Subject: Making sure that C++ Set and Relation methods return pointers from a new set/map... 
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7490661d4de271a8aba6a68847066691ab30abe3;p=iot2.git

Making sure that C++ Set and Relation methods return pointers from a new set/map every time the method is called; this keeps the original set/map intact/immutable.
---

diff --git a/common.mk b/common.mk
index fdf6b88..e5fcbb8 100644
--- a/common.mk
+++ b/common.mk
@@ -1,5 +1,6 @@
 G++ := g++
 ARM_G++ := arm-linux-gnueabihf-g++
+#ARM_G++ := arm-linux-gnueabi-g++
 JAVA := java
 JAR := jar
 JAVADOC := javadoc
diff --git a/iotjava/iotruntime/cpp/iotslave/Makefile b/iotjava/iotruntime/cpp/iotslave/Makefile
index 4157921..1d73cca 100755
--- a/iotjava/iotruntime/cpp/iotslave/Makefile
+++ b/iotjava/iotruntime/cpp/iotslave/Makefile
@@ -22,6 +22,10 @@ cpp-arm:
 	$(ARM_G++) $(GCCFLAGS) -o IoTSlave.o IoTSlave.cpp $(INCLUDE) $(CCCLINKERFLAGS)
 	cp IoTSlave.o $(BASE)/bin/iotruntime/slave/
 
+PHONY += cpp-test
+cpp-test:
+	$(G++) $(GCCFLAGS) -o SetRelationTest.o SetRelationTest.cpp $(INCLUDE) $(CCCLINKERFLAGS)
+
 PHONY += run
 run:
 	java IoTSlave
diff --git a/iotjava/iotruntime/cpp/iotslave/SetRelationTest.cpp b/iotjava/iotruntime/cpp/iotslave/SetRelationTest.cpp
new file mode 100644
index 0000000..36769a5
--- /dev/null
+++ b/iotjava/iotruntime/cpp/iotslave/SetRelationTest.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include "IoTSet.hpp"
+#include "IoTRelation.hpp"
+
+using namespace std;
+
+int main ()
+{
+	unordered_set<string> myset = { "red","green","blue" };
+
+	IoTSet<string> iotset(&myset);
+
+	unordered_set<string>::const_iterator got = iotset.find ("red");
+
+	if ( got == iotset.end() )
+		cout << "not found in myset" << endl;
+	else
+		cout << *got << " is in myset" << endl;
+
+	cout << "size: " << iotset.size() << endl;
+
+	unordered_multimap<string,string> mymap = {
+		{"mom","church"},
+		{"mom","college"},
+		{"dad","office"},
+		{"bro","school"} };
+
+	unordered_set<string>* retset = iotset.values();
+	cout << "Returned set: " << retset->size() << endl;
+	retset->erase("red");
+	cout << "Returned set: " << retset->size() << endl;
+	cout << "Original set: " << myset.size() << endl;
+
+	//cout << "one of the values for 'mom' is: ";
+	//cout << mymap.find("mom")->second;
+	//cout << endl;
+	IoTRelation<string,string> iotrel(&mymap);
+
+	std::pair<unordered_multimap<string,string>::const_iterator, 
+		unordered_multimap<string,string>::const_iterator> ret;
+	ret = iotrel.equal_range("mom");
+	for (std::unordered_multimap<string,string>::const_iterator it=ret.first; it!=ret.second; ++it)
+		cout << ' ' << it->second << endl;
+
+	cout << "size: " << iotrel.size() << endl;
+
+	return 0;
+}
diff --git a/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp
index 372f695..ad1f8bf 100644
--- a/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp
+++ b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp
@@ -69,7 +69,7 @@ IoTRelation<K,V>::~IoTRelation() {
 template <class K,class V>
 typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::find(const K& k) {
 
-	return rel->find(k);
+	return (new unordered_multimap<K,V>(*rel))->find(k);
 }
 
 
@@ -79,7 +79,7 @@ typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::find(const K&
 template <class K,class V>
 typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::begin() {
 
-	return rel->begin();
+	return (new unordered_multimap<K,V>(*rel))->begin();
 }
 
 
@@ -89,7 +89,7 @@ typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::begin() {
 template <class K,class V>
 typename unordered_multimap<K,V>::const_iterator IoTRelation<K,V>::end() {
 
-	return rel->end();
+	return (new unordered_multimap<K,V>(*rel))->end();
 }
 
 
@@ -101,7 +101,7 @@ std::pair<typename unordered_multimap<K,V>::const_iterator,
 	typename unordered_multimap<K,V>::const_iterator> 
 	IoTRelation<K,V>::equal_range(const K& k) {
 
-	return rel->equal_range(k);
+	return (new unordered_multimap<K,V>(*rel))->equal_range(k);
 }
 
 
diff --git a/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp
index 60f34f7..0c45ca8 100644
--- a/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp
+++ b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp
@@ -64,7 +64,7 @@ IoTSet<T>::~IoTSet() {
 template <class T>
 typename unordered_set<T>::const_iterator IoTSet<T>::find(const T& k) {
 
-	return set->find(k);
+	return (new unordered_set<T>(*set))->find(k);
 }
 
 
@@ -74,7 +74,7 @@ typename unordered_set<T>::const_iterator IoTSet<T>::find(const T& k) {
 template <class T>
 typename unordered_set<T>::const_iterator IoTSet<T>::begin() {
 
-	return set->begin();
+	return (new unordered_set<T>(*set))->begin();
 }
 
 
@@ -84,7 +84,7 @@ typename unordered_set<T>::const_iterator IoTSet<T>::begin() {
 template <class T>
 typename unordered_set<T>::const_iterator IoTSet<T>::end() {
 
-	return set->end();
+	return (new unordered_set<T>(*set))->end();
 }