diff --git a/wrap/Function.cpp b/wrap/Function.cpp new file mode 100644 index 000000000..8fd1d0655 --- /dev/null +++ b/wrap/Function.cpp @@ -0,0 +1,71 @@ +/* ---------------------------------------------------------------------------- + + * 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 Function.ccp + * @author Frank Dellaert + * @date Nov 13, 2014 + **/ + +#include "Function.h" +#include "utilities.h" + +#include +#include +#include + +#include +#include + +using namespace std; +using namespace wrap; + +/* ************************************************************************* */ +void Function::addOverload(bool verbose, const std::string& name, + const ArgumentList& args, const ReturnValue& retVal, + const Qualified& instName) { + + // Check if this overload is give to the correct method + if (name_.empty()) + name_ = name; + else if (name_ != name) + throw std::runtime_error( + "Function::addOverload: tried to add overload with name " + name + + " instead of expected " + name_); + + // Check if this overload is give to the correct method + if (templateArgValue_.empty()) + templateArgValue_ = instName; + else if (templateArgValue_ != instName) + throw std::runtime_error( + "Function::addOverload: tried to add overload with template argument " + + instName.qualifiedName(":") + " instead of expected " + + templateArgValue_.qualifiedName(":")); + + verbose_ = verbose; + argLists.push_back(args); + returnVals.push_back(retVal); +} + +/* ************************************************************************* */ +vector Function::expandArgumentListsTemplate( + const string& templateArg, const Qualified& qualifiedType, + const Qualified& expandedClass) const { + vector result; + BOOST_FOREACH(const ArgumentList& argList, argLists) { + ArgumentList instArgList = argList.expandTemplate(templateArg, + qualifiedType, expandedClass); + result.push_back(instArgList); + } + return result; +} + +/* ************************************************************************* */ diff --git a/wrap/Function.h b/wrap/Function.h new file mode 100644 index 000000000..76b513907 --- /dev/null +++ b/wrap/Function.h @@ -0,0 +1,109 @@ +/* ---------------------------------------------------------------------------- + + * 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 Function.h + * @brief Base class for global functions and methods + * @author Frank Dellaert + * @date Nov 13, 2014 + **/ + +#pragma once + +#include "Argument.h" +#include "ReturnValue.h" +#include "TypeAttributesTable.h" + +#include +#include + +namespace wrap { + +/// Function class +struct Function { + + /// Constructor creates empty object + Function(bool verbose = true) : + verbose_(verbose) { + } + + Function(const std::string& name, bool verbose = true) : + verbose_(verbose), name_(name) { + } + + bool verbose_; + std::string name_; ///< name of method + Qualified templateArgValue_; ///< value of template argument if applicable + std::vector argLists; + std::vector returnVals; + + // The first time this function is called, it initializes the class members + // with those in rhs, but in subsequent calls it adds additional argument + // lists as function overloads. + void addOverload(bool verbose, const std::string& name, + const ArgumentList& args, const ReturnValue& retVal, + const Qualified& instName = Qualified()); + + std::vector expandArgumentListsTemplate( + const std::string& templateArg, const Qualified& qualifiedType, + const Qualified& expandedClass) const; +}; + +// Templated checking functions +// TODO: do this via polymorphism ? + +template +FUNCTION expandMethodTemplate(const FUNCTION& method, + const std::string& templateArg, const Qualified& qualifiedType, + const Qualified& expandedClass) { + // Create new instance + FUNCTION instMethod = method; + // substitute template in arguments + instMethod.argLists = method.expandArgumentListsTemplate(templateArg, + qualifiedType, expandedClass); + // do the same for return types + instMethod.returnVals = ReturnValue::ExpandTemplate(method.returnVals, + templateArg, qualifiedType, expandedClass); + // return new method + return instMethod; +} + +// TODO use transform +template +static std::map expandMethodTemplate( + const std::map& methods, + const std::string& templateArg, const Qualified& qualifiedType, + const Qualified& expandedClass) { + std::map result; + typedef std::pair NamedMethod; + BOOST_FOREACH(NamedMethod namedMethod, methods) { + namedMethod.second = expandMethodTemplate(namedMethod.second, templateArg, + qualifiedType, expandedClass); + result.insert(namedMethod); + } + return result; +} +template +inline void verifyReturnTypes(const std::vector& validtypes, + const std::map& vt) { + typedef typename std::map::value_type NamedMethod; + BOOST_FOREACH(const NamedMethod& namedMethod, vt) { + const T& t = namedMethod.second; + BOOST_FOREACH(const ReturnValue& retval, t.returnVals) { + retval.type1.verify(validtypes, t.name_); + if (retval.isPair) + retval.type2.verify(validtypes, t.name_); + } + } +} + +} // \namespace wrap +