Accessing C++ typedef in Python using SWIG
I have a C++ API I'm trying to wrap in Python. I want to call a wrapped C++ function myfunc taking as an argument the following C++ typedef
/* my_header.h */
namespace my_namespace {
typedef std::vector<Foo> Bar
}
where Foo is a C++ class. I managed to wrap the function and the underlying class Foo, but I don't know how to create the vector of Foo. I included the .h file in my SWIG .i file as follows
/* my_interface.i */
%{
#include "my_header.h"
typedef my_namespace::Bar Bar;
%}
%include "my_header.h"
I also tried wrapping the std::vector template in SWIG, as follows
%include std_vector.i
namespace std {
%template(vector_foo) vector<Foo>;
}
This works, and I can import vector_foo in Python. However, when I send a vector_foo as an argument to the function mentioned above, I get a TypeError. Neither am I able to populate vector_foo with Foo.
In Python I do the following
>>> a = mymodule.vector_foo()
>>> a
<Swig Object of type 'std::vector <Foo, std::allocator< Foo > > *'
>>> mymodule.myfunc(a, 'string')
TypeError: in method 'myfunc', argument 1 of type 'my_namespace::Bar &'
Either if I can make my own implementation of vector of Foo work, or somehow access the C++ typedef directly. I'm calling SWIG and compiling using Python Distutils.
Thanks for any help!
I solved it.
Seems like the problem was I had to tell SWIG about the typedef already present in the %{ %} braces in the interface file, ie
/* my_interface.i */
%{
#include "my_header.h"
typedef my_namespace::Bar Bar;
%}
typedef my_namespace::Bar Bar;
%include "my_header.h"
Although I'm not 100% that this was the mistake. In any case, I now have an interface file like the above one and wrapping works.
链接地址: http://www.djcxy.com/p/48332.html上一篇: C ++谷歌协议缓冲区打开http套接字