| 
									
										
										
										
											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-29 22:31:29 +08:00
										 |  |  | #include "StaticMethod.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:22:10 +08:00
										 |  |  | #include "utilities.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-10 04:39:47 +08:00
										 |  |  | #include "Class.h"
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 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"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:34:33 +08:00
										 |  |  | string StaticMethod::wrapper_call(FileWriter& wrapperFile, Str cppClassName, | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  |     Str matlabUniqueName, const ArgumentList& args) const { | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   // 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_; | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  |   if (templateArgValue_) | 
					
						
							|  |  |  |     expanded += ("<" + templateArgValue_->qualifiedName("::") + ">"); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return expanded; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-09-12 06:14:19 +08:00
										 |  |  | void StaticMethod::emit_cython_pxd(FileWriter& file, const Class& cls) const { | 
					
						
							| 
									
										
										
										
											2016-09-09 19:09:39 +08:00
										 |  |  |   // don't support overloads for static method :-(
 | 
					
						
							|  |  |  |   for(size_t i = 0; i < nrOverloads(); ++i) { | 
					
						
							|  |  |  |     file.oss << "\t\t@staticmethod\n"; | 
					
						
							|  |  |  |     file.oss << "\t\t"; | 
					
						
							| 
									
										
										
										
											2016-11-23 01:13:33 +08:00
										 |  |  |     returnVals_[i].emit_cython_pxd(file, cls.pxdClassName()); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     file.oss << name_ + ((i>0)?"_" + to_string(i):"") << " \"" << name_ << "\"" << "("; | 
					
						
							| 
									
										
										
										
											2016-11-23 01:13:33 +08:00
										 |  |  |     argumentList(i).emit_cython_pxd(file, cls.pxdClassName()); | 
					
						
							| 
									
										
										
										
											2016-09-09 19:09:39 +08:00
										 |  |  |     file.oss << ")\n"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void StaticMethod::emit_cython_pyx_no_overload(FileWriter& file, | 
					
						
							|  |  |  |                                                const Class& cls) const { | 
					
						
							|  |  |  |   assert(nrOverloads() == 1); | 
					
						
							|  |  |  |   file.oss << "\t@staticmethod\n"; | 
					
						
							|  |  |  |   file.oss << "\tdef " << name_ << "("; | 
					
						
							|  |  |  |   argumentList(0).emit_cython_pyx(file); | 
					
						
							|  |  |  |   file.oss << "):\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Call cython corresponding function and return
 | 
					
						
							| 
									
										
										
										
											2016-11-23 01:13:33 +08:00
										 |  |  |   string ret = pyx_functionCall(cls.pxd_class_in_pyx(), name_, 0); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   file.oss << "\t\t"; | 
					
						
							|  |  |  |   if (!returnVals_[0].isVoid()) { | 
					
						
							|  |  |  |     file.oss << "return " << returnVals_[0].pyx_casting(ret) << "\n"; | 
					
						
							|  |  |  |   } else | 
					
						
							|  |  |  |     file.oss << ret << "\n"; | 
					
						
							|  |  |  |   file.oss << "\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 19:09:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-09-10 04:39:47 +08:00
										 |  |  | void StaticMethod::emit_cython_pyx(FileWriter& file, const Class& cls) const { | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   size_t N = nrOverloads(); | 
					
						
							|  |  |  |   if (N == 1) { | 
					
						
							|  |  |  |     emit_cython_pyx_no_overload(file, cls); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Dealing with overloads..
 | 
					
						
							|  |  |  |   file.oss << "\t@staticmethod\n"; | 
					
						
							|  |  |  |   file.oss << "\tdef " << name_ << "(*args, **kwargs):\n"; | 
					
						
							|  |  |  |   file.oss << pyx_checkDuplicateNargsKwArgs(); | 
					
						
							|  |  |  |   for (size_t i = 0; i < N; ++i) { | 
					
						
							|  |  |  |     string funcName = name_ + "_" + to_string(i); | 
					
						
							| 
									
										
										
										
											2016-11-23 01:13:33 +08:00
										 |  |  |     file.oss << "\t\tsuccess, results = " << cls.pyxClassName() << "." | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |              << funcName << "(*args, **kwargs)\n"; | 
					
						
							|  |  |  |     file.oss << "\t\tif success:\n\t\t\treturn results\n"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   file.oss << "\t\traise TypeError('Could not find the correct overload')\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for(size_t i = 0; i < N; ++i) { | 
					
						
							| 
									
										
										
										
											2016-09-10 04:39:47 +08:00
										 |  |  |     file.oss << "\t@staticmethod\n"; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     string funcName = name_ + "_" + to_string(i); | 
					
						
							|  |  |  |     string pxdFuncName = name_ + ((i>0)?"_" + to_string(i):""); | 
					
						
							|  |  |  |     ArgumentList args = argumentList(i); | 
					
						
							|  |  |  |     file.oss << "\tdef " + funcName + "(*args, **kwargs):\n"; | 
					
						
							| 
									
										
										
										
											2016-11-22 06:14:30 +08:00
										 |  |  |     file.oss << pyx_resolveOverloadParams(args, false); // lazy: always return None even if it's a void function
 | 
					
						
							| 
									
										
										
										
											2016-09-12 04:40:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-12 05:14:06 +08:00
										 |  |  |     /// Call cython corresponding function and return
 | 
					
						
							| 
									
										
										
										
											2016-11-23 01:13:33 +08:00
										 |  |  |     string ret = pyx_functionCall(cls.pxd_class_in_pyx(), pxdFuncName, i); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     if (!returnVals_[i].isVoid()) { | 
					
						
							|  |  |  |       file.oss << "\t\tcdef " << returnVals_[i].pyx_returnType() | 
					
						
							|  |  |  |               << " ret = " << ret << "\n"; | 
					
						
							|  |  |  |       file.oss << "\t\treturn True, " << returnVals_[i].pyx_casting("ret") << "\n"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |       file.oss << "\t\t" << ret << "\n"; | 
					
						
							|  |  |  |       file.oss << "\t\treturn True, None\n"; | 
					
						
							|  |  |  |     }  | 
					
						
							| 
									
										
										
										
											2016-09-12 04:40:09 +08:00
										 |  |  |     file.oss << "\n"; | 
					
						
							| 
									
										
										
										
											2016-09-10 04:39:47 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |