| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |  * @file StaticMethod.ccp | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |  * @author Andrew Melim | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | #include <boost/lexical_cast.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "StaticMethod.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | void StaticMethod::addOverload(bool verbose, const std::string& name, | 
					
						
							|  |  |  | 		const ArgumentList& args, const ReturnValue& retVal) { | 
					
						
							|  |  |  | 	this->verbose = verbose; | 
					
						
							|  |  |  | 	this->name = name; | 
					
						
							|  |  |  | 	this->argLists.push_back(args); | 
					
						
							|  |  |  | 	this->returnVals.push_back(retVal); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | void StaticMethod::proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, | 
					
						
							|  |  |  | 																		 const string& cppClassName, | 
					
						
							|  |  |  | 																		 const string& matlabClassName, | 
					
						
							|  |  |  | 																		 const string& wrapperName, | 
					
						
							|  |  |  | 																		 const vector<string>& using_namespaces, | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 																		 const ReturnValue::TypeAttributesTable& typeAttributes, | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 																		 vector<string>& functionNames) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   string upperName = name;  upperName[0] = std::toupper(upperName[0], std::locale()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	proxyFile.oss << "    function varargout = " << upperName << "(varargin)\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(size_t overload = 0; overload < argLists.size(); ++overload) { | 
					
						
							|  |  |  | 		const ArgumentList& args = argLists[overload]; | 
					
						
							|  |  |  | 		const ReturnValue& returnVal = returnVals[overload]; | 
					
						
							|  |  |  | 		size_t nrArgs = args.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const int id = functionNames.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Output proxy matlab code
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// check for number of arguments...
 | 
					
						
							|  |  |  | 		proxyFile.oss << "      " << (overload==0?"":"else") << "if length(varargin) == " << nrArgs; | 
					
						
							|  |  |  | 		if (nrArgs>0) proxyFile.oss << " && "; | 
					
						
							|  |  |  | 		// ...and their types
 | 
					
						
							|  |  |  | 		bool first = true; | 
					
						
							|  |  |  | 		for(size_t i=0;i<nrArgs;i++) { | 
					
						
							|  |  |  | 			if (!first) proxyFile.oss << " && "; | 
					
						
							|  |  |  | 			proxyFile.oss << "isa(varargin{" << i+1 << "},'" << args[i].matlabClass() << "')"; | 
					
						
							|  |  |  | 			first=false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		proxyFile.oss << "\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// output call to C++ wrapper
 | 
					
						
							|  |  |  | 		string output; | 
					
						
							|  |  |  | 		if(returnVal.isPair) | 
					
						
							|  |  |  | 			output = "[ varargout{1} varargout{2} ] = "; | 
					
						
							|  |  |  | 		else if(returnVal.category1 == ReturnValue::VOID) | 
					
						
							|  |  |  | 			output = ""; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			output = "varargout{1} = "; | 
					
						
							|  |  |  | 		proxyFile.oss << "        " << output << wrapperName << "(" << id << ", varargin{:});\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Output C++ wrapper code
 | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		const string wrapFunctionName = wrapper_fragment( | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 			wrapperFile, cppClassName, matlabClassName, overload, id, using_namespaces, typeAttributes); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Add to function list
 | 
					
						
							|  |  |  | 		functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  |   proxyFile.oss << "      else\n"; | 
					
						
							|  |  |  | 	proxyFile.oss << "        error('Arguments do not match any overload of function " << | 
					
						
							|  |  |  | 		matlabClassName << "." << upperName << "');" << endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	proxyFile.oss << "      end\n"; | 
					
						
							|  |  |  | 	proxyFile.oss << "    end\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | string StaticMethod::wrapper_fragment(FileWriter& file,  | 
					
						
							|  |  |  | 			    const string& cppClassName, | 
					
						
							|  |  |  | 			    const string& matlabClassName, | 
					
						
							|  |  |  | 					int overload, | 
					
						
							|  |  |  | 					int id, | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 			    const vector<string>& using_namespaces, | 
					
						
							|  |  |  | 					const ReturnValue::TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | 	const string wrapFunctionName = matlabClassName + "_" + name + "_" + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 	const ArgumentList& args = argLists[overload]; | 
					
						
							|  |  |  | 	const ReturnValue& returnVal = returnVals[overload]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | 	// call
 | 
					
						
							|  |  |  | 	file.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							|  |  |  | 	// start
 | 
					
						
							|  |  |  | 	file.oss << "{\n"; | 
					
						
							|  |  |  | 	generateUsingNamespace(file, using_namespaces); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   if(returnVal.isPair) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |       if(returnVal.category1 == ReturnValue::CLASS) | 
					
						
							|  |  |  |         file.oss << "  typedef boost::shared_ptr<"  << returnVal.qualifiedType1("::")  << "> Shared" <<  returnVal.type1 << ";"<< endl; | 
					
						
							|  |  |  |       if(returnVal.category2 == ReturnValue::CLASS) | 
					
						
							|  |  |  |         file.oss << "  typedef boost::shared_ptr<"  << returnVal.qualifiedType2("::")  << "> Shared" <<  returnVal.type2 << ";"<< endl; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |       if(returnVal.category1 == ReturnValue::CLASS) | 
					
						
							|  |  |  |         file.oss << "  typedef boost::shared_ptr<"  << returnVal.qualifiedType1("::")  << "> Shared" <<  returnVal.type1 << ";"<< endl; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |   file.oss << "  typedef boost::shared_ptr<"  << cppClassName  << "> Shared;" << endl; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check arguments
 | 
					
						
							| 
									
										
										
										
											2011-12-06 00:36:48 +08:00
										 |  |  |   // NOTE: for static functions, there is no object passed
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  |   file.oss << "  checkArguments(\"" << matlabClassName << "." << name << "\",nargout,nargin," << args.size() << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // unwrap arguments, see Argument.cpp
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   args.matlab_unwrap(file,0); // We start at 0 because there is no self object
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "  "; | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |   // call method with default type and wrap result
 | 
					
						
							| 
									
										
										
										
											2011-12-07 11:05:37 +08:00
										 |  |  |   if (returnVal.type1!="void") | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  | 		returnVal.wrap_result(cppClassName+"::"+name+"("+args.names()+")", file, typeAttributes); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		file.oss << cppClassName+"::"+name+"("+args.names()+");\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // finish
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "}\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | 	return wrapFunctionName; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |