| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2014-11-30 04:43:48 +08:00
										 |  |  | #include <boost/lexical_cast.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | Argument Argument::expandTemplate(const TemplateSubstitution& ts) const { | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   Argument instArg = *this; | 
					
						
							| 
									
										
										
										
											2014-11-30 07:13:29 +08:00
										 |  |  |   instArg.type = ts.tryToSubstitite(type); | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   return instArg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-30 04:11:13 +08:00
										 |  |  | ArgumentList ArgumentList::expandTemplate( | 
					
						
							|  |  |  |     const TemplateSubstitution& ts) const { | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   ArgumentList instArgList; | 
					
						
							|  |  |  |   BOOST_FOREACH(const Argument& arg, *this) { | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |     Argument instArg = arg.expandTemplate(ts); | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |     instArgList.push_back(instArg); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return instArgList; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   BOOST_FOREACH(const string& ns, type.namespaces()) | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     result += ns + delim; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (type.name() == "string" || type.name() == "unsigned char" | 
					
						
							|  |  |  |       || type.name() == "char") | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     return result + "char"; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (type.name() == "Vector" || type.name() == "Matrix") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "double"; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (type.name() == "int" || type.name() == "size_t") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "numeric"; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (type.name() == "bool") | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |     return result + "logical"; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   return result + type.name(); | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 03:21:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool Argument::isScalar() const { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   return (type.name() == "bool" || type.name() == "char" | 
					
						
							|  |  |  |       || type.name() == "unsigned char" || type.name() == "int" | 
					
						
							|  |  |  |       || type.name() == "size_t" || type.name() == "double"); | 
					
						
							| 
									
										
										
										
											2014-05-26 03:21:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   string cppType = type.qualifiedName("::"); | 
					
						
							|  |  |  |   string matlabUniqueType = type.qualifiedName(); | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 00:34:34 +08:00
										 |  |  |   if (is_ptr && type.category != Qualified::EIGEN) | 
					
						
							| 
									
										
										
										
											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< "; | 
					
						
							| 
									
										
										
										
											2014-12-13 00:34:34 +08:00
										 |  |  |   else if (is_ref && type.category != Qualified::EIGEN) | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     // 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-12-13 00:34:34 +08:00
										 |  |  |   if( (is_ptr || is_ref) && type.category != Qualified::EIGEN) | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     file.oss << ", \"ptr_" << matlabUniqueType << "\""; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |   file.oss << ");" << endl; | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 04:11:13 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-30 04:43:48 +08:00
										 |  |  | void Argument::proxy_check(FileWriter& proxyFile, const string& s) const { | 
					
						
							|  |  |  |   proxyFile.oss << "isa(" << s << ",'" << matlabClass(".") << "')"; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   if (type.name() == "Vector") | 
					
						
							| 
									
										
										
										
											2014-11-30 04:43:48 +08:00
										 |  |  |     proxyFile.oss << " && size(" << s << ",2)==1"; | 
					
						
							| 
									
										
										
										
											2014-11-30 04:11:13 +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 += ","; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     str += arg.type.name(); | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     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) { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     BOOST_FOREACH(char ch, arg.type.name()) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |       sig += arg.type.name()[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) | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |     if (!arg.isScalar()) | 
					
						
							|  |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2014-05-26 03:21:13 +08:00
										 |  |  |   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-12-01 03:12:03 +08:00
										 |  |  |     file.oss << arg.type.name() << " " << arg.name; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:12:48 +08:00
										 |  |  |     first = false; | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   file.oss << ")"; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-30 03:53:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:01:36 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2014-11-30 04:11:13 +08:00
										 |  |  | void ArgumentList::proxy_check(FileWriter& proxyFile) const { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   // Check nr of arguments
 | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |   proxyFile.oss << "if length(varargin) == " << size(); | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   if (size() > 0) | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |     proxyFile.oss << " && "; | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   // ...and their type.names
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |   bool first = true; | 
					
						
							|  |  |  |   for (size_t i = 0; i < size(); i++) { | 
					
						
							|  |  |  |     if (!first) | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |       proxyFile.oss << " && "; | 
					
						
							| 
									
										
										
										
											2014-11-30 04:43:48 +08:00
										 |  |  |     string s = "varargin{" + boost::lexical_cast<string>(i + 1) + "}"; | 
					
						
							|  |  |  |     (*this)[i].proxy_check(proxyFile, s); | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  |     first = false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-11-13 03:51:47 +08:00
										 |  |  |   proxyFile.oss << "\n"; | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-30 04:11:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:35:07 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 |