| 
									
										
										
										
											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
										 |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-12-15 05:10:56 +08:00
										 |  |  | #include <boost/regex.hpp>
 | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Argument.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  |   result += ns + delim; | 
					
						
							|  |  |  |   if (type=="string" || type=="unsigned char" || type=="char") | 
					
						
							|  |  |  |     return result + "char"; | 
					
						
							| 
									
										
										
										
											2012-06-13 20:03:33 +08:00
										 |  |  |   if (type=="Vector" || type=="Matrix") | 
					
						
							|  |  |  |     return result + "double"; | 
					
						
							|  |  |  |   if (type=="int" || type=="size_t") | 
					
						
							|  |  |  |     return result + "numeric"; | 
					
						
							|  |  |  |   if (type=="bool") | 
					
						
							|  |  |  |     return result + "logical"; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   return result + type; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |     file.oss << "boost::shared_ptr<" << cppType << "> " << name << " = unwrap_shared_ptr< "; | 
					
						
							|  |  |  |   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; | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +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; | 
					
						
							|  |  |  |   BOOST_FOREACH(const string& ns, namespaces) result += ns + delim; | 
					
						
							|  |  |  |   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; | 
					
						
							|  |  |  |   bool first=true; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     if (!first) str += ","; str += arg.type; first=false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2011-12-12 05:09:07 +08:00
										 |  |  | string ArgumentList::signature() const { | 
					
						
							| 
									
										
										
										
											2011-12-16 03:39:09 +08:00
										 |  |  |   string sig; | 
					
						
							| 
									
										
										
										
											2011-12-16 06:26:39 +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) | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |         if(isupper(ch)) { | 
					
						
							| 
									
										
										
										
											2011-12-16 06:26:39 +08:00
										 |  |  |             sig += ch; | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |             //If there is a capital letter, we don't want to read it below
 | 
					
						
							| 
									
										
										
										
											2011-12-16 06:26:39 +08:00
										 |  |  |             cap=true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-01-10 13:06:46 +08:00
										 |  |  |     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; | 
					
						
							|  |  |  |   bool first=true; | 
					
						
							|  |  |  |   BOOST_FOREACH(Argument arg, *this) { | 
					
						
							|  |  |  |     if (!first) str += ","; str += arg.name; first=false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return str; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											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 << "]"; | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  |     arg.matlab_unwrap(file,buf.str()); | 
					
						
							| 
									
										
										
										
											2011-10-14 02:41:56 +08:00
										 |  |  |     index++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2012-06-27 02:52:27 +08:00
										 |  |  | 
 |