gtsam/wrap/ReturnValue.cpp

108 lines
3.6 KiB
C++
Raw Normal View History

/**
* @file ReturnValue.cpp
* @date Dec 1, 2011
* @author Alex Cunningham
2012-06-27 02:52:27 +08:00
* @author Andrew Melim
2012-07-13 06:28:28 +08:00
* @author Richard Roberts
*/
#include "ReturnValue.h"
#include "utilities.h"
#include <iostream>
using namespace std;
using namespace wrap;
2014-11-13 07:39:15 +08:00
/* ************************************************************************* */
ReturnValue ReturnValue::expandTemplate(const TemplateSubstitution& ts) const {
2014-11-13 07:39:15 +08:00
ReturnValue instRetVal = *this;
2014-11-30 07:13:29 +08:00
instRetVal.type1 = ts.tryToSubstitite(type1);
if (isPair) instRetVal.type2 = ts.tryToSubstitite(type2);
2014-11-13 07:39:15 +08:00
return instRetVal;
}
/* ************************************************************************* */
2014-11-12 21:37:08 +08:00
string ReturnValue::return_type(bool add_ptr) const {
if (isPair)
return "pair< " + type1.str(add_ptr) + ", " + type2.str(add_ptr) + " >";
else
return type1.str(add_ptr);
}
2014-11-12 20:31:46 +08:00
/* ************************************************************************* */
2014-11-12 21:37:08 +08:00
string ReturnValue::matlab_returnType() const {
return isPair ? "[first,second]" : "result";
2014-11-12 20:31:46 +08:00
}
/* ************************************************************************* */
void ReturnValue::wrap_result(const string& result, FileWriter& wrapperFile,
const TypeAttributesTable& typeAttributes) const {
if (isPair) {
// For a pair, store the returned pair so we do not evaluate the function
// twice
wrapperFile.oss << " " << return_type(true) << " pairResult = " << result
<< ";\n";
2014-11-13 07:39:15 +08:00
type1.wrap_result(" out[0]", "pairResult.first", wrapperFile,
typeAttributes);
2014-11-13 07:39:15 +08:00
type2.wrap_result(" out[1]", "pairResult.second", wrapperFile,
typeAttributes);
} else { // Not a pair
type1.wrap_result(" out[0]", result, wrapperFile, typeAttributes);
}
}
/* ************************************************************************* */
void ReturnValue::wrapTypeUnwrap(FileWriter& wrapperFile) const {
type1.wrapTypeUnwrap(wrapperFile);
if (isPair) type2.wrapTypeUnwrap(wrapperFile);
2012-07-24 02:24:39 +08:00
}
2014-11-12 21:37:08 +08:00
2012-07-24 02:24:39 +08:00
/* ************************************************************************* */
void ReturnValue::emit_matlab(FileWriter& proxyFile) const {
string output;
if (isPair)
proxyFile.oss << "[ varargout{1} varargout{2} ] = ";
2014-11-12 21:37:08 +08:00
else if (type1.category != ReturnType::VOID)
proxyFile.oss << "varargout{1} = ";
}
2014-11-12 21:37:08 +08:00
/* ************************************************************************* */
void ReturnValue::emit_cython_pxd(
FileWriter& file, const std::string& className,
const std::vector<std::string>& templateArgs) const {
2016-09-09 19:10:04 +08:00
if (isPair) {
file.oss << "pair[";
type1.emit_cython_pxd(file, className, templateArgs);
2016-09-09 19:10:04 +08:00
file.oss << ",";
type2.emit_cython_pxd(file, className, templateArgs);
2016-09-09 19:16:29 +08:00
file.oss << "] ";
} else {
type1.emit_cython_pxd(file, className, templateArgs);
file.oss << " ";
2016-09-09 19:10:04 +08:00
}
}
/* ************************************************************************* */
std::string ReturnValue::pyx_returnType() const {
if (isVoid()) return "";
if (isPair) {
return "pair [" + type1.pyx_returnType(false) + "," +
type2.pyx_returnType(false) + "]";
2016-09-12 04:40:09 +08:00
} else {
return type1.pyx_returnType(true);
2016-09-12 04:40:09 +08:00
}
}
/* ************************************************************************* */
std::string ReturnValue::pyx_casting(const std::string& var) const {
if (isVoid()) return "";
2016-09-12 04:40:09 +08:00
if (isPair) {
return "(" + type1.pyx_casting(var + ".first", false) + "," +
type2.pyx_casting(var + ".second", false) + ")";
} else {
return type1.pyx_casting(var);
}
}
/* ************************************************************************* */