gtsam/wrap/Argument.h

239 lines
6.9 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------------
* 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 Argument.h
* @brief arguments to constructors and methods
* @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
**/
#pragma once
#include "TemplateSubstitution.h"
2014-05-26 01:29:06 +08:00
#include "FileWriter.h"
#include "ReturnValue.h"
2014-05-26 01:29:06 +08:00
namespace wrap {
2011-10-14 12:43:06 +08:00
/// Argument class
struct Argument {
Qualified type;
std::string name;
2014-11-30 23:08:42 +08:00
bool is_const, is_ref, is_ptr;
Argument() :
is_const(false), is_ref(false), is_ptr(false) {
}
2011-10-14 12:43:06 +08:00
2014-11-30 23:08:42 +08:00
Argument(const Qualified& t, const std::string& n) :
type(t), name(n), is_const(false), is_ref(false), is_ptr(false) {
}
bool typesEqual(const Argument& other) const {
return type == other.type
&& is_const == other.is_const && is_ref == other.is_ref
&& is_ptr == other.is_ptr;
}
2014-11-30 23:08:42 +08:00
bool operator==(const Argument& other) const {
return type == other.type && name == other.name
&& is_const == other.is_const && is_ref == other.is_ref
&& is_ptr == other.is_ptr;
}
Argument expandTemplate(const TemplateSubstitution& ts) const;
/// return MATLAB class for use in isa(x,class)
std::string matlabClass(const std::string& delim = "") const;
/// MATLAB code generation, MATLAB to C++
void matlab_unwrap(FileWriter& file, const std::string& matlabName) const;
2014-11-14 04:11:29 +08:00
2014-11-30 04:11:13 +08:00
/**
* emit checking argument to MATLAB proxy
* @param proxyFile output stream
*/
2014-11-30 04:43:48 +08:00
void proxy_check(FileWriter& proxyFile, const std::string& s) const;
2014-11-30 04:11:13 +08:00
2016-09-09 01:33:32 +08:00
/**
* emit arguments for cython pxd
* @param file output stream
*/
2016-09-12 06:14:19 +08:00
void emit_cython_pxd(FileWriter& file, const std::string& className) const;
void emit_cython_pyx(FileWriter& file) const;
void emit_cython_pyx_asParam(FileWriter& file) const;
2016-09-09 01:33:32 +08:00
2014-11-14 04:11:29 +08:00
friend std::ostream& operator<<(std::ostream& os, const Argument& arg) {
os << (arg.is_const ? "const " : "") << arg.type << (arg.is_ptr ? "*" : "")
<< (arg.is_ref ? "&" : "");
return os;
}
};
/// Argument list is just a container with Arguments
2011-12-09 04:51:15 +08:00
struct ArgumentList: public std::vector<Argument> {
/// create a comma-separated string listing all argument types (not used)
std::string types() const;
/// create a short "signature" string
std::string signature() const;
/// create a comma-separated string listing all argument names, used in m-files
std::string names() const;
2011-10-14 12:43:06 +08:00
/// Check if all arguments scalar
bool allScalar() const;
ArgumentList expandTemplate(const TemplateSubstitution& ts) const;
bool typesEqual(const ArgumentList& other) const {
for(size_t i = 0; i<size(); ++i)
if (!at(i).typesEqual(other[i])) return false;
return true;
}
// MATLAB code generation:
2011-10-14 12:43:06 +08:00
/**
* emit code to unwrap arguments
* @param file output stream
* @param start initial index for input array, set to 1 for method
*/
void matlab_unwrap(FileWriter& file, int start = 0) const; // MATLAB to C++
2012-06-27 02:52:27 +08:00
/**
* emit MATLAB prototype
* @param file output stream
* @param name of method or function
*/
void emit_prototype(FileWriter& file, const std::string& name) const;
2016-09-09 01:33:32 +08:00
/**
* emit arguments for cython pxd
* @param file output stream
*/
2016-09-12 06:14:19 +08:00
void emit_cython_pxd(FileWriter& file, const std::string& className) const;
void emit_cython_pyx(FileWriter& file) const;
void emit_cython_pyx_asParams(FileWriter& file) const;
2016-11-17 06:51:03 +08:00
void emit_cython_pyx_params_list(FileWriter& file) const;
void emit_cython_pyx_cast_params_to_python_type(FileWriter& file) const;
2016-09-09 01:33:32 +08:00
/**
2014-11-30 03:53:38 +08:00
* emit checking arguments to MATLAB proxy
* @param proxyFile output stream
2014-05-26 02:59:20 +08:00
*/
2014-11-30 04:11:13 +08:00
void proxy_check(FileWriter& proxyFile) const;
2014-11-14 04:11:29 +08:00
2014-11-30 03:53:38 +08:00
/// Output stream operator
2014-11-14 04:11:29 +08:00
friend std::ostream& operator<<(std::ostream& os,
const ArgumentList& argList) {
os << "(";
if (argList.size() > 0)
os << argList.front();
if (argList.size() > 1)
for (size_t i = 1; i < argList.size(); i++)
os << ", " << argList[i];
os << ")";
return os;
}
};
2014-12-01 03:25:26 +08:00
/* ************************************************************************* */
// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html
struct ArgumentGrammar: public classic::grammar<ArgumentGrammar> {
wrap::Argument& result_; ///< successful parse will be placed in here
2014-12-02 03:03:26 +08:00
TypeGrammar argument_type_g; ///< Type parser for Argument::type
2014-12-01 03:25:26 +08:00
/// Construct type grammar and specify where result is placed
ArgumentGrammar(wrap::Argument& result) :
result_(result), argument_type_g(result.type) {
}
/// Definition of type grammar
template<typename ScannerT>
2014-12-02 03:03:26 +08:00
struct definition: BasicRules<ScannerT> {
2014-12-01 03:25:26 +08:00
typedef classic::rule<ScannerT> Rule;
2014-12-02 03:03:26 +08:00
Rule argument_p;
2014-12-01 03:25:26 +08:00
definition(ArgumentGrammar const& self) {
using namespace classic;
// NOTE: allows for pointers to all types
// Slightly more permissive than before on basis/eigen type qualification
// Also, currently parses Point2*&, can't make it work otherwise :-(
argument_p = !str_p("const")[assign_a(self.result_.is_const, T)] //
>> self.argument_type_g //
>> !ch_p('*')[assign_a(self.result_.is_ptr, T)]
>> !ch_p('&')[assign_a(self.result_.is_ref, T)]
2014-12-02 03:03:26 +08:00
>> BasicRules<ScannerT>::name_p[assign_a(self.result_.name)];
2014-12-01 03:25:26 +08:00
}
Rule const& start() const {
return argument_p;
}
};
};
// ArgumentGrammar
/* ************************************************************************* */
// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html
struct ArgumentListGrammar: public classic::grammar<ArgumentListGrammar> {
wrap::ArgumentList& result_; ///< successful parse will be placed in here
/// Construct type grammar and specify where result is placed
ArgumentListGrammar(wrap::ArgumentList& result) :
result_(result) {
2014-12-01 03:25:26 +08:00
}
/// Definition of type grammar
template<typename ScannerT>
2014-12-02 03:03:26 +08:00
struct definition {
2014-12-01 03:25:26 +08:00
const Argument arg0; ///< used to reset arg
Argument arg; ///< temporary argument for use during parsing
ArgumentGrammar argument_g; ///< single Argument parser
2014-12-02 03:03:26 +08:00
classic::rule<ScannerT> argument_p, argumentList_p;
2014-12-01 03:25:26 +08:00
definition(ArgumentListGrammar const& self) :
argument_g(arg) {
2014-12-01 03:25:26 +08:00
using namespace classic;
2014-12-02 03:03:26 +08:00
argument_p = argument_g //
[classic::push_back_a(self.result_, arg)] //
[assign_a(arg, arg0)];
2014-12-02 03:03:26 +08:00
2014-12-01 03:25:26 +08:00
argumentList_p = '(' >> !argument_p >> *(',' >> argument_p) >> ')';
}
2014-12-02 03:03:26 +08:00
classic::rule<ScannerT> const& start() const {
2014-12-01 03:25:26 +08:00
return argumentList_p;
}
};
};
// ArgumentListGrammar
/* ************************************************************************* */
}// \namespace wrap