| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |  * 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 OverloadedFunction.h | 
					
						
							|  |  |  |  * @brief Function that can overload its arguments only | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @date Nov 13, 2014 | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Function.h"
 | 
					
						
							|  |  |  | #include "Argument.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | #include <unordered_set>
 | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | namespace wrap { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * ArgumentList Overloads | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ArgumentOverloads { | 
					
						
							| 
									
										
										
										
											2016-09-12 23:05:10 +08:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |   std::vector<ArgumentList> argLists_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   size_t nrOverloads() const { return argLists_.size(); } | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   const ArgumentList& argumentList(size_t i) const { return argLists_.at(i); } | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   void push_back(const ArgumentList& args) { argLists_.push_back(args); } | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   std::vector<ArgumentList> expandArgumentListsTemplate( | 
					
						
							|  |  |  |       const TemplateSubstitution& ts) const { | 
					
						
							|  |  |  |     std::vector<ArgumentList> result; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     for (const ArgumentList& argList : argLists_) { | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |       ArgumentList instArgList = argList.expandTemplate(ts); | 
					
						
							|  |  |  |       result.push_back(instArgList); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Expand templates, imperative !
 | 
					
						
							|  |  |  |   virtual void ExpandTemplate(const TemplateSubstitution& ts) { | 
					
						
							|  |  |  |     argLists_ = expandArgumentListsTemplate(ts); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void verifyArguments(const std::vector<std::string>& validArgs, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                        const std::string s) const { | 
					
						
							|  |  |  |     for (const ArgumentList& argList : argLists_) { | 
					
						
							|  |  |  |       for (Argument arg : argList) { | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |         std::string fullType = arg.type.qualifiedName("::"); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |         if (find(validArgs.begin(), validArgs.end(), fullType) == | 
					
						
							|  |  |  |             validArgs.end()) | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |           throw DependencyMissing(fullType, "checking argument of " + s); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   friend std::ostream& operator<<(std::ostream& os, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                                   const ArgumentOverloads& overloads) { | 
					
						
							|  |  |  |     for (const ArgumentList& argList : overloads.argLists_) | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |       os << argList << std::endl; | 
					
						
							|  |  |  |     return os; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 02:07:13 +08:00
										 |  |  |   std::string pyx_resolveOverloadParams(const ArgumentList& args, bool isVoid, | 
					
						
							|  |  |  |       size_t indentLevel = 2) const { | 
					
						
							| 
									
										
										
										
											2016-11-23 06:32:48 +08:00
										 |  |  |     std::string indent; | 
					
						
							| 
									
										
										
										
											2017-08-07 02:07:13 +08:00
										 |  |  |     for (size_t i = 0; i < indentLevel; ++i) | 
					
						
							|  |  |  |       indent += "    "; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     std::string s; | 
					
						
							| 
									
										
										
										
											2017-08-07 02:07:13 +08:00
										 |  |  |     s += indent + "__params = process_args([" + args.pyx_paramsList() | 
					
						
							|  |  |  |         + "], args, kwargs)\n"; | 
					
						
							|  |  |  |     s += args.pyx_castParamsToPythonType(indent); | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     if (args.size() > 0) { | 
					
						
							| 
									
										
										
										
											2017-08-07 02:07:13 +08:00
										 |  |  |       for (size_t i = 0; i < args.size(); ++i) { | 
					
						
							|  |  |  |         // For python types we can do the assert after the assignment and save list accesses
 | 
					
						
							| 
									
										
										
										
											2016-11-30 00:58:22 +08:00
										 |  |  |         if (args[i].type.isNonBasicType() || args[i].type.isEigen()) { | 
					
						
							| 
									
										
										
										
											2017-08-07 02:07:13 +08:00
										 |  |  |           std::string param = args[i].name; | 
					
						
							|  |  |  |           s += indent + "assert isinstance(" + param + ", " | 
					
						
							|  |  |  |               + args[i].type.pyxArgumentType() + ")"; | 
					
						
							|  |  |  |           if (args[i].type.isEigen()) { | 
					
						
							|  |  |  |             s += " and " + param + ".ndim == " | 
					
						
							|  |  |  |                 + ((args[i].type.pyxClassName() == "Vector") ? "1" : "2"); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           s += "\n"; | 
					
						
							| 
									
										
										
										
											2016-11-30 00:58:22 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return s; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | class OverloadedFunction : public Function, public ArgumentOverloads { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |   bool addOverload(const std::string& name, const ArgumentList& args, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                    boost::optional<const Qualified> instName = boost::none, | 
					
						
							|  |  |  |                    bool verbose = false) { | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |     bool first = initializeOrCheck(name, instName, verbose); | 
					
						
							|  |  |  |     ArgumentOverloads::push_back(args); | 
					
						
							|  |  |  |     return first; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Templated checking functions
 | 
					
						
							|  |  |  | // TODO: do this via polymorphism, use transform ?
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | template <class F> | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | static std::map<std::string, F> expandMethodTemplate( | 
					
						
							|  |  |  |     const std::map<std::string, F>& methods, const TemplateSubstitution& ts) { | 
					
						
							|  |  |  |   std::map<std::string, F> result; | 
					
						
							|  |  |  |   typedef std::pair<const std::string, F> NamedMethod; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   for (NamedMethod namedMethod : methods) { | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |     F instMethod = namedMethod.second; | 
					
						
							|  |  |  |     instMethod.expandTemplate(ts); | 
					
						
							|  |  |  |     namedMethod.second = instMethod; | 
					
						
							|  |  |  |     result.insert(namedMethod); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | template <class F> | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  | inline void verifyArguments(const std::vector<std::string>& validArgs, | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |                             const std::map<std::string, F>& vt) { | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |   typedef typename std::map<std::string, F>::value_type NamedMethod; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  |   for (const NamedMethod& namedMethod : vt) | 
					
						
							| 
									
										
										
										
											2014-11-14 23:43:53 +08:00
										 |  |  |     namedMethod.second.verifyArguments(validArgs); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:43 +08:00
										 |  |  | }  // \namespace wrap
 |