| 
									
										
										
										
											2014-11-12 09:49:23 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file Qualified.h | 
					
						
							|  |  |  |  * @brief Qualified name | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @date Nov 11, 2014 | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  | #include <boost/spirit/include/classic_core.hpp>
 | 
					
						
							|  |  |  | #include <boost/spirit/include/classic_push_back_actor.hpp>
 | 
					
						
							|  |  |  | #include <boost/spirit/include/classic_clear_actor.hpp>
 | 
					
						
							|  |  |  | #include <boost/spirit/include/classic_assign_actor.hpp>
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  | #include <boost/spirit/include/classic_confix.hpp>
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace classic = BOOST_SPIRIT_CLASSIC_NS; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace wrap { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Class to encapuslate a qualified name, i.e., with (nested) namespaces | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  | class Qualified { | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  | //protected:
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<std::string> namespaces_; ///< Stack of namespaces
 | 
					
						
							|  |  |  |   std::string name_; ///< type name
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend class TypeGrammar; | 
					
						
							|  |  |  |   friend class TemplateSubstitution; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |   /// the different categories
 | 
					
						
							|  |  |  |   typedef enum { | 
					
						
							|  |  |  |     CLASS = 1, EIGEN = 2, BASIS = 3, VOID = 4 | 
					
						
							|  |  |  |   } Category; | 
					
						
							|  |  |  |   Category category; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |   Qualified() : | 
					
						
							|  |  |  |       category(VOID) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   Qualified(const std::string& n, Category c = CLASS) : | 
					
						
							|  |  |  |       name_(n), category(c) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Qualified(const std::string& ns1, const std::string& ns2, | 
					
						
							|  |  |  |       const std::string& n, Category c = CLASS) : | 
					
						
							|  |  |  |       name_(n), category(c) { | 
					
						
							|  |  |  |     namespaces_.push_back(ns1); | 
					
						
							|  |  |  |     namespaces_.push_back(ns2); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Qualified(const std::string& ns1, const std::string& n, Category c = CLASS) : | 
					
						
							|  |  |  |       name_(n), category(c) { | 
					
						
							|  |  |  |     namespaces_.push_back(ns1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  |   Qualified(std::vector<std::string> ns, const std::string& name) : | 
					
						
							| 
									
										
										
										
											2014-12-01 16:48:56 +08:00
										 |  |  |       namespaces_(ns), name_(name), category(CLASS) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   std::string name() const { | 
					
						
							|  |  |  |     return name_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::vector<std::string> namespaces() const { | 
					
						
							|  |  |  |     return namespaces_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Qualified is 'abused' as template argument name as well
 | 
					
						
							|  |  |  |   // this function checks whether *this matches with templateArg
 | 
					
						
							|  |  |  |   bool match(const std::string& templateArg) const { | 
					
						
							| 
									
										
										
										
											2014-12-01 04:54:10 +08:00
										 |  |  |     return (name_ == templateArg && namespaces_.empty());//TODO && category == CLASS);
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void rename(const Qualified& q) { | 
					
						
							|  |  |  |     namespaces_ = q.namespaces_; | 
					
						
							|  |  |  |     name_ = q.name_; | 
					
						
							|  |  |  |     category = q.category; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void expand(const std::string& expansion) { | 
					
						
							|  |  |  |     name_ += expansion; | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool operator==(const Qualified& other) const { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     return namespaces_ == other.namespaces_ && name_ == other.name_ | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |         && category == other.category; | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   bool empty() const { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     return namespaces_.empty() && name_.empty(); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 05:33:30 +08:00
										 |  |  |   virtual void clear() { | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     namespaces_.clear(); | 
					
						
							|  |  |  |     name_.clear(); | 
					
						
							|  |  |  |     category = VOID; | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static Qualified MakeClass(std::vector<std::string> namespaces, | 
					
						
							|  |  |  |       const std::string& name) { | 
					
						
							|  |  |  |     return Qualified(namespaces, name); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static Qualified MakeEigen(const std::string& name) { | 
					
						
							|  |  |  |     return Qualified(name, EIGEN); | 
					
						
							| 
									
										
										
										
											2014-11-13 07:39:15 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  |   static Qualified MakeBasis(const std::string& name) { | 
					
						
							|  |  |  |     return Qualified(name, BASIS); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:38:24 +08:00
										 |  |  |   static Qualified MakeVoid() { | 
					
						
							|  |  |  |     return Qualified("void", VOID); | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 19:52:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |   /// Return a qualified string using given delimiter
 | 
					
						
							|  |  |  |   std::string qualifiedName(const std::string& delimiter = "") const { | 
					
						
							|  |  |  |     std::string result; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     for (std::size_t i = 0; i < namespaces_.size(); ++i) | 
					
						
							|  |  |  |       result += (namespaces_[i] + delimiter); | 
					
						
							|  |  |  |     result += name_; | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Return a matlab file name, i.e. "toolboxPath/+ns1/+ns2/name.m"
 | 
					
						
							|  |  |  |   std::string matlabName(const std::string& toolboxPath) const { | 
					
						
							|  |  |  |     std::string result = toolboxPath; | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     for (std::size_t i = 0; i < namespaces_.size(); ++i) | 
					
						
							|  |  |  |       result += ("/+" + namespaces_[i]); | 
					
						
							|  |  |  |     result += "/" + name_ + ".m"; | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 04:11:29 +08:00
										 |  |  |   friend std::ostream& operator<<(std::ostream& os, const Qualified& q) { | 
					
						
							|  |  |  |     os << q.qualifiedName("::"); | 
					
						
							|  |  |  |     return os; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 09:49:23 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  | template<typename ScannerT> | 
					
						
							|  |  |  | struct basic_rules { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef classic::rule<ScannerT> Rule; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |   Rule comments_p, basisType_p, eigenType_p, keywords_p, stlType_p, name_p, | 
					
						
							| 
									
										
										
										
											2014-12-01 07:01:31 +08:00
										 |  |  |       className_p, namespace_p; | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   basic_rules() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     using namespace classic; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |     comments_p = comment_p("/*", "*/") | comment_p("//", eol_p); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |     basisType_p = (str_p("string") | "bool" | "size_t" | "int" | "double" | 
					
						
							|  |  |  |         | "char" | "unsigned char"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     eigenType_p = (str_p("Vector") | "Matrix"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     keywords_p = | 
					
						
							|  |  |  |         (str_p("const") | "static" | "namespace" | "void" | basisType_p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stlType_p = (str_p("vector") | "list"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  |     name_p = lexeme_d[alpha_p >> *(alnum_p | '_')]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |     className_p = (lexeme_d[upper_p >> *(alnum_p | '_')] - eigenType_p | 
					
						
							|  |  |  |         - keywords_p) | stlType_p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 07:01:31 +08:00
										 |  |  |     namespace_p = lexeme_d[lower_p >> *(alnum_p | '_')] - keywords_p; | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | // http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  | class TypeGrammar: public classic::grammar<TypeGrammar> { | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   wrap::Qualified& result_; ///< successful parse will be placed in here
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  |   /// Construct type grammar and specify where result is placed
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |   TypeGrammar(wrap::Qualified& result) : | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  |       result_(result) { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |   /// Definition of type grammar
 | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  |   template<typename ScannerT> | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  |   struct definition: basic_rules<ScannerT> { | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     typedef classic::rule<ScannerT> Rule; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  |     Rule void_p, my_basisType_p, my_eigenType_p, namespace_del_p, class_p, | 
					
						
							|  |  |  |         type_p; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |     definition(TypeGrammar const& self) { | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |       using namespace wrap; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  |       using namespace classic; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |       // HACK: use const values instead of using enums themselves - somehow this doesn't result in values getting assigned to gibberish
 | 
					
						
							|  |  |  |       static const Qualified::Category EIGEN = Qualified::EIGEN; | 
					
						
							|  |  |  |       static const Qualified::Category BASIS = Qualified::BASIS; | 
					
						
							|  |  |  |       static const Qualified::Category CLASS = Qualified::CLASS; | 
					
						
							|  |  |  |       static const Qualified::Category VOID = Qualified::VOID; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |       void_p = str_p("void")[assign_a(self.result_.name_)] //
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |           [assign_a(self.result_.category, VOID)]; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  |       my_basisType_p = basic_rules<ScannerT>::basisType_p //
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |           [assign_a(self.result_.name_)] //
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |           [assign_a(self.result_.category, BASIS)]; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  |       my_eigenType_p = basic_rules<ScannerT>::eigenType_p //
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |           [assign_a(self.result_.name_)] //
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |           [assign_a(self.result_.category, EIGEN)]; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 07:01:31 +08:00
										 |  |  |       namespace_del_p = basic_rules<ScannerT>::namespace_p //
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |       [push_back_a(self.result_.namespaces_)] >> str_p("::"); | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  |       class_p = *namespace_del_p >> basic_rules<ScannerT>::className_p //
 | 
					
						
							| 
									
										
										
										
											2014-12-01 03:12:03 +08:00
										 |  |  |           [assign_a(self.result_.name_)] //
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  |           [assign_a(self.result_.category, CLASS)]; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 04:54:10 +08:00
										 |  |  |       type_p = void_p | my_basisType_p | class_p | my_eigenType_p; | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Rule const& start() const { | 
					
						
							|  |  |  |       return type_p; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-30 20:09:23 +08:00
										 |  |  | // type_grammar
 | 
					
						
							| 
									
										
										
										
											2014-11-30 18:30:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 05:33:30 +08:00
										 |  |  | // Needed for other parsers in Argument.h and ReturnType.h
 | 
					
						
							|  |  |  | static const bool T = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:08:42 +08:00
										 |  |  | }// \namespace wrap
 | 
					
						
							| 
									
										
										
										
											2014-11-30 20:27:04 +08:00
										 |  |  | 
 |