Greetings,
I'm new to Boost.Python. I have a C++ library (written and shipped by
another party) that I want to wrap into Python modules so that I can use
several functions in Python. One function takes an argument that is a C++
template vector. How can I export this function to a python function?
In a nutshell, here is what I'm doing now but without luck. I wrap the
template vector into a Python class using
Boost.Python's vector_indexing_suite. Then in the Python script, I first
declare this Python class and then pass it to the function. But I got no
luck and the following error message,
"
Boost.Python.ArgumentError: Python argument types in
canupo.read_msc_header(MSCFile, FloatVec, int)
did not match C++ signature:
read_msc_header(MSCFile {lvalue}, std::vector<float,
std::allocator<float> > {lvalue}, int {lvalue})
"
More specifically, here are the C++ code and Python script I have.
In the C++ library code file "helper.hpp", it has a struct MSCFile and a
function read_msc_header.
struct MSCFile
{
// definition of MSCFile
};
int read_msc_header(MSCFile& mscfile, std::vector<FloatType>& scales, int&
ptn)
{
// implementation of read_msc_header
}
The "FloatType" is just a alias for float.
In the C++ wrapper code file "wrapper.cpp"
#include "helpers.hpp"
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
BOOST_PYTHON_MODULE(canupo)
{
using namespace boost::python;
class_<std::vector<FloatType> >("FloatVec")
.def(vector_indexing_suite<std::vector<FloatType> >())
;
class_<MSCFile>("MSCFile", init<const char*>())
;
def("read_msc_header", read_msc_header);
}
In the python script, I tried to use the function as following but failed.
import canupo
mscfile = canupo.MSCFile("msc_file_name")
scales = canupo.FloatVec()
ptnparams = 0
print canupo.read_msc_header(mscfile, scales, ptnparams)
And the error goes like this:
Boost.Python.ArgumentError: Python argument types in
canupo.read_msc_header(MSCFile, FloatVec, int)
did not match C++ signature:
read_msc_header(MSCFile {lvalue}, std::vector<float,
std::allocator<float> > {lvalue}, int {lvalue})
I googled around but didn't find a good solution or a solution that I can
understand well. Most requires rewrite the function with Boost.Python. But
I don't want to change the C++ library code itself. I think there may be a
good way to expose such a function to Python. Thanks a lot!
Zhan.
--
Zhan Li, 李展;
Earth & Environment, Boston University, USA;
2008 - 2011, in Chinese Academy of Sciences, China;
2004 - 2008, in Nanjing University, China;
_______________________________________________
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig