[C++-sig] wrapping reference members
Hi all,
I'm a beginner of c++ and boost::python, so this may be a stupid
question. When I have the following class:
class Foo{
public:
float &a;
};
Is there some way to expose "a" in python? I would like to use it like a
normal python attribute, e.g. set it's value with "Foo().a = 3.1".
Thanks for any help,
Sebastian
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] wrapping reference members
I would think that .def_readwrite would do what you need. If it doesn't look at using .def_property and get/set pair. Caveat: making sure that 'a' stays a valid reference is your problem. Python will happily crash horribly (if you are lucky) in response to bogus pointers or references. -Original Message- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@py thon.org] On Behalf Of Sebastian Kraemer Sent: Wednesday, July 22, 2009 9:16 AM To: [email protected] Subject: [C++-sig] wrapping reference members Hi all, I'm a beginner of c++ and boost::python, so this may be a stupid question. When I have the following class: class Foo{ public: float &a; }; Is there some way to expose "a" in python? I would like to use it like a normal python attribute, e.g. set it's value with "Foo().a = 3.1". Thanks for any help, Sebastian ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] wrapping reference members
I would think that .def_readwrite would do what you need. If it doesn't
look at using .def_property and get/set pair. Caveat: making sure that
'a' stays a valid reference is your problem. Python will happily crash
horribly (if you are lucky) in response to bogus pointers or references.
Thanks for your fast answer! I tried it with following code:
class_("Foo", init<>())
.def_readwrite("a", &Foo::a)
;
But it raises the error:
error: cannot create pointer to reference member 'Foo::a'
And it seems it's really not possible to create a pointer to a reference in c++ ;) So what
else do I have to write instead of "&Foo::a"?
Cheers,
Sebastian
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] wrapping reference members
Try something like this:
float get_a(const Foo& self) {return self.a}
void set_a(Foo& self, float a_new) {self.a = a_new }
class_("Foo", init<>())
.def_property("a", &get_a, &set_a)
;
-Original Message-
From:
cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@python.org
[mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@py
thon.org] On Behalf Of Sebastian Kraemer
Sent: Wednesday, July 22, 2009 9:57 AM
To: [email protected]
Subject: Re: [C++-sig] wrapping reference members
> I would think that .def_readwrite would do what you need. If it
doesn't
> look at using .def_property and get/set pair. Caveat: making sure that
> 'a' stays a valid reference is your problem. Python will happily crash
> horribly (if you are lucky) in response to bogus pointers or
references.
Thanks for your fast answer! I tried it with following code:
class_("Foo", init<>())
.def_readwrite("a", &Foo::a)
;
But it raises the error:
error: cannot create pointer to reference member 'Foo::a'
And it seems it's really not possible to create a pointer to a reference
in c++ ;) So what else do I have to write instead of "&Foo::a"?
Cheers,
Sebastian
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] wrapping reference members
You may need to create a getter and a setter in your wrapper, and then you can use the "make_function" function to change the CallPolicy when you use create a property. I think you want your call policy to be return_internal_reference<> and that will handle the lifetime correctly. Here is a reference link that that technique: http://wiki.python.org/moin/boost.python/HowTo#getterandsettermethodsasaproperty There may be an easier solution to your problem, but I know that the technique above will work. Hope this helps, Bill -Original Message- From: [email protected] [mailto:[email protected]] On Behalf Of Sebastian Kraemer Sent: Wednesday, July 22, 2009 9:57 AM To: [email protected] Subject: Re: [C++-sig] wrapping reference members > I would think that .def_readwrite would do what you need. If it doesn't > look at using .def_property and get/set pair. Caveat: making sure that > 'a' stays a valid reference is your problem. Python will happily crash > horribly (if you are lucky) in response to bogus pointers or references. Thanks for your fast answer! I tried it with following code: class_("Foo", init<>()) .def_readwrite("a", &Foo::a) ; But it raises the error: error: cannot create pointer to reference member 'Foo::a' And it seems it's really not possible to create a pointer to a reference in c++ ;) So what else do I have to write instead of "&Foo::a"? Cheers, Sebastian ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] wrapping reference members
Thanks for the answer! ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Boost.Python indexing suite version 2?
I am looking into whether this will satisfy my needs. It looks good so far but I have a question: How am I supposed to use container_proxy? The only thing given for an example is a link to an empty file. Using it as a drop-in replacement for container_suite does not work. -Original Message- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies@py thon.org] On Behalf Of Roman Yakovenko Sent: Monday, July 20, 2009 1:31 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Boost.Python indexing suite version 2? On Mon, Jul 20, 2009 at 8:51 PM, Matthew Scouten (TT) wrote: > I would like to second this question. Right now, I am, with help from other people and from the author support this suite. Here( http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/index ing_suite_v2/ ) you can find an improve version of originally proposed suite. The important change is that today this library( indexing suite v2) is header only and could be installed\used without modifying Boost directories. I also added few unit tests and it is used in a few large projects without known issues. For more information read the following document: http://language-binding.net/pyplusplus/documentation/containers.html -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Boost.Python indexing suite version 2?
On Wed, Jul 22, 2009 at 11:00 PM, Matthew Scouten (TT) wrote: > I am looking into whether this will satisfy my needs. It looks good so > far but I have a question: > How am I supposed to use container_proxy? The only thing given for an > example is a link to an empty file. Using it as a drop-in replacement > for container_suite does not work. Unfortunately, I never used this class before. If you will send **small and complete** example of what you are trying to do, I will take a look on it. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ ___ Cplusplus-sig mailing list [email protected] http://mail.python.org/mailman/listinfo/cplusplus-sig
