| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  |  * @author Richard Roberts | 
					
						
							| 
									
										
										
										
											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-10-02 22:40:07 +08:00
										 |  |  |         maybe_shared_ptr(add_ptr || isPtr1, qualifiedType1("::"), type1) + ", " + | 
					
						
							| 
									
										
										
										
											2012-07-26 22:06:37 +08:00
										 |  |  |       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 { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return isPair? "[first,second]" : "result"; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ReturnValue::qualifiedType1(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string result; | 
					
						
							|  |  |  |   BOOST_FOREACH(const string& ns, namespaces1) result += ns + delim; | 
					
						
							|  |  |  |   return result + type1; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ReturnValue::qualifiedType2(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +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 { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string cppType1 = qualifiedType1("::"), matlabType1 = qualifiedType1("."); | 
					
						
							|  |  |  |   string cppType2 = qualifiedType2("::"), matlabType2 = qualifiedType2("."); | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:37 +08:00
										 |  |  |   if (isPair) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     // For a pair, store the returned pair so we do not evaluate the function twice
 | 
					
						
							|  |  |  |     file.oss << "  " << return_type(false, pair) << " pairResult = " << result << ";\n"; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     // first return value in pair
 | 
					
						
							| 
									
										
										
										
											2012-07-11 23:44:04 +08:00
										 |  |  |     if (category1 == ReturnValue::CLASS) { // if we are going to make one
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       string objCopy, ptrType; | 
					
						
							|  |  |  |       ptrType = "Shared" + type1; | 
					
						
							|  |  |  |       const bool isVirtual = typeAttributes.at(cppType1).isVirtual; | 
					
						
							|  |  |  |       if(isVirtual) { | 
					
						
							|  |  |  |         if(isPtr1) | 
					
						
							|  |  |  |           objCopy = "pairResult.first"; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = "pairResult.first.clone()"; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         if(isPtr1) | 
					
						
							|  |  |  |           objCopy = "pairResult.first"; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = ptrType + "(new " + cppType1 + "(pairResult.first))"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       file.oss << "  out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-07-11 23:44:04 +08:00
										 |  |  |     } else if(isPtr1) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       file.oss << "  Shared" << type1 <<"* ret = new Shared" << type1 << "(pairResult.first);" << endl; | 
					
						
							|  |  |  |       file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\", false);\n"; | 
					
						
							|  |  |  |     } else // if basis type
 | 
					
						
							| 
									
										
										
										
											2012-07-26 22:06:37 +08:00
										 |  |  |       file.oss << "  out[0] = wrap< " << return_type(true,arg1) << " >(pairResult.first);\n"; | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // second return value in pair
 | 
					
						
							| 
									
										
										
										
											2012-07-11 23:44:04 +08:00
										 |  |  |     if (category2 == ReturnValue::CLASS) { // if we are going to make one
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       string objCopy, ptrType; | 
					
						
							|  |  |  |       ptrType = "Shared" + type2; | 
					
						
							|  |  |  |       const bool isVirtual = typeAttributes.at(cppType2).isVirtual; | 
					
						
							|  |  |  |       if(isVirtual) { | 
					
						
							|  |  |  |         if(isPtr2) | 
					
						
							|  |  |  |           objCopy = "pairResult.second"; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = "pairResult.second.clone()"; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         if(isPtr2) | 
					
						
							|  |  |  |           objCopy = "pairResult.second"; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = ptrType + "(new " + cppType2 + "(pairResult.second))"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       file.oss << "  out[1] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType2 << "\", " << (isVirtual ? "true" : "false") << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-07-11 23:44:04 +08:00
										 |  |  |     } else if(isPtr2) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       file.oss << "  Shared" << type2 <<"* ret = new Shared" << type2 << "(pairResult.second);" << endl; | 
					
						
							|  |  |  |       file.oss << "  out[1] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n"; | 
					
						
							|  |  |  |     } else | 
					
						
							| 
									
										
										
										
											2012-07-26 22:06:37 +08:00
										 |  |  |       file.oss << "  out[1] = wrap< " << return_type(true,arg2) << " >(pairResult.second);\n"; | 
					
						
							| 
									
										
										
										
											2012-11-28 03:03:16 +08:00
										 |  |  |   } else { // Not a pair
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (category1 == ReturnValue::CLASS) { | 
					
						
							|  |  |  |       string objCopy, ptrType; | 
					
						
							|  |  |  |       ptrType = "Shared" + type1; | 
					
						
							|  |  |  |       const bool isVirtual = typeAttributes.at(cppType1).isVirtual; | 
					
						
							|  |  |  |       if(isVirtual) { | 
					
						
							|  |  |  |         if(isPtr1) | 
					
						
							|  |  |  |           objCopy = result; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = result + ".clone()"; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         if(isPtr1) | 
					
						
							|  |  |  |           objCopy = result; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           objCopy = ptrType + "(new " + cppType1 + "(" + result + "))"; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       file.oss << "  out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; | 
					
						
							|  |  |  |     } else if(isPtr1) { | 
					
						
							|  |  |  |       file.oss << "  Shared" << type1 <<"* ret = new Shared" << type1 << "(" << result << ");" << endl; | 
					
						
							|  |  |  |       file.oss << "  out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; | 
					
						
							|  |  |  |     } else if (matlabType1!="void") | 
					
						
							|  |  |  |       file.oss << "  out[0] = wrap< " << return_type(true,arg1) << " >(" << result << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:39 +08:00
										 |  |  | void ReturnValue::wrapTypeUnwrap(FileWriter& wrapperFile) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   if(isPair) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if(category1 == ReturnValue::CLASS) | 
					
						
							|  |  |  |       wrapperFile.oss << "  typedef boost::shared_ptr<"  << qualifiedType1("::")  << "> Shared" <<  type1 << ";"<< endl; | 
					
						
							|  |  |  |     if(category2 == ReturnValue::CLASS) | 
					
						
							|  |  |  |       wrapperFile.oss << "  typedef boost::shared_ptr<"  << qualifiedType2("::")  << "> Shared" <<  type2 << ";"<< endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     if (category1 == ReturnValue::CLASS) | 
					
						
							|  |  |  |       wrapperFile.oss << "  typedef boost::shared_ptr<"  << qualifiedType1("::")  << "> Shared" <<  type1 << ";"<< endl; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |