| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file MethodBase.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @author Andrew Melim | 
					
						
							|  |  |  |  * @author Richard Roberts | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Method.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-12 05:14:06 +08:00
										 |  |  | #include "Class.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/lexical_cast.hpp>
 | 
					
						
							|  |  |  | #include <boost/algorithm/string.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace wrap; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | void MethodBase::proxy_wrapper_fragments( | 
					
						
							|  |  |  |     FileWriter& proxyFile, FileWriter& wrapperFile, Str cppClassName, | 
					
						
							|  |  |  |     Str matlabQualName, Str matlabUniqueName, Str wrapperName, | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  |     const TypeAttributesTable& typeAttributes, | 
					
						
							|  |  |  |     vector<string>& functionNames) const { | 
					
						
							|  |  |  |   // emit header, e.g., function varargout = templatedMethod(this, varargin)
 | 
					
						
							|  |  |  |   proxy_header(proxyFile); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Emit comments for documentation
 | 
					
						
							|  |  |  |   string up_name = boost::to_upper_copy(matlabName()); | 
					
						
							|  |  |  |   proxyFile.oss << "      % " << up_name << " usage: "; | 
					
						
							|  |  |  |   usage_fragment(proxyFile, matlabName()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Emit URL to Doxygen page
 | 
					
						
							|  |  |  |   proxyFile.oss << "      % " | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                 << "Doxygen can be found at " | 
					
						
							|  |  |  |                    "http://research.cc.gatech.edu/borg/sites/edu.borg/html/" | 
					
						
							|  |  |  |                    "index.html" << endl; | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Handle special case of single overload with all numeric arguments
 | 
					
						
							|  |  |  |   if (nrOverloads() == 1 && argumentList(0).allScalar()) { | 
					
						
							|  |  |  |     // Output proxy matlab code
 | 
					
						
							|  |  |  |     // TODO: document why is it OK to not check arguments in this case
 | 
					
						
							|  |  |  |     proxyFile.oss << "      "; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     const int id = (int)functionNames.size(); | 
					
						
							| 
									
										
										
										
											2014-11-30 03:59:38 +08:00
										 |  |  |     emit_call(proxyFile, returnValue(0), wrapperName, id); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Output C++ wrapper code
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     const string wrapFunctionName = wrapper_fragment( | 
					
						
							|  |  |  |         wrapperFile, cppClassName, matlabUniqueName, 0, id, typeAttributes); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Add to function list
 | 
					
						
							|  |  |  |     functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     // Check arguments for all overloads
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < nrOverloads(); ++i) { | 
					
						
							|  |  |  |       // Output proxy matlab code
 | 
					
						
							|  |  |  |       proxyFile.oss << "      " << (i == 0 ? "" : "else"); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |       const int id = (int)functionNames.size(); | 
					
						
							| 
									
										
										
										
											2014-11-30 03:53:38 +08:00
										 |  |  |       emit_conditional_call(proxyFile, returnValue(i), argumentList(i), | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                             wrapperName, id); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Output C++ wrapper code
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |       const string wrapFunctionName = wrapper_fragment( | 
					
						
							|  |  |  |           wrapperFile, cppClassName, matlabUniqueName, i, id, typeAttributes); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Add to function list
 | 
					
						
							|  |  |  |       functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     proxyFile.oss << "      else\n"; | 
					
						
							|  |  |  |     proxyFile.oss | 
					
						
							|  |  |  |         << "        error('Arguments do not match any overload of function " | 
					
						
							|  |  |  |         << matlabQualName << "." << name_ << "');" << endl; | 
					
						
							|  |  |  |     proxyFile.oss << "      end\n"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   proxyFile.oss << "    end\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | string MethodBase::wrapper_fragment( | 
					
						
							|  |  |  |     FileWriter& wrapperFile, Str cppClassName, Str matlabUniqueName, | 
					
						
							|  |  |  |     int overload, int id, const TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   const string wrapFunctionName = | 
					
						
							|  |  |  |       matlabUniqueName + "_" + name_ + "_" + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const ArgumentList& args = argumentList(overload); | 
					
						
							|  |  |  |   const ReturnValue& returnVal = returnValue(overload); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // call
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   wrapperFile.oss | 
					
						
							|  |  |  |       << "void " << wrapFunctionName | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  |       << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							|  |  |  |   // start
 | 
					
						
							|  |  |  |   wrapperFile.oss << "{\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   returnVal.wrapTypeUnwrap(wrapperFile); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   wrapperFile.oss << "  typedef boost::shared_ptr<" << cppClassName | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                   << "> Shared;" << endl; | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // get call
 | 
					
						
							|  |  |  |   // for static methods: cppClassName::staticMethod<TemplateVal>
 | 
					
						
							|  |  |  |   // for instance methods: obj->instanceMethod<TemplateVal>
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   string expanded = | 
					
						
							|  |  |  |       wrapper_call(wrapperFile, cppClassName, matlabUniqueName, args); | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   expanded += ("(" + args.names() + ")"); | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (returnVal.type1.name() != "void") | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  |     returnVal.wrap_result(expanded, wrapperFile, typeAttributes); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     wrapperFile.oss << "  " + expanded + ";\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // finish
 | 
					
						
							|  |  |  |   wrapperFile.oss << "}\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return wrapFunctionName; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-30 02:34:46 +08:00
										 |  |  | void MethodBase::python_wrapper(FileWriter& wrapperFile, Str className) const { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   wrapperFile.oss << "  .def(\"" << name_ << "\", &" << className | 
					
						
							|  |  |  |                   << "::" << name_ << ");\n"; | 
					
						
							| 
									
										
										
										
											2014-11-29 22:31:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |