[C++-sig] Help wrapping an unconventional setter

Stefan Seefeld seefeld at sympatico.ca
Thu Oct 23 18:36:08 CEST 2008


Craig Bosma wrote:
> Hi,
>
> I'm pretty new to Boost.Python, and am trying to wrap a simple class 
> similar to:
>
> class Foo {
> public:
>     float value() const { return value_; }
>     float& value() { return value_; }
> private:
>     float value_;
> };
>
> I have no problem wrapping value() as a read-only property, like so:
>
> class_<Foo>("Foo")
>   .add_property("value", (float (Foo::*)(void) const)&Foo::value)
>   ;
>
> But I'm having a difficult time determining how to wrap the 
> unconventional 'setter' method for read-write property access; as you 
> can see it returns a non-const reference to be set rather than taking 
> the new value as an argument. Any suggestions?

Use a thin wrapper function, such as:

void Foo_setter(Foo &foo, float v) { foo.value() = v;}
...

float (Foo::*getter)() const = &Foo::value; // to disambiguate
class_<Foo> foo("Foo");
foo.add_property("value", getter, Foo_setter);

HTH,
       Stefan

...

class_<Foo>("Foo")
  .add_property("value", Foo::value, setter);

should


>
> Thanks,
>
> Craig
> ------------------------------------------------------------------------
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig


-- 

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



More information about the Cplusplus-sig mailing list