| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  |  * @author Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:22:10 +08:00
										 |  |  | #include "StaticMethod.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | #include <boost/lexical_cast.hpp>
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:22:10 +08:00
										 |  |  | #include <boost/algorithm/string.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:22:10 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | void StaticMethod::addOverload(bool verbose, const std::string& name, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     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-24 02:24:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  | void StaticMethod::proxy_wrapper_fragments(FileWriter& file, | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     FileWriter& wrapperFile, const string& cppClassName, | 
					
						
							|  |  |  |     const std::string& matlabQualName, const std::string& matlabUniqueName, | 
					
						
							|  |  |  |     const string& wrapperName, const TypeAttributesTable& typeAttributes, | 
					
						
							|  |  |  |     vector<string>& functionNames) const { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   string upperName = name; | 
					
						
							|  |  |  |   upperName[0] = std::toupper(upperName[0], std::locale()); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "    function varargout = " << upperName << "(varargin)\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   //Comments for documentation
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   string up_name = boost::to_upper_copy(name); | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "      % " << up_name << " usage: "; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   unsigned int argLCount = 0; | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   BOOST_FOREACH(ArgumentList argList, argLists) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     argList.emit_prototype(file, name); | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     if (argLCount != argLists.size() - 1) | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |       file.oss << ", "; | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |       file.oss << " : returns " | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |           << returnVals[0].return_type(false, returnVals[0].pair) << endl; | 
					
						
							|  |  |  |     argLCount++; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "      % " | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |       << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" | 
					
						
							|  |  |  |       << endl; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "      % " << "" << endl; | 
					
						
							|  |  |  |   file.oss << "      % " << "Usage" << endl; | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   BOOST_FOREACH(ArgumentList argList, argLists) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     file.oss << "      % "; | 
					
						
							|  |  |  |     argList.emit_prototype(file, up_name); | 
					
						
							|  |  |  |     file.oss << endl; | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-08-27 23:19:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   // Check arguments for all overloads
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   for (size_t overload = 0; overload < argLists.size(); ++overload) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Output proxy matlab code
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     file.oss << "      " << (overload == 0 ? "" : "else"); | 
					
						
							|  |  |  |     const int id = (int) functionNames.size(); | 
					
						
							|  |  |  |     argLists[overload].emit_conditional_call(file, returnVals[overload], | 
					
						
							|  |  |  |         wrapperName, id, true); // last bool is to indicate static !
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Output C++ wrapper code
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, | 
					
						
							|  |  |  |         matlabUniqueName, (int) overload, id, typeAttributes); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Add to function list
 | 
					
						
							|  |  |  |     functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "      else\n"; | 
					
						
							|  |  |  |   file.oss << "        error('Arguments do not match any overload of function " | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |       << matlabQualName << "." << upperName << "');" << endl; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "      end\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   file.oss << "    end\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  | string StaticMethod::wrapper_fragment(FileWriter& file, | 
					
						
							|  |  |  |     const string& cppClassName, const string& matlabUniqueName, int overload, | 
					
						
							|  |  |  |     int id, const TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   const string wrapFunctionName = matlabUniqueName + "_" + name + "_" | 
					
						
							|  |  |  |       + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   const ArgumentList& args = argLists[overload]; | 
					
						
							|  |  |  |   const ReturnValue& returnVal = returnVals[overload]; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // call
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   file.oss << "void " << wrapFunctionName | 
					
						
							|  |  |  |       << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // start
 | 
					
						
							|  |  |  |   file.oss << "{\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   returnVal.wrapTypeUnwrap(file); | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +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
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   file.oss << "  checkArguments(\"" << matlabUniqueName << "." << name | 
					
						
							|  |  |  |       << "\",nargout,nargin," << args.size() << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // unwrap arguments, see Argument.cpp
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +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-07-10 04:19:37 +08:00
										 |  |  |   // call method with default type and wrap result
 | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |   if (returnVal.type1 != "void") | 
					
						
							|  |  |  |     returnVal.wrap_result(cppClassName + "::" + name + "(" + args.names() + ")", | 
					
						
							|  |  |  |         file, typeAttributes); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     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-10-02 22:40:07 +08:00
										 |  |  |   return wrapFunctionName; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |