| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "ReturnValue.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | ReturnValue ReturnValue::expandTemplate(const TemplateSubstitution& ts) const { | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |   ReturnValue instRetVal = *this; | 
					
						
							| 
									
										
										
										
											2014-11-30 07:13:29 +08:00
										 |  |  |   instRetVal.type1 = ts.tryToSubstitite(type1); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   if (isPair) instRetVal.type2 = ts.tryToSubstitite(type2); | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |   return instRetVal; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2019-03-20 00:42:40 +08:00
										 |  |  | string ReturnValue::returnType() const { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:31:40 +08:00
										 |  |  |   if (isPair) | 
					
						
							| 
									
										
										
										
											2019-03-20 00:42:40 +08:00
										 |  |  |     return "pair< " + type1.qualifiedName("::") + ", " + | 
					
						
							|  |  |  |            type2.qualifiedName("::") + " >"; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:31:40 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-03-20 00:42:40 +08:00
										 |  |  |     return type1.qualifiedName("::"); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 20:31:46 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-12 21:37:08 +08:00
										 |  |  | string ReturnValue::matlab_returnType() const { | 
					
						
							|  |  |  |   return isPair ? "[first,second]" : "result"; | 
					
						
							| 
									
										
										
										
											2014-11-12 20:31:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  | void ReturnValue::wrap_result(const string& result, FileWriter& wrapperFile, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                               const TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:37 +08:00
										 |  |  |   if (isPair) { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     // For a pair, store the returned pair so we do not evaluate the function
 | 
					
						
							|  |  |  |     // twice
 | 
					
						
							| 
									
										
										
										
											2019-03-20 00:09:00 +08:00
										 |  |  |     wrapperFile.oss << "  auto pairResult = " << result | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                     << ";\n"; | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |     type1.wrap_result("  out[0]", "pairResult.first", wrapperFile, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                       typeAttributes); | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |     type2.wrap_result("  out[1]", "pairResult.second", wrapperFile, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                       typeAttributes); | 
					
						
							|  |  |  |   } else {  // Not a pair
 | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |     type1.wrap_result("  out[0]", result, wrapperFile, typeAttributes); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  | void ReturnValue::emit_matlab(FileWriter& proxyFile) const { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:52:49 +08:00
										 |  |  |   string output; | 
					
						
							|  |  |  |   if (isPair) | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |     proxyFile.oss << "[ varargout{1} varargout{2} ] = "; | 
					
						
							| 
									
										
										
										
											2014-11-12 21:37:08 +08:00
										 |  |  |   else if (type1.category != ReturnType::VOID) | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |     proxyFile.oss << "varargout{1} = "; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:52:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 21:37:08 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-12-16 13:23:45 +08:00
										 |  |  | void ReturnValue::emit_cython_pxd( | 
					
						
							|  |  |  |     FileWriter& file, const std::string& className, | 
					
						
							|  |  |  |     const std::vector<std::string>& templateArgs) const { | 
					
						
							| 
									
										
										
										
											2016-09-09 19:10:04 +08:00
										 |  |  |   if (isPair) { | 
					
						
							|  |  |  |     file.oss << "pair["; | 
					
						
							| 
									
										
										
										
											2016-12-16 13:23:45 +08:00
										 |  |  |     type1.emit_cython_pxd(file, className, templateArgs); | 
					
						
							| 
									
										
										
										
											2016-09-09 19:10:04 +08:00
										 |  |  |     file.oss << ","; | 
					
						
							| 
									
										
										
										
											2016-12-16 13:23:45 +08:00
										 |  |  |     type2.emit_cython_pxd(file, className, templateArgs); | 
					
						
							| 
									
										
										
										
											2016-09-09 19:16:29 +08:00
										 |  |  |     file.oss << "] "; | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2016-12-16 13:23:45 +08:00
										 |  |  |     type1.emit_cython_pxd(file, className, templateArgs); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     file.oss << " "; | 
					
						
							| 
									
										
										
										
											2016-09-09 19:10:04 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | std::string ReturnValue::pyx_returnType() const { | 
					
						
							|  |  |  |   if (isVoid()) return ""; | 
					
						
							| 
									
										
										
										
											2016-09-10 06:37:48 +08:00
										 |  |  |   if (isPair) { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     return "pair [" + type1.pyx_returnType(false) + "," + | 
					
						
							|  |  |  |            type2.pyx_returnType(false) + "]"; | 
					
						
							| 
									
										
										
										
											2016-09-12 04:40:09 +08:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     return type1.pyx_returnType(true); | 
					
						
							| 
									
										
										
										
											2016-09-12 04:40:09 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | std::string ReturnValue::pyx_casting(const std::string& var) const { | 
					
						
							|  |  |  |   if (isVoid()) return ""; | 
					
						
							| 
									
										
										
										
											2016-09-12 04:40:09 +08:00
										 |  |  |   if (isPair) { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     return "(" + type1.pyx_casting(var + ".first", false) + "," + | 
					
						
							|  |  |  |            type2.pyx_casting(var + ".second", false) + ")"; | 
					
						
							| 
									
										
										
										
											2016-09-10 06:37:48 +08:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     return type1.pyx_casting(var); | 
					
						
							| 
									
										
										
										
											2016-09-10 06:37:48 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |