[C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread MM
Hi

class C {
public:
  const std::string& name() const;
  std::string& name();
private:
  std::string name_;
};

given this class C, how would I expose it to python with the class property
name?

class_("C").
  .add_property("name",   &C::name, &C::name);

or do I use 2 mem function pointers to distinguish the 2 names, and pass
those 2 pointers?

MM
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread Stefan Seefeld
On 2014-10-02 16:09, MM wrote:
> Hi
>
> class C {
> public:
>   const std::string& name() const;
>   std::string& name();
> private:
>   std::string name_;
> };
>
> given this class C, how would I expose it to python with the class
> property name?
>
> class_("C").
>   .add_property("name",   &C::name, &C::name);
>
> or do I use 2 mem function pointers to distinguish the 2 names, and
> pass those 2 pointers?

You need to disambiguate the two overloads. The cleanest way to do that
is to introduce two (local) variables of the appropriate
pointer-to-member-function types, then pass those variables to the
'add_property' call.
In addition, I believe boost.python expects a different signature for
the setter (a function taking a value-type argument), so you may have to
provide a wrapper function for that, unless you want to modify the 'C'
class in-place.

HTH,
Stefan



-- 

  ...ich hab' noch einen Koffer in Berlin...

___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread MM
yes i did that.

class C {
> public:
>   const std::string& get_name() const;
>   void set_name(const std::string&);
> private:
>   std::string name_;
> };
>


> class_("C").
>   .add_property("name",   &C::get_name, &C::set_name);


this fails to compile because of unspecified call policies about the string
refs.


The following, on the other hand, compiles.

> class C {
> public:
>   const std::string get_name() const;
>   void set_name(const std::string);
> 
> class_("C").
>   .add_property("name",   &C::get_name, &C::set_name);


Which policy do I specify? and how do I set it in add_property?

MM

On 3 October 2014 17:50, Stefan Seefeld  wrote:

> On 2014-10-02 16:09, MM wrote:
> > Hi
> >
> > class C {
> > public:
> >   const std::string& name() const;
> >   std::string& name();
> > private:
> >   std::string name_;
> > };
> >
> > given this class C, how would I expose it to python with the class
> > property name?
> >
> > class_("C").
> >   .add_property("name",   &C::name, &C::name);
> >
> > or do I use 2 mem function pointers to distinguish the 2 names, and
> > pass those 2 pointers?
>
> You need to disambiguate the two overloads. The cleanest way to do that
> is to introduce two (local) variables of the appropriate
> pointer-to-member-function types, then pass those variables to the
> 'add_property' call.
> In addition, I believe boost.python expects a different signature for
> the setter (a function taking a value-type argument), so you may have to
> provide a wrapper function for that, unless you want to modify the 'C'
> class in-place.
>
> HTH,
> Stefan
>
>
>
> --
>
>   ...ich hab' noch einen Koffer in Berlin...
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread Stefan Seefeld
On 2014-10-03 12:56, MM wrote:
> yes i did that.
>
> class C {
> public:
>   const std::string& get_name() const;
>   void set_name(const std::string&);
> private:
>   std::string name_;
> };
>
>  
>
> class_("C").
>   .add_property("name",   &C::get_name, &C::set_name);
>
>
> this fails to compile because of unspecified call policies about the
> string refs.
>
>
> The following, on the other hand, compiles.
>
> class C {
> public:
>   const std::string get_name() const;
>   void set_name(const std::string);
> 
> class_("C").
>   .add_property("name",   &C::get_name, &C::set_name);
>
>
> Which policy do I specify? and how do I set it in add_property?

Good question. The policy you want is likely pass-by-value (In Python
strings are immutable anyhow), however I have no idea how to express
that with the add_property() call.
As a quick hack I suggest adding a wrapper function that returns the
result by-value:

  std::string get_name(C &c) { return c.get_name();}

and use that. That's neither elegant nor efficient (if you call it a
lot), but it may unblock you until you find a real fix.

Stefan

-- 

  ...ich hab' noch einen Koffer in Berlin...

___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread Jim Bosch
On Fri, Oct 3, 2014 at 1:15 PM, Stefan Seefeld  wrote:

> On 2014-10-03 12:56, MM wrote:
> > yes i did that.
> >
> > class C {
> > public:
> >   const std::string& get_name() const;
> >   void set_name(const std::string&);
> > private:
> >   std::string name_;
> > };
> >
> >
> >
> > class_("C").
> >   .add_property("name",   &C::get_name, &C::set_name);
> >
> >
> > this fails to compile because of unspecified call policies about the
> > string refs.
> >
> >
> > The following, on the other hand, compiles.
> >
> > class C {
> > public:
> >   const std::string get_name() const;
> >   void set_name(const std::string);
> > 
> > class_("C").
> >   .add_property("name",   &C::get_name, &C::set_name);
> >
> >
> > Which policy do I specify? and how do I set it in add_property?
>
> Good question. The policy you want is likely pass-by-value (In Python
> strings are immutable anyhow), however I have no idea how to express
> that with the add_property() call.
> As a quick hack I suggest adding a wrapper function that returns the
> result by-value:
>
>   std::string get_name(C &c) { return c.get_name();}
>
> and use that. That's neither elegant nor efficient (if you call it a
> lot), but it may unblock you until you find a real fix.
>
>
To use a call policy here, I *think* you'd pass
return_value_policy() as the fourth argument to
add_property, but it may be some slight modification of that.  In any case,
I suspect that's no more efficient than Stefan's solution in this case.


Jim
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost python class member getter/setter same name different only by constness

2014-10-03 Thread MM
On 3 October 2014 18:23, Jim Bosch  wrote:

> On Fri, Oct 3, 2014 at 1:15 PM, Stefan Seefeld 
> wrote:
>
>> On 2014-10-03 12:56, MM wrote:
>> > yes i did that.
>> >
>> > class C {
>> > public:
>> >   const std::string& get_name() const;
>> >   void set_name(const std::string&);
>> > private:
>> >   std::string name_;
>> > };
>> >
>> >
>> >
>> > class_("C").
>> >   .add_property("name",   &C::get_name, &C::set_name);
>> >
>> >
>> > this fails to compile because of unspecified call policies about the
>> > string refs.
>> >
>> >
>> > The following, on the other hand, compiles.
>> >
>> > class C {
>> > public:
>> >   const std::string get_name() const;
>> >   void set_name(const std::string);
>> > 
>> > class_("C").
>> >   .add_property("name",   &C::get_name, &C::set_name);
>> >
>> >
>> > Which policy do I specify? and how do I set it in add_property?
>>
>> Good question. The policy you want is likely pass-by-value (In Python
>> strings are immutable anyhow), however I have no idea how to express
>> that with the add_property() call.
>> As a quick hack I suggest adding a wrapper function that returns the
>> result by-value:
>>
>>   std::string get_name(C &c) { return c.get_name();}
>>
>> and use that. That's neither elegant nor efficient (if you call it a
>> lot), but it may unblock you until you find a real fix.
>>
>>
> To use a call policy here, I *think* you'd pass
> return_value_policy() as the fourth argument to
> add_property, but it may be some slight modification of that.  In any case,
> I suspect that's no more efficient than Stefan's solution in this case.
>
>
> Jim
>
> Not quite. get_property's 4th argument is just a docstring.
It seems only def has a policy argument.

Actually this is a problem because the next 2 properties of the class are
big vectors.

I am gonna go with just defs of getter/setter instead as add_property
doesn't help (i looked at class.hpp inside boost.python)

MM
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig