Sign in to follow this  
Geoff the Medio

Convert Member Function STL Containers / Boost Python

Recommended Posts

I have a class with a member function that returns an std::set...
class Example
{
    //...
    const std::set<int> Values();
    int OtherFunction();
    //...
}
I want to expose this function using Boost Python, however there are no premade indexing suites to expose a set as there are for std::vector or std::map. So, I thought I might convert the output of this function to a std::vector<int> when returning it, and save myself the trouble of trying to properly expose set::set to Python. However, I'm having a hard time figuring out a good way to convert the output of Example::Values. I don't want to add anything the class Example, and I'd like the reworked version of Values that returns std::vector<int> to be accessible as a member function of the Example class that is exposed to Python. So, it would seem I need to be able to get an Example class member function pointer, IntVectExampleValuesPtr, that points to a free function, IntVectExampleValues() that calls this->Example::Values and converts the results from std::set<int> to std::vector<int>. This would allow me to expose Example like so:
class_<Example, noncopyable>("Example", no_init)
    .def("Values", IntVectExampleValuesPtr, return_value_policy<return_by_value>());
    .def("OtherFunction", &Example::OtherFunction)
;
Altneratively, I could expose the free function directly, instead of using the pointer... But in either case, I need some way to "fake" an Example class member function, so as to be able to get the this pointer of the Example object in Python. This doesn't seem like it's going to work. I also haven't been able to figure out anything involving functors, which could seemingly circumvent the need to fake an Example class member function by storing the this pointer, but how can I get the this pointer with which to initialize the functor? Perhaps a specialized Python-only constructor could help? Any suggestions? Is there some other better way to do this? Thanks.

Share this post


Link to post
Share on other sites
What set operations do you want to expose? If the subset of the API is small, it may be simpler to wrap the functions yourself than try to force it through another container.

Share this post


Link to post
Share on other sites
Quote:
Original post by BrianL
What set operations do you want to expose? If the subset of the API is small, it may be simpler to wrap the functions yourself than try to force it through another container.

If I was going to expose std::set directly, I'd want find, size, begin, end, const_iterator (or Python equilvalents). I can probably do without the ability to modify the exposed set (via insert or erase) which likely simplies things.

However, the Boost Python indexing suite notes state that "Emulating Python containers in C++ ... using Boost Python is non trivial", and I don't want to have std::vector and std::map acting like normal Python containers, but then have std::set functioning somewhat differently as a C++ container exposed directly to Python but not behaving like it belongs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this