| 
									
										
										
										
											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 Argument.ccp | 
					
						
							|  |  |  |  * @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-10-14 02:41:56 +08:00
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  | #include "Argument.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-12-15 05:10:56 +08:00
										 |  |  | #include <boost/regex.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | using namespace wrap; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-29 03:44:33 +08:00
										 |  |  | string Argument::matlabClass(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string result; | 
					
						
							|  |  |  |   BOOST_FOREACH(const string& ns, namespaces) | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     result += ns + delim; | 
					
						
							|  |  |  |   if (type == "string" || type == "unsigned char" || type == "char") | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     return result + "char"; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   if (type == "Vector" || type == "Matrix") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "double"; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   if (type == "int" || type == "size_t") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "numeric"; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   if (type == "bool") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "logical"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return result + type; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 03:21:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool Argument::isScalar() const { | 
					
						
							|  |  |  |   return (type == "bool" || type == "char" || type == "unsigned char" | 
					
						
							|  |  |  |       || type == "int" || type == "size_t" || type == "double"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | void Argument::matlab_unwrap(FileWriter& file, const string& matlabName) const { | 
					
						
							|  |  |  |   file.oss << "  "; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:15 +08:00
										 |  |  |   string cppType = qualifiedType("::"); | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +08:00
										 |  |  |   string matlabUniqueType = qualifiedType(); | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-21 10:17:20 +08:00
										 |  |  |   if (is_ptr) | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     // A pointer: emit an "unwrap_shared_ptr" call which returns a pointer
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     file.oss << "boost::shared_ptr<" << cppType << "> " << name | 
					
						
							|  |  |  |         << " = unwrap_shared_ptr< "; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   else if (is_ref) | 
					
						
							|  |  |  |     // A reference: emit an "unwrap_shared_ptr" call and de-reference the pointer
 | 
					
						
							|  |  |  |     file.oss << cppType << "& " << name << " = *unwrap_shared_ptr< "; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     // Not a pointer or a reference: emit an "unwrap" call
 | 
					
						
							|  |  |  |     // unwrap is specified in matlab.h as a series of template specializations
 | 
					
						
							|  |  |  |     // that know how to unpack the expected MATLAB object
 | 
					
						
							|  |  |  |     // example: double tol = unwrap< double >(in[2]);
 | 
					
						
							|  |  |  |     // example: Vector v = unwrap< Vector >(in[1]);
 | 
					
						
							|  |  |  |     file.oss << cppType << " " << name << " = unwrap< "; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   file.oss << cppType << " >(" << matlabName; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   if (is_ptr || is_ref) | 
					
						
							|  |  |  |     file.oss << ", \"ptr_" << matlabUniqueType << "\""; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << ");" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:15 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string Argument::qualifiedType(const string& delim) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   string result; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   BOOST_FOREACH(const string& ns, namespaces) | 
					
						
							|  |  |  |     result += ns + delim; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return result + type; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ArgumentList::types() const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   string str; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   bool first = true; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     if (!first) | 
					
						
							|  |  |  |       str += ","; | 
					
						
							|  |  |  |     str += arg.type; | 
					
						
							|  |  |  |     first = false; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ArgumentList::signature() const { | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:09 +08:00
										 |  |  |   string sig; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   bool cap = false; | 
					
						
							| 
									
										
										
										
											2011-12-15 05:10:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     BOOST_FOREACH(char ch, arg.type) | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |       if (isupper(ch)) { | 
					
						
							|  |  |  |         sig += ch; | 
					
						
							|  |  |  |         //If there is a capital letter, we don't want to read it below
 | 
					
						
							|  |  |  |         cap = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     if (!cap) | 
					
						
							|  |  |  |       sig += arg.type[0]; | 
					
						
							| 
									
										
										
										
											2011-12-16 06:26:39 +08:00
										 |  |  |     //Reset to default
 | 
					
						
							|  |  |  |     cap = false; | 
					
						
							| 
									
										
										
										
											2011-12-15 05:10:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:09 +08:00
										 |  |  |   return sig; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ArgumentList::names() const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   string str; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   bool first = true; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     if (!first) | 
					
						
							|  |  |  |       str += ","; | 
					
						
							|  |  |  |     str += arg.name; | 
					
						
							|  |  |  |     first = false; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 03:21:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool ArgumentList::allScalar() const { | 
					
						
							|  |  |  |   BOOST_FOREACH(Argument arg, *this) | 
					
						
							|  |  |  |       if (!arg.isScalar()) return false; | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | void ArgumentList::matlab_unwrap(FileWriter& file, int start) const { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |   int index = start; | 
					
						
							|  |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							|  |  |  |     stringstream buf; | 
					
						
							|  |  |  |     buf << "in[" << index << "]"; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     arg.matlab_unwrap(file, buf.str()); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     index++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  | void ArgumentList::emit_prototype(FileWriter& file, const string& name) const { | 
					
						
							|  |  |  |   file.oss << name << "("; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:12:48 +08:00
										 |  |  |   bool first = true; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     if (!first) | 
					
						
							|  |  |  |       file.oss << ", "; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:12:48 +08:00
										 |  |  |     file.oss << arg.type << " " << arg.name; | 
					
						
							|  |  |  |     first = false; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   file.oss << ")"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-05-26 02:59:20 +08:00
										 |  |  | void ArgumentList::emit_call(FileWriter& file, const ReturnValue& returnVal, | 
					
						
							|  |  |  |     const string& wrapperName, int id, bool staticMethod) const { | 
					
						
							|  |  |  |   returnVal.emit_matlab(file); | 
					
						
							|  |  |  |   file.oss << wrapperName << "(" << id; | 
					
						
							|  |  |  |   if (!staticMethod) | 
					
						
							|  |  |  |     file.oss << ", this"; | 
					
						
							|  |  |  |   file.oss << ", varargin{:});\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  | void ArgumentList::emit_conditional_call(FileWriter& file, | 
					
						
							|  |  |  |     const ReturnValue& returnVal, const string& wrapperName, int id, | 
					
						
							|  |  |  |     bool staticMethod) const { | 
					
						
							|  |  |  |   // Check nr of arguments
 | 
					
						
							|  |  |  |   file.oss << "if length(varargin) == " << size(); | 
					
						
							|  |  |  |   if (size() > 0) | 
					
						
							|  |  |  |     file.oss << " && "; | 
					
						
							|  |  |  |   // ...and their types
 | 
					
						
							|  |  |  |   bool first = true; | 
					
						
							|  |  |  |   for (size_t i = 0; i < size(); i++) { | 
					
						
							|  |  |  |     if (!first) | 
					
						
							|  |  |  |       file.oss << " && "; | 
					
						
							|  |  |  |     file.oss << "isa(varargin{" << i + 1 << "},'" << (*this)[i].matlabClass(".") | 
					
						
							|  |  |  |         << "')"; | 
					
						
							|  |  |  |     first = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   file.oss << "\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // output call to C++ wrapper
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:52:49 +08:00
										 |  |  |   file.oss << "        "; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:59:20 +08:00
										 |  |  |   emit_call(file, returnVal, wrapperName, id, staticMethod); | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 |