| 
									
										
										
										
											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-11-14 00:28:05 +08:00
										 |  |  | #include "Method.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:22:10 +08:00
										 |  |  | #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-24 02:24:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  | void StaticMethod::addOverload(bool verbose, Str name, const ArgumentList& args, | 
					
						
							|  |  |  |     const ReturnValue& retVal, const Qualified& instName) { | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Function::addOverload(verbose, name, instName); | 
					
						
							|  |  |  |   SignatureOverloads::addOverload(args, retVal); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void StaticMethod::proxy_header(FileWriter& proxyFile) const { | 
					
						
							| 
									
										
										
										
											2014-11-14 05:43:29 +08:00
										 |  |  |   string upperName = matlabName(); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  |   upperName[0] = toupper(upperName[0], locale()); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   proxyFile.oss << "    function varargout = " << upperName << "(varargin)\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void StaticMethod::proxy_wrapper_fragments(FileWriter& proxyFile, | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  |     FileWriter& wrapperFile, Str cppClassName, Str matlabQualName, | 
					
						
							|  |  |  |     Str matlabUniqueName, Str wrapperName, | 
					
						
							|  |  |  |     const TypeAttributesTable& typeAttributes, | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |     vector<string>& functionNames) const { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 05:43:29 +08:00
										 |  |  |   // emit header, e.g., function varargout = templatedMethod(this, varargin)
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   proxy_header(proxyFile); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   // Emit comments for documentation
 | 
					
						
							| 
									
										
										
										
											2014-11-14 06:21:05 +08:00
										 |  |  |   string up_name = boost::to_upper_copy(matlabName()); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   proxyFile.oss << "      % " << up_name << " usage: "; | 
					
						
							| 
									
										
										
										
											2014-11-14 06:21:05 +08:00
										 |  |  |   usage_fragment(proxyFile, matlabName()); | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   // Emit URL to Doxygen page
 | 
					
						
							|  |  |  |   proxyFile.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; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   // Handle special case of single overload with all numeric arguments
 | 
					
						
							|  |  |  |   if (nrOverloads() == 1 && argumentList(0).allScalar()) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     // Output proxy matlab code
 | 
					
						
							| 
									
										
										
										
											2014-11-14 06:21:05 +08:00
										 |  |  |     // TODO: document why is it OK to not check arguments in this case
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     proxyFile.oss << "      "; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     const int id = (int) functionNames.size(); | 
					
						
							| 
									
										
										
										
											2014-11-14 06:21:05 +08:00
										 |  |  |     argumentList(0).emit_call(proxyFile, returnValue(0), wrapperName, id, | 
					
						
							|  |  |  |         isStatic()); | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |         matlabUniqueName, 0, id, typeAttributes, templateArgValue_); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Add to function list
 | 
					
						
							|  |  |  |     functionNames.push_back(wrapFunctionName); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     // Check arguments for all overloads
 | 
					
						
							|  |  |  |     for (size_t i = 0; i < nrOverloads(); ++i) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Output proxy matlab code
 | 
					
						
							|  |  |  |       proxyFile.oss << "      " << (i == 0 ? "" : "else"); | 
					
						
							|  |  |  |       const int id = (int) functionNames.size(); | 
					
						
							| 
									
										
										
										
											2014-11-14 05:15:36 +08:00
										 |  |  |       argumentList(i).emit_conditional_call(proxyFile, returnValue(i), | 
					
						
							| 
									
										
										
										
											2014-11-14 06:21:05 +08:00
										 |  |  |           wrapperName, id, isStatic()); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Output C++ wrapper code
 | 
					
						
							|  |  |  |       const string wrapFunctionName = wrapper_fragment(wrapperFile, | 
					
						
							|  |  |  |           cppClassName, matlabUniqueName, i, id, typeAttributes, | 
					
						
							|  |  |  |           templateArgValue_); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // 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"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   proxyFile.oss << "    end\n"; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  | string StaticMethod::wrapper_fragment(FileWriter& wrapperFile, Str cppClassName, | 
					
						
							|  |  |  |     Str matlabUniqueName, int overload, int id, | 
					
						
							|  |  |  |     const TypeAttributesTable& typeAttributes, | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     const Qualified& instName) const { | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   const string wrapFunctionName = matlabUniqueName + "_" + name_ + "_" | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |       + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   const ArgumentList& args = argumentList(overload); | 
					
						
							|  |  |  |   const ReturnValue& returnVal = returnValue(overload); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // call
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   wrapperFile.oss << "void " << wrapFunctionName | 
					
						
							| 
									
										
										
										
											2014-05-26 00:43:19 +08:00
										 |  |  |       << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // start
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   wrapperFile.oss << "{\n"; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   returnVal.wrapTypeUnwrap(wrapperFile); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   wrapperFile.oss << "  typedef boost::shared_ptr<" << cppClassName | 
					
						
							|  |  |  |       << "> Shared;" << endl; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   // get call
 | 
					
						
							|  |  |  |   // for static methods: cppClassName::staticMethod<TemplateVal>
 | 
					
						
							|  |  |  |   // for instance methods: obj->instanceMethod<TemplateVal>
 | 
					
						
							|  |  |  |   string expanded = wrapper_call(wrapperFile, cppClassName, matlabUniqueName, | 
					
						
							|  |  |  |       args, returnVal, typeAttributes, instName); | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   expanded += ("(" + args.names() + ")"); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   if (returnVal.type1.name != "void") | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     returnVal.wrap_result(expanded, wrapperFile, typeAttributes); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     wrapperFile.oss << "  " + expanded + ";\n"; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // finish
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   wrapperFile.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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  | string StaticMethod::wrapper_call(FileWriter& wrapperFile, Str cppClassName, | 
					
						
							|  |  |  |     Str matlabUniqueName, const ArgumentList& args, | 
					
						
							|  |  |  |     const ReturnValue& returnVal, const TypeAttributesTable& typeAttributes, | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     const Qualified& instName) const { | 
					
						
							|  |  |  |   // check arguments
 | 
					
						
							|  |  |  |   // NOTE: for static functions, there is no object passed
 | 
					
						
							|  |  |  |   wrapperFile.oss << "  checkArguments(\"" << matlabUniqueName << "." << name_ | 
					
						
							|  |  |  |       << "\",nargout,nargin," << args.size() << ");\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // unwrap arguments, see Argument.cpp
 | 
					
						
							|  |  |  |   args.matlab_unwrap(wrapperFile, 0); // We start at 0 because there is no self object
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // call method and wrap result
 | 
					
						
							|  |  |  |   // example: out[0]=wrap<bool>(staticMethod(t));
 | 
					
						
							|  |  |  |   string expanded = cppClassName + "::" + name_; | 
					
						
							|  |  |  |   if (!instName.empty()) | 
					
						
							|  |  |  |     expanded += ("<" + instName.qualifiedName("::") + ">"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return expanded; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |