![]() |
Wrappers |
![]() |
![]() |
![]() |
Suppose you have this function:
std::vector<std::string> names();
But you don't want to export a vector<string>, you want this function to return a python list of strings. Boost.Python has an excellent support for that:
list names_wrapper()
{
list result;
vector<string> v = names();
// put each string in the vector in the list
return result;
}
BOOST_PYTHON_MODULE(test)
{
def("names", &names_wrapper);
}
Nice heh? Pyste supports this mechanism too. You declare the names_wrapper function in a header, like "test_wrappers.h", and in the interface file:
Include("test_wrappers.h")
names = Function("names", "test.h")
set_wrapper(names, "names_wrapper")
You can optionally declare the function in the interface file itself:
names_wrapper = Wrapper("names_wrapper",
"""
list names_wrapper()
{
// call name() and convert the vector to a list...
}
""")
names = Function("names", "test.h")
set_wrapper(names, names_wrapper)
The same mechanism can be done with methods too. Just remember that the first parameter of wrappers for methods is a pointer to the class, like in Boost.Python:
struct C
{
std::vector<std::string> names();
}
list names_wrapper(C* c)
{
// same as before, calling c->names() and converting result to a list
}
And then in the interface file:
C = Class("C", "test.h")
set_wrapper(C.names, "names_wrapper")
![]() |
![]() |
![]() |
Copyright © 2003 Bruno da Silva de Oliveira
Copyright © 2002-2003 Joel de Guzman
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.