Python prototype
parent
e07402a58a
commit
6c24fc2aca
|
@ -583,4 +583,16 @@ string Class::getSerializationExport() const {
|
|||
return "BOOST_CLASS_EXPORT_GUID(" + qualifiedName("::") + ", \""
|
||||
+ qualifiedName() + "\");";
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void Class::python_wrapper(FileWriter& wrapperFile) const {
|
||||
wrapperFile.oss << "class_<" << name << ">(\"" << name << "\")\n";
|
||||
constructor.python_wrapper(wrapperFile, name);
|
||||
BOOST_FOREACH(const StaticMethod& m, static_methods | boost::adaptors::map_values)
|
||||
m.python_wrapper(wrapperFile, name);
|
||||
BOOST_FOREACH(const Method& m, methods | boost::adaptors::map_values)
|
||||
m.python_wrapper(wrapperFile, name);
|
||||
wrapperFile.oss << ";\n\n";
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -111,6 +111,9 @@ public:
|
|||
void deserialization_fragments(FileWriter& proxyFile, FileWriter& wrapperFile,
|
||||
Str wrapperName, std::vector<std::string>& functionNames) const;
|
||||
|
||||
// emit python wrapper
|
||||
void python_wrapper(FileWriter& wrapperFile) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Class& cls) {
|
||||
os << "class " << cls.name << "{\n";
|
||||
os << cls.constructor << ";\n";
|
||||
|
|
|
@ -29,25 +29,28 @@
|
|||
using namespace std;
|
||||
using namespace wrap;
|
||||
|
||||
|
||||
/* ************************************************************************* */
|
||||
string Constructor::matlab_wrapper_name(const string& className) const {
|
||||
string Constructor::matlab_wrapper_name(Str className) const {
|
||||
string str = "new_" + className;
|
||||
return str;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void Constructor::proxy_fragment(FileWriter& file, const std::string& wrapperName,
|
||||
bool hasParent, const int id, const ArgumentList args) const {
|
||||
void Constructor::proxy_fragment(FileWriter& file,
|
||||
const std::string& wrapperName, bool hasParent, const int id,
|
||||
const ArgumentList args) const {
|
||||
size_t nrArgs = args.size();
|
||||
// check for number of arguments...
|
||||
file.oss << " elseif nargin == " << nrArgs;
|
||||
if (nrArgs>0) file.oss << " && ";
|
||||
if (nrArgs > 0)
|
||||
file.oss << " && ";
|
||||
// ...and their types
|
||||
bool first = true;
|
||||
for (size_t i = 0; i < nrArgs; i++) {
|
||||
if (!first) file.oss << " && ";
|
||||
file.oss << "isa(varargin{" << i+1 << "},'" << args[i].matlabClass(".") << "')";
|
||||
if (!first)
|
||||
file.oss << " && ";
|
||||
file.oss << "isa(varargin{" << i + 1 << "},'" << args[i].matlabClass(".")
|
||||
<< "')";
|
||||
first = false;
|
||||
}
|
||||
// emit code for calling constructor
|
||||
|
@ -65,16 +68,16 @@ void Constructor::proxy_fragment(FileWriter& file, const std::string& wrapperNam
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
string Constructor::wrapper_fragment(FileWriter& file,
|
||||
const string& cppClassName,
|
||||
const string& matlabUniqueName,
|
||||
const string& cppBaseClassName,
|
||||
int id,
|
||||
string Constructor::wrapper_fragment(FileWriter& file, Str cppClassName,
|
||||
Str matlabUniqueName, Str cppBaseClassName, int id,
|
||||
const ArgumentList& al) const {
|
||||
|
||||
const string wrapFunctionName = matlabUniqueName + "_constructor_" + boost::lexical_cast<string>(id);
|
||||
const string wrapFunctionName = matlabUniqueName + "_constructor_"
|
||||
+ boost::lexical_cast<string>(id);
|
||||
|
||||
file.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])" << endl;
|
||||
file.oss << "void " << wrapFunctionName
|
||||
<< "(int nargout, mxArray *out[], int nargin, const mxArray *in[])"
|
||||
<< endl;
|
||||
file.oss << "{\n";
|
||||
file.oss << " mexAtExit(&_deleteAllObjects);\n";
|
||||
//Typedef boost::shared_ptr
|
||||
|
@ -84,20 +87,27 @@ string Constructor::wrapper_fragment(FileWriter& file,
|
|||
//Check to see if there will be any arguments and remove {} for consiseness
|
||||
if (al.size() > 0)
|
||||
al.matlab_unwrap(file); // unwrap arguments
|
||||
file.oss << " Shared *self = new Shared(new " << cppClassName << "(" << al.names() << "));" << endl;
|
||||
file.oss << " Shared *self = new Shared(new " << cppClassName << "("
|
||||
<< al.names() << "));" << endl;
|
||||
file.oss << " collector_" << matlabUniqueName << ".insert(self);\n";
|
||||
|
||||
if (verbose_)
|
||||
file.oss << " std::cout << \"constructed \" << self << std::endl;" << endl;
|
||||
file.oss << " out[0] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);" << endl;
|
||||
file.oss << " *reinterpret_cast<Shared**> (mxGetData(out[0])) = self;" << endl;
|
||||
file.oss
|
||||
<< " out[0] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);"
|
||||
<< endl;
|
||||
file.oss << " *reinterpret_cast<Shared**> (mxGetData(out[0])) = self;"
|
||||
<< endl;
|
||||
|
||||
// If we have a base class, return the base class pointer (MATLAB will call the base class collectorInsertAndMakeBase to add this to the collector and recurse the heirarchy)
|
||||
if (!cppBaseClassName.empty()) {
|
||||
file.oss << "\n";
|
||||
file.oss << " typedef boost::shared_ptr<" << cppBaseClassName << "> SharedBase;\n";
|
||||
file.oss << " out[1] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);\n";
|
||||
file.oss << " *reinterpret_cast<SharedBase**>(mxGetData(out[1])) = new SharedBase(*self);\n";
|
||||
file.oss << " typedef boost::shared_ptr<" << cppBaseClassName
|
||||
<< "> SharedBase;\n";
|
||||
file.oss
|
||||
<< " out[1] = mxCreateNumericMatrix(1, 1, mxUINT32OR64_CLASS, mxREAL);\n";
|
||||
file.oss
|
||||
<< " *reinterpret_cast<SharedBase**>(mxGetData(out[1])) = new SharedBase(*self);\n";
|
||||
}
|
||||
|
||||
file.oss << "}" << endl;
|
||||
|
@ -106,3 +116,9 @@ string Constructor::wrapper_fragment(FileWriter& file,
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void Constructor::python_wrapper(FileWriter& wrapperFile, Str className) const {
|
||||
wrapperFile.oss << " .def(\"" << name_ << "\", &" << className << "::" << name_
|
||||
<< ");\n";
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -28,6 +28,8 @@ namespace wrap {
|
|||
// Constructor class
|
||||
struct Constructor: public OverloadedFunction {
|
||||
|
||||
typedef const std::string& Str;
|
||||
|
||||
/// Constructor creates an empty class
|
||||
Constructor(bool verbose = false) {
|
||||
verbose_ = verbose;
|
||||
|
@ -45,7 +47,7 @@ struct Constructor: public OverloadedFunction {
|
|||
// classFile is class proxy file, e.g., ../matlab/@Point2/Point2.m
|
||||
|
||||
/// wrapper name
|
||||
std::string matlab_wrapper_name(const std::string& className) const;
|
||||
std::string matlab_wrapper_name(Str className) const;
|
||||
|
||||
void comment_fragment(FileWriter& proxyFile) const {
|
||||
if (nrOverloads() > 0)
|
||||
|
@ -61,19 +63,21 @@ struct Constructor: public OverloadedFunction {
|
|||
* Create fragment to select constructor in proxy class, e.g.,
|
||||
* if nargin == 2, obj.self = new_Pose3_RP(varargin{1},varargin{2}); end
|
||||
*/
|
||||
void proxy_fragment(FileWriter& file, const std::string& wrapperName,
|
||||
bool hasParent, const int id, const ArgumentList args) const;
|
||||
void proxy_fragment(FileWriter& file, Str wrapperName, bool hasParent,
|
||||
const int id, const ArgumentList args) const;
|
||||
|
||||
/// cpp wrapper
|
||||
std::string wrapper_fragment(FileWriter& file,
|
||||
const std::string& cppClassName, const std::string& matlabUniqueName,
|
||||
const std::string& cppBaseClassName, int id,
|
||||
std::string wrapper_fragment(FileWriter& file, Str cppClassName,
|
||||
Str matlabUniqueName, Str cppBaseClassName, int id,
|
||||
const ArgumentList& al) const;
|
||||
|
||||
/// constructor function
|
||||
void generate_construct(FileWriter& file, const std::string& cppClassName,
|
||||
void generate_construct(FileWriter& file, Str cppClassName,
|
||||
std::vector<ArgumentList>& args_list) const;
|
||||
|
||||
// emit python wrapper
|
||||
void python_wrapper(FileWriter& wrapperFile, Str className) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Constructor& m) {
|
||||
for (size_t i = 0; i < m.nrOverloads(); i++)
|
||||
os << m.name_ << m.argLists_[i];
|
||||
|
|
|
@ -127,6 +127,11 @@ void GlobalFunction::generateSingleFunction(const string& toolboxPath,
|
|||
mfunctionFile.emit(true);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void GlobalFunction::python_wrapper(FileWriter& wrapperFile) const {
|
||||
wrapperFile.oss << "def(\"" << name_ << "\", " << name_ << ");\n";
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
||||
} // \namespace wrap
|
||||
|
|
|
@ -35,6 +35,9 @@ struct GlobalFunction: public FullyOverloadedFunction {
|
|||
const std::string& wrapperName, const TypeAttributesTable& typeAttributes,
|
||||
FileWriter& file, std::vector<std::string>& functionNames) const;
|
||||
|
||||
// emit python wrapper
|
||||
void python_wrapper(FileWriter& wrapperFile) const;
|
||||
|
||||
private:
|
||||
|
||||
// Creates a single global function - all in same namespace
|
||||
|
|
|
@ -650,3 +650,31 @@ void Module::WriteRTTIRegistry(FileWriter& wrapperFile, const std::string& modul
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void Module::python_wrapper(const string& toolboxPath) const {
|
||||
|
||||
fs::create_directories(toolboxPath);
|
||||
|
||||
// create the unified .cpp switch file
|
||||
const string wrapperName = name + "_python";
|
||||
string wrapperFileName = toolboxPath + "/" + wrapperName + ".cpp";
|
||||
FileWriter wrapperFile(wrapperFileName, verbose, "//");
|
||||
wrapperFile.oss << "#include <boost/python.hpp>\n\n";
|
||||
wrapperFile.oss << "using namespace boost::python;\n";
|
||||
wrapperFile.oss << "BOOST_PYTHON_MODULE(" + name + ")\n";
|
||||
wrapperFile.oss << "{\n";
|
||||
|
||||
// write out classes
|
||||
BOOST_FOREACH(const Class& cls, expandedClasses)
|
||||
cls.python_wrapper(wrapperFile);
|
||||
|
||||
// write out global functions
|
||||
BOOST_FOREACH(const GlobalFunctions::value_type& p, global_functions)
|
||||
p.second.python_wrapper(wrapperFile);
|
||||
|
||||
// finish wrapper file
|
||||
wrapperFile.oss << "}\n";
|
||||
|
||||
wrapperFile.emit(true);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -70,6 +70,9 @@ struct Module {
|
|||
void finish_wrapper(FileWriter& file,
|
||||
const std::vector<std::string>& functionNames) const;
|
||||
|
||||
/// Python code generation:
|
||||
void python_wrapper(const std::string& path) const;
|
||||
|
||||
private:
|
||||
static std::vector<Class> ExpandTypedefInstantiations(
|
||||
const std::vector<Class>& classes,
|
||||
|
|
|
@ -165,3 +165,10 @@ string StaticMethod::wrapper_call(FileWriter& wrapperFile, Str cppClassName,
|
|||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void StaticMethod::python_wrapper(FileWriter& wrapperFile,
|
||||
Str className) const {
|
||||
wrapperFile.oss << " .def(\"" << name_ << "\", &" << className << "::" << name_
|
||||
<< ");\n";
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -52,6 +52,9 @@ struct StaticMethod: public FullyOverloadedFunction {
|
|||
Str wrapperName, const TypeAttributesTable& typeAttributes,
|
||||
std::vector<std::string>& functionNames) const;
|
||||
|
||||
// emit python wrapper
|
||||
void python_wrapper(FileWriter& wrapperFile, Str className) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const StaticMethod& m) {
|
||||
for (size_t i = 0; i < m.nrOverloads(); i++)
|
||||
os << "static " << m.returnVals_[i] << " " << m.name_ << m.argLists_[i];
|
||||
|
|
|
@ -460,6 +460,25 @@ TEST( wrap, matlab_code_geometry ) {
|
|||
EXPECT(files_equal(epath + "overloadedGlobalFunction.m" , apath + "overloadedGlobalFunction.m" ));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
TEST( wrap, python_code_geometry ) {
|
||||
// Parse into class object
|
||||
string header_path = topdir + "/wrap/tests";
|
||||
Module module(header_path,"geometry",enable_verbose);
|
||||
string path = topdir + "/wrap";
|
||||
|
||||
// clean out previous generated code
|
||||
fs::remove_all("actual-python");
|
||||
|
||||
// emit MATLAB code
|
||||
// make_geometry will not compile, use make testwrap to generate real make
|
||||
module.python_wrapper("actual-python");
|
||||
string epath = path + "/tests/expected-python/";
|
||||
string apath = "actual-python/";
|
||||
|
||||
EXPECT(files_equal(epath + "geometry_python.cpp", apath + "geometry_python.cpp" ));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
|
||||
/* ************************************************************************* */
|
||||
|
|
Loading…
Reference in New Issue