[C++-sig] wrapping generic getter/setter functions in boost.python

vikas chauhan presentisgood at gmail.com
Sat Jun 16 17:57:56 CEST 2012


Hi all,
I am pretty new to boost.python and I have been getting some problems
wrapping generic getter/setter functions.
Say for eg. we have class like this.

class X{
    int a[10];
    public:
    int geta(int index) throw(some_exception) {
        if(index >= 10 || index <0)
            throw some_exception;
        return a[index];
    }
    void seta(int index, int val) throw(some_exception) {
        if(index >= 10 || index < 0)
            throw some_exception;
        a[index] = value;
    }
};

I want to add attributes like a0, a1, a2,.. a9 in my python class, wherein
a0 corresponds to a[0] , a1 to a[1] and likewise.
In the boost.python docs ( exposing Class properties ), getter/settter
functions are documented for single value only.
Is it possible to do what I want ( i.e add separate attributes using  the
single geta()/seta() functions ) or I need to write some kind of wrapper
functions (each for accessing each a[i]) ?

i.e, what I want to do is something like this.

BOOST_PYTHON_MODULE(mymod)
{
    using namespace boost::python;

    class_<X>("X")
        .add_property("a0",    &X::geta, &X::seta)
        .add_property("a1",    &X::geta, &X::seta)
        ..
        ..
        ;
}

and passing the arguments to geta ( i.e 0, 1,.. etc ) in someway.

So, basically what I want is , to avoid unnecessary code bloat.

regards,
vikas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120616/53a515d4/attachment.html>


More information about the Cplusplus-sig mailing list