| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file ReturnValue.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @brief Encapsulates a return value from a method | 
					
						
							|  |  |  |  * @date Dec 1, 2011 | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  |  * @author Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 02:34:15 +08:00
										 |  |  | #include "ReturnType.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  | #include "TemplateSubstitution.h"
 | 
					
						
							| 
									
										
										
										
											2012-01-16 05:42:41 +08:00
										 |  |  | #include "FileWriter.h"
 | 
					
						
							| 
									
										
										
										
											2012-07-13 06:28:28 +08:00
										 |  |  | #include "TypeAttributesTable.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-12 21:37:08 +08:00
										 |  |  | #include "utilities.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-26 01:29:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | namespace wrap { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:40:39 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2014-11-14 02:34:15 +08:00
										 |  |  |  * Encapsulates return type of a method or function, possibly a pair | 
					
						
							| 
									
										
										
										
											2014-11-12 20:31:46 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | struct ReturnValue { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool isPair; | 
					
						
							|  |  |  |   ReturnType type1, type2; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 06:24:24 +08:00
										 |  |  |   friend struct ReturnValueGrammar; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-26 02:40:39 +08:00
										 |  |  |   /// Constructor
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:31:40 +08:00
										 |  |  |   ReturnValue() : | 
					
						
							|  |  |  |       isPair(false) { | 
					
						
							| 
									
										
										
										
											2014-05-26 02:40:39 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 05:06:53 +08:00
										 |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   ReturnValue(const ReturnType& type) : | 
					
						
							|  |  |  |       isPair(false), type1(type) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 05:33:30 +08:00
										 |  |  |   /// Constructor
 | 
					
						
							|  |  |  |   ReturnValue(const ReturnType& t1, const ReturnType& t2) : | 
					
						
							|  |  |  |       isPair(true), type1(t1), type2(t2) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual void clear() { | 
					
						
							|  |  |  |     type1.clear(); | 
					
						
							|  |  |  |     type2.clear(); | 
					
						
							|  |  |  |     isPair = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool operator==(const ReturnValue& other) const { | 
					
						
							|  |  |  |     return isPair == other.isPair && type1 == other.type1 | 
					
						
							|  |  |  |         && type2 == other.type2; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |   /// Substitute template argument
 | 
					
						
							| 
									
										
										
										
											2014-11-14 00:28:05 +08:00
										 |  |  |   ReturnValue expandTemplate(const TemplateSubstitution& ts) const; | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 21:37:08 +08:00
										 |  |  |   std::string return_type(bool add_ptr) const; | 
					
						
							| 
									
										
										
										
											2011-12-09 04:51:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   std::string matlab_returnType() const; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 03:52:07 +08:00
										 |  |  |   void wrap_result(const std::string& result, FileWriter& wrapperFile, | 
					
						
							| 
									
										
										
										
											2014-05-26 01:29:06 +08:00
										 |  |  |       const TypeAttributesTable& typeAttributes) const; | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   void wrapTypeUnwrap(FileWriter& wrapperFile) const; | 
					
						
							| 
									
										
										
										
											2012-07-24 02:24:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 03:52:07 +08:00
										 |  |  |   void emit_matlab(FileWriter& proxyFile) const; | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 04:11:29 +08:00
										 |  |  |   friend std::ostream& operator<<(std::ostream& os, const ReturnValue& r) { | 
					
						
							|  |  |  |     if (!r.isPair && r.type1.category == ReturnType::VOID) | 
					
						
							|  |  |  |       os << "void"; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       os << r.return_type(true); | 
					
						
							|  |  |  |     return os; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 10:32:18 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2011-12-03 00:43:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 06:26:07 +08:00
										 |  |  | //******************************************************************************
 | 
					
						
							|  |  |  | // http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html
 | 
					
						
							|  |  |  | struct ReturnValueGrammar: public classic::grammar<ReturnValueGrammar> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   wrap::ReturnValue& result_; ///< successful parse will be placed in here
 | 
					
						
							| 
									
										
										
										
											2014-12-02 03:03:26 +08:00
										 |  |  |   ReturnTypeGrammar returnType1_g, returnType2_g; ///< Type parsers
 | 
					
						
							| 
									
										
										
										
											2014-12-01 06:26:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// Construct type grammar and specify where result is placed
 | 
					
						
							|  |  |  |   ReturnValueGrammar(wrap::ReturnValue& result) : | 
					
						
							|  |  |  |       result_(result), returnType1_g(result.type1), returnType2_g(result.type2) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Definition of type grammar
 | 
					
						
							|  |  |  |   template<typename ScannerT> | 
					
						
							|  |  |  |   struct definition { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     classic::rule<ScannerT> pair_p, returnValue_p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     definition(ReturnValueGrammar const& self) { | 
					
						
							|  |  |  |       using namespace classic; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       pair_p = (str_p("pair") >> '<' >> self.returnType1_g >> ',' | 
					
						
							|  |  |  |           >> self.returnType2_g >> '>')[assign_a(self.result_.isPair, T)]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       returnValue_p = pair_p | self.returnType1_g; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     classic::rule<ScannerT> const& start() const { | 
					
						
							|  |  |  |       return returnValue_p; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | // ReturnValueGrammar
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 03:03:26 +08:00
										 |  |  | }// \namespace wrap
 |