| 
									
										
										
										
											2011-10-14 02:41:56 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  |  * @file Method.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  |  * @author Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | #include <boost/lexical_cast.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Method.h"
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | void Method::addOverload(bool verbose, bool is_const, const std::string& name, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     const ArgumentList& args, const ReturnValue& retVal) { | 
					
						
							|  |  |  |   this->verbose_ = verbose; | 
					
						
							|  |  |  |   this->is_const_ = is_const; | 
					
						
							|  |  |  |   this->name = name; | 
					
						
							|  |  |  |   this->argLists.push_back(args); | 
					
						
							|  |  |  |   this->returnVals.push_back(retVal); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-28 03:03:19 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | void Method::proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |                                      const string& cppClassName, | 
					
						
							|  |  |  |                                      const std::string& matlabQualName, | 
					
						
							|  |  |  |                                      const std::string& matlabUniqueName, | 
					
						
							|  |  |  |                                      const string& wrapperName, | 
					
						
							|  |  |  |                                      const TypeAttributesTable& typeAttributes, | 
					
						
							|  |  |  |                                      vector<string>& functionNames) const { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   proxyFile.oss << "    function varargout = " << name << "(this, varargin)\n"; | 
					
						
							|  |  |  |   //Comments for documentation
 | 
					
						
							|  |  |  |   string up_name  = boost::to_upper_copy(name); | 
					
						
							|  |  |  |   proxyFile.oss << "      % " << up_name << " usage:"; | 
					
						
							|  |  |  |   unsigned int argLCount = 0; | 
					
						
							|  |  |  |   BOOST_FOREACH(ArgumentList argList, argLists)  | 
					
						
							| 
									
										
										
										
											2012-08-28 01:54:25 +08:00
										 |  |  |     {  | 
					
						
							| 
									
										
										
										
											2012-08-30 04:27:10 +08:00
										 |  |  |         proxyFile.oss << " " << name << "("; | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  |         unsigned int i = 0; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     BOOST_FOREACH(const Argument& arg, argList)  | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(i != argList.size()-1) | 
					
						
							|  |  |  |             proxyFile.oss << arg.type << " " << arg.name << ", "; | 
					
						
							| 
									
										
										
										
											2012-08-28 01:54:25 +08:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |             proxyFile.oss << arg.type << " " << arg.name; | 
					
						
							|  |  |  |         i++; | 
					
						
							| 
									
										
										
										
											2012-08-28 01:54:25 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  |         if(argLCount != argLists.size()-1) | 
					
						
							|  |  |  |             proxyFile.oss << "), "; | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2012-08-30 04:27:10 +08:00
										 |  |  |             proxyFile.oss << ") : returns " << returnVals[0].return_type(false, returnVals[0].pair) << endl;  | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  |         argLCount++; | 
					
						
							| 
									
										
										
										
											2012-08-28 01:54:25 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   proxyFile.oss << "      % " << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" << endl; | 
					
						
							|  |  |  |   proxyFile.oss << "      % " << "" << endl; | 
					
						
							|  |  |  |   proxyFile.oss << "      % " << "Method Overloads" << endl; | 
					
						
							| 
									
										
										
										
											2012-08-27 23:19:55 +08:00
										 |  |  |     BOOST_FOREACH(ArgumentList argList, argLists)  | 
					
						
							|  |  |  |     {  | 
					
						
							|  |  |  |         proxyFile.oss << "      % " << name << "("; | 
					
						
							| 
									
										
										
										
											2012-08-29 05:44:45 +08:00
										 |  |  |         unsigned int i = 0; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     BOOST_FOREACH(const Argument& arg, argList)  | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(i != argList.size()-1) | 
					
						
							|  |  |  |             proxyFile.oss << arg.type << " " << arg.name << ", "; | 
					
						
							| 
									
										
										
										
											2012-08-27 23:19:55 +08:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |             proxyFile.oss << arg.type << " " << arg.name; | 
					
						
							|  |  |  |         i++; | 
					
						
							| 
									
										
										
										
											2012-08-27 23:19:55 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         proxyFile.oss << ")" << endl; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   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 << ", this, varargin{:});\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Output C++ wrapper code
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     const string wrapFunctionName = wrapper_fragment( | 
					
						
							|  |  |  |       wrapperFile, cppClassName, matlabUniqueName, overload, id, typeAttributes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Add to function list
 | 
					
						
							|  |  |  |     functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  |   proxyFile.oss << "      else\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   proxyFile.oss << "        error('Arguments do not match any overload of function " << | 
					
						
							|  |  |  |     matlabQualName << "." << name << "');" << endl; | 
					
						
							| 
									
										
										
										
											2012-07-05 22:05:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   proxyFile.oss << "      end\n"; | 
					
						
							|  |  |  |   proxyFile.oss << "    end\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  | string Method::wrapper_fragment(FileWriter& file,  | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |           const string& cppClassName, | 
					
						
							|  |  |  |           const string& matlabUniqueName, | 
					
						
							|  |  |  |           int overload, | 
					
						
							|  |  |  |           int id, | 
					
						
							|  |  |  |           const TypeAttributesTable& typeAttributes) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   const string wrapFunctionName = matlabUniqueName + "_" + name + "_" + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +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
 | 
					
						
							|  |  |  |   file.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; | 
					
						
							|  |  |  |   // start
 | 
					
						
							|  |  |  |   file.oss << "{\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   if(returnVal.isPair) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2012-06-28 05:50:45 +08:00
										 |  |  |       if(returnVal.category1 == ReturnValue::CLASS) | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |         file.oss << "  typedef boost::shared_ptr<"  << returnVal.qualifiedType1("::")  << "> Shared" <<  returnVal.type1 << ";"<< endl; | 
					
						
							| 
									
										
										
										
											2012-06-28 05:50:45 +08:00
										 |  |  |       if(returnVal.category2 == ReturnValue::CLASS) | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |         file.oss << "  typedef boost::shared_ptr<"  << returnVal.qualifiedType2("::")  << "> Shared" <<  returnVal.type2 << ";"<< endl; | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2012-06-28 05:50:45 +08:00
										 |  |  |       if(returnVal.category1 == ReturnValue::CLASS) | 
					
						
							| 
									
										
										
										
											2012-07-05 22:04:36 +08:00
										 |  |  |         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-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // check arguments
 | 
					
						
							|  |  |  |   // extra argument obj -> nargin-1 is passed !
 | 
					
						
							|  |  |  |   // example: checkArguments("equals",nargout,nargin-1,2);
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "  checkArguments(\"" << name << "\",nargout,nargin-1," << args.size() << ");\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // get class pointer
 | 
					
						
							|  |  |  |   // example: shared_ptr<Test> = unwrap_shared_ptr< Test >(in[0], "Test");
 | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +08:00
										 |  |  |   file.oss << "  Shared obj = unwrap_shared_ptr<" << cppClassName << ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   // unwrap arguments, see Argument.cpp
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   args.matlab_unwrap(file,1); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:19:37 +08:00
										 |  |  |   // call method and wrap result
 | 
					
						
							|  |  |  |   // example: out[0]=wrap<bool>(self->return_field(t));
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   if (returnVal.type1!="void") | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     returnVal.wrap_result("obj->"+name+"("+args.names()+")", file, typeAttributes); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     file.oss << "  obj->"+name+"("+args.names()+");\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // finish
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "}\n"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return wrapFunctionName; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |