| 
									
										
										
										
											2012-07-23 22:09:40 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file GlobalFunction.cpp | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @date Jul 22, 2012 | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "GlobalFunction.h"
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/lexical_cast.hpp>
 | 
					
						
							| 
									
										
										
										
											2012-07-23 22:09:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace wrap { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-23 22:09:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | void GlobalFunction::addOverload(const Qualified& overload, | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |     const ArgumentList& args, const ReturnValue& retVal, | 
					
						
							| 
									
										
										
										
											2014-12-01 16:48:56 +08:00
										 |  |  |     boost::optional<const Qualified> instName, bool verbose) { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   FullyOverloadedFunction::addOverload(overload.name(), args, retVal, instName, | 
					
						
							|  |  |  |       verbose); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   overloads.push_back(overload); | 
					
						
							| 
									
										
										
										
											2012-07-23 22:09:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | void GlobalFunction::matlab_proxy(const string& toolboxPath, | 
					
						
							|  |  |  |     const string& wrapperName, const TypeAttributesTable& typeAttributes, | 
					
						
							|  |  |  |     FileWriter& file, vector<string>& functionNames) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // cluster overloads with same namespace
 | 
					
						
							|  |  |  |   // create new GlobalFunction structures around namespaces - same namespaces and names are overloads
 | 
					
						
							|  |  |  |   // map of namespace to global function
 | 
					
						
							|  |  |  |   typedef map<string, GlobalFunction> GlobalFunctionMap; | 
					
						
							|  |  |  |   GlobalFunctionMap grouped_functions; | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   for (size_t i = 0; i < overloads.size(); ++i) { | 
					
						
							|  |  |  |     Qualified overload = overloads.at(i); | 
					
						
							|  |  |  |     // use concatenated namespaces as key
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     string str_ns = qualifiedName("", overload.namespaces()); | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     const ReturnValue& ret = returnValue(i); | 
					
						
							|  |  |  |     const ArgumentList& args = argumentList(i); | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |     grouped_functions[str_ns].addOverload(overload, args, ret); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   size_t lastcheck = grouped_functions.size(); | 
					
						
							|  |  |  |   BOOST_FOREACH(const GlobalFunctionMap::value_type& p, grouped_functions) { | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     p.second.generateSingleFunction(toolboxPath, wrapperName, typeAttributes, | 
					
						
							|  |  |  |         file, functionNames); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     if (--lastcheck != 0) | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |       file.oss << endl; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | void GlobalFunction::generateSingleFunction(const string& toolboxPath, | 
					
						
							|  |  |  |     const string& wrapperName, const TypeAttributesTable& typeAttributes, | 
					
						
							|  |  |  |     FileWriter& file, vector<string>& functionNames) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create the folder for the namespace
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   const Qualified& overload1 = overloads.front(); | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   createNamespaceStructure(overload1.namespaces(), toolboxPath); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open destination mfunctionFileName
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   string mfunctionFileName = overload1.matlabName(toolboxPath); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   FileWriter mfunctionFile(mfunctionFileName, verbose_, "%"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // get the name of actual matlab object
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   const string matlabQualName = overload1.qualifiedName("."); | 
					
						
							|  |  |  |   const string matlabUniqueName = overload1.qualifiedName(""); | 
					
						
							|  |  |  |   const string cppName = overload1.qualifiedName("::"); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   mfunctionFile.oss << "function varargout = " << name_ << "(varargin)\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   for (size_t i = 0; i < nrOverloads(); ++i) { | 
					
						
							|  |  |  |     const ArgumentList& args = argumentList(i); | 
					
						
							|  |  |  |     const ReturnValue& returnVal = returnValue(i); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const int id = functionNames.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Output proxy matlab code
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     mfunctionFile.oss << "      " << (i == 0 ? "" : "else"); | 
					
						
							| 
									
										
										
										
											2014-11-30 03:59:38 +08:00
										 |  |  |     emit_conditional_call(mfunctionFile, returnVal, args, wrapperName, id); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Output C++ wrapper code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     const string wrapFunctionName = matlabUniqueName + "_" | 
					
						
							|  |  |  |         + boost::lexical_cast<string>(id); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // call
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +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
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     file.oss << "{\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     returnVal.wrapTypeUnwrap(file); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // check arguments
 | 
					
						
							|  |  |  |     // NOTE: for static functions, there is no object passed
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     file.oss << "  checkArguments(\"" << matlabUniqueName | 
					
						
							|  |  |  |         << "\",nargout,nargin," << args.size() << ");\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // unwrap arguments, see Argument.cpp
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     args.matlab_unwrap(file, 0); // We start at 0 because there is no self object
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // call method with default type and wrap result
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     if (returnVal.type1.name() != "void") | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |       returnVal.wrap_result(cppName + "(" + args.names() + ")", file, | 
					
						
							|  |  |  |           typeAttributes); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |       file.oss << cppName + "(" + args.names() + ");\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // finish
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |     file.oss << "}\n"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Add to function list
 | 
					
						
							|  |  |  |     functionNames.push_back(wrapFunctionName); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 04:28:39 +08:00
										 |  |  |   mfunctionFile.oss << "      else\n"; | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |   mfunctionFile.oss | 
					
						
							| 
									
										
										
										
											2014-05-26 04:28:39 +08:00
										 |  |  |       << "        error('Arguments do not match any overload of function " | 
					
						
							| 
									
										
										
										
											2014-05-26 04:01:30 +08:00
										 |  |  |       << matlabQualName << "');" << endl; | 
					
						
							| 
									
										
										
										
											2014-05-26 04:28:39 +08:00
										 |  |  |   mfunctionFile.oss << "      end" << endl; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Close file
 | 
					
						
							|  |  |  |   mfunctionFile.emit(true); | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 00:47:25 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void GlobalFunction::python_wrapper(FileWriter& wrapperFile) const { | 
					
						
							|  |  |  |   wrapperFile.oss << "def(\"" << name_ << "\", " << name_ << ");\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-23 22:09:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace wrap
 | 
					
						
							|  |  |  | 
 |