On 17 June 2012 15:45, <cplusplus-sig-requ...@python.org> wrote:

> Send Cplusplus-sig mailing list submissions to
>        cplusplus-sig@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.python.org/mailman/listinfo/cplusplus-sig
> or, via email, send a message with subject or body 'help' to
>        cplusplus-sig-requ...@python.org
>
> You can reach the person managing the list at
>        cplusplus-sig-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Cplusplus-sig digest..."
>
>
> Today's Topics:
>
>   1. wrapping generic getter/setter functions in boost.python
>      (vikas chauhan)
>   2. Re: wrapping generic getter/setter functions in   boost.python
>      (Jonas Wielicki)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 16 Jun 2012 21:42:56 +0545
> From: vikas chauhan <presentisg...@gmail.com>
> To: cplusplus-sig@python.org
> Subject: [C++-sig] wrapping generic getter/setter functions in
>        boost.python
> Message-ID:
>        <cag2q8sbd4cgeh9gx9mnj6xyaretp5vql_wutfo6aiqnzcpr...@mail.gmail.com
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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-0001.html
> >
>
> ------------------------------
>
> Message: 2
> Date: Sat, 16 Jun 2012 18:14:34 +0200
> From: Jonas Wielicki <j.wieli...@sotecware.net>
> To: Development of Python/C++ integration <cplusplus-sig@python.org>
> Subject: Re: [C++-sig] wrapping generic getter/setter functions in
>        boost.python
> Message-ID: <4fdcb0ea.3000...@sotecware.net>
> Content-Type: text/plain; charset=UTF-8
>
> On 16.06.2012 17:57, vikas chauhan wrote:
> > 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 guess the simplest way to do that is to template the geta and seta
> function similar to this one:
>
> class X {
>  template <int index>
>  int geta() throw(foo) {
>    return a[index];
>  }
>  // ...
> }
>
> you then somehow need to force instanciation I guess, maybe a plain
>
> // ...
>  .add_property("a0", &X::geta<0>)
>
> does the trick, but I doubt it. This just as a suggestion for a
> direction to do research in, I did not try it, neither I would bet that
> it works ;)
>
> cheers,
> Jonas
>
>
> ------------------------------
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
> End of Cplusplus-sig Digest, Vol 45, Issue 15
> *********************************************
>

Hi Jonas,
thanks a lot for your suggestions. It actually worked !!

cheers,
vikas
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to