| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file ReturnValue.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @date Dec 1, 2011 | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |  * @author Andrew Melim | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | #include "ReturnValue.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ReturnValue::return_type(bool add_ptr, pairing p) const { | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:37 +08:00
										 |  |  |   if (p==pair && isPair) { | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |     string str = "pair< " + | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     		maybe_shared_ptr(add_ptr && isPtr1, qualifiedType1("::"), type1) + ", " + | 
					
						
							|  |  |  |       maybe_shared_ptr(add_ptr && isPtr2, qualifiedType2("::"), type2) + " >"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |     return str; | 
					
						
							|  |  |  |   } else | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     return maybe_shared_ptr(add_ptr && isPtr1, (p==arg2)? qualifiedType2("::") : qualifiedType1("::"), (p==arg2)? type2 : type1); | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | string ReturnValue::matlab_returnType() const { | 
					
						
							|  |  |  | 	return isPair? "[first,second]" : "result"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ReturnValue::qualifiedType1(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | 	string result; | 
					
						
							|  |  |  | 	BOOST_FOREACH(const string& ns, namespaces1) result += ns + delim; | 
					
						
							|  |  |  | 	return result + type1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ReturnValue::qualifiedType2(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | 	string result; | 
					
						
							|  |  |  | 	BOOST_FOREACH(const string& ns, namespaces2) result += ns + delim; | 
					
						
							|  |  |  | 	return result + type2; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | //TODO:Fix this
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | void ReturnValue::wrap_result(const string& result, FileWriter& file, const TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | 	string cppType1 = qualifiedType1("::"), matlabType1 = qualifiedType1(); | 
					
						
							|  |  |  | 	string cppType2 = qualifiedType2("::"), matlabType2 = qualifiedType2(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:37 +08:00
										 |  |  |   if (isPair) { | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:30 +08:00
										 |  |  |   	// first return value in pair
 | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     if (isPtr1) {// if we already have a pointer
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |       file.oss << "  Shared" << type1 <<"* ret = new Shared" << type1 << "(" << result << ".first);" << endl; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |       file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (category1 == ReturnValue::CLASS) { // if we are going to make one
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 			string objCopy; | 
					
						
							|  |  |  | 			if(typeAttributes.at(cppType1).isVirtual) | 
					
						
							|  |  |  | 				objCopy = "boost::dynamic_pointer_cast<" + cppType1 + ">(" + result + ".first.clone())"; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				objCopy = "new " + cppType1 + "(" + result + ".first)"; | 
					
						
							|  |  |  |       file.oss << "  Shared" <<  type1 << "* ret = new Shared" <<  type1 << "(" << objCopy << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |     	file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:33 +08:00
										 |  |  |     else // if basis type
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |       file.oss << "  out[0] = wrap< " << return_type(true,arg1) << " >(" << result << ".first);\n"; | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // second return value in pair
 | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     if (isPtr2) {// if we already have a pointer
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |       file.oss << "  Shared" << type2 <<"* ret = new Shared" << type2 << "(" << result << ".second);" << endl; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |       file.oss << "  out[1] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (category2 == ReturnValue::CLASS) { // if we are going to make one
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 			string objCopy; | 
					
						
							|  |  |  | 			if(typeAttributes.at(cppType1).isVirtual) | 
					
						
							|  |  |  | 				objCopy = "boost::dynamic_pointer_cast<" + cppType2 + ">(" + result + ".second.clone())"; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				objCopy = "new " + cppType1 + "(" + result + ".second)"; | 
					
						
							|  |  |  |       file.oss << "  Shared" <<  type2 << "* ret = new Shared" <<  type2 << "(" << objCopy << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |     	file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |       file.oss << "  out[1] = wrap< " << return_type(true,arg2) << " >(" << result << ".second);\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   else if (isPtr1){ | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |     file.oss << "  Shared" << type1 <<"* ret = new Shared" << type1 << "(" << result << ");" << endl; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |     file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   else if (category1 == ReturnValue::CLASS){ | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 		string objCopy; | 
					
						
							|  |  |  | 		if(typeAttributes.at(cppType1).isVirtual) | 
					
						
							|  |  |  | 			objCopy = "boost::dynamic_pointer_cast<" + cppType1 + ">(" + result + ".clone())"; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			objCopy = "new " + cppType1 + "(" + result + ")"; | 
					
						
							|  |  |  |     file.oss << "  Shared" <<  type1 << "* ret = new Shared" <<  type1 << "(" << objCopy << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | 		file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-02-06 09:59:45 +08:00
										 |  |  |   else if (matlabType1!="void") | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |     file.oss << "  out[0] = wrap< " << return_type(true,arg1) << " >(" << result << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |