| 
									
										
										
										
											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 Constructor.ccp | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "utilities.h"
 | 
					
						
							|  |  |  | #include "Constructor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string Constructor::matlab_wrapper_name(const string& className) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   string str = "new_" + className + "_" + args.signature(); | 
					
						
							|  |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | void Constructor::matlab_proxy_fragment(FileWriter& file, const string& className) const { | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  | 	size_t nrArgs = args.size(); | 
					
						
							|  |  |  | 	// check for number of arguments...
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "      if (nargin == " << nrArgs; | 
					
						
							|  |  |  |   if (nrArgs>0) file.oss << " && "; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  | 	// ...and their types
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   bool first = true; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |   for(size_t i=0;i<nrArgs;i++) { | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |     if (!first) file.oss << " && "; | 
					
						
							|  |  |  |     file.oss << "isa(varargin{" << i+1 << "},'" << args[i].matlabClass() << "')"; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |     first=false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // emit code for calling constructor
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "), obj.self = " << matlab_wrapper_name(className) << "("; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |   // emit constructor arguments
 | 
					
						
							|  |  |  |   first = true; | 
					
						
							|  |  |  |   for(size_t i=0;i<nrArgs;i++) { | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |     if (!first) file.oss << ","; | 
					
						
							|  |  |  |     file.oss << "varargin{" << i+1 << "}"; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     first=false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "); end" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | void Constructor::matlab_mfile(const string& toolboxPath, const string& qualifiedMatlabName) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string matlabName = matlab_wrapper_name(qualifiedMatlabName); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open destination m-file
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string wrapperFile = toolboxPath + "/" + matlabName + ".m"; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  |   FileWriter file(wrapperFile, verbose_, "%"); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << "function result = " << matlabName << "(obj"; | 
					
						
							|  |  |  |   if (args.size()) file.oss << "," << args.names(); | 
					
						
							|  |  |  |   file.oss << ")" << endl; | 
					
						
							|  |  |  |   file.oss << "  error('need to compile " << matlabName << ".cpp');" << endl; | 
					
						
							|  |  |  |   file.oss << "end" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // close file
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.emit(true); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  | void Constructor::matlab_wrapper(const string& toolboxPath, | 
					
						
							|  |  |  | 				 const string& cppClassName, | 
					
						
							|  |  |  | 				 const string& matlabClassName, | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | 				 const vector<string>& using_namespaces, const vector<string>& includes) const { | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string matlabName = matlab_wrapper_name(matlabClassName); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // open destination wrapperFile
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:13 +08:00
										 |  |  |   string wrapperFile = toolboxPath + "/" + matlabName + ".cpp"; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:44 +08:00
										 |  |  |   FileWriter file(wrapperFile, verbose_, "//"); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // generate code
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   generateIncludes(file, name, includes); | 
					
						
							|  |  |  |   generateUsingNamespace(file, using_namespaces); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   file.oss << "void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])" << endl; | 
					
						
							|  |  |  |   file.oss << "{" << endl; | 
					
						
							|  |  |  |   file.oss << "  checkArguments(\"" << matlabName << "\",nargout,nargin," << args.size() << ");" << endl; | 
					
						
							|  |  |  |   args.matlab_unwrap(file); // unwrap arguments
 | 
					
						
							|  |  |  |   file.oss << "  " << cppClassName << "* self = new " << cppClassName << "(" << args.names() << ");" << endl; // need qualified name, delim: "::"
 | 
					
						
							|  |  |  |   file.oss << "  out[0] = wrap_constructed(self,\"" << matlabClassName << "\");" << endl; // need matlab qualified name
 | 
					
						
							|  |  |  |   file.oss << "}" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // close file
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.emit(true); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |