[C++-sig] A wrapped class contains another wrapped class. Hilarity ensues.

Matthew Scouten matthew.scouten at gmail.com
Mon Nov 26 22:05:30 CET 2007


After one week with no reply I think I must have asked a stupid question (or
one that is so crazy difficult that no one knows the answer).

Still search as I might, I can not understand what is wrong here, or why it
does not work.
Maybe I do not understand some important concept that world make the answer
obvious (I am rather new to python after all).

Much googleing found this link:
http://mail.python.org/pipermail/c++-sig/2005-August/009339.html

I do not think that his problem is quite the same as mine, but there were
just enough hits to be more frustrating than helpful.

Why is the error message telling me about l-values when I am not trying to
assign to anything, just call a function on it?
What are to/from python converters exactly? Are they related to, or the same
as  l-value,r-value converters?
Why would I define them manually? I thought that was what I was doing when I
wrapped the class in the first place.
Is that really my problem, or is it more (or less) subtle than that?

If I really am being a dumb ass, please feel free to flame me (as long as
you include a link to the answers I apparently should have found on my own,
to prove I am a dumb ass).




On Nov 16, 2007 2:31 PM, Matthew Scouten <matthew.scouten at gmail.com> wrote:

> I have 2 c++ classes called foo and bar. I wrapped both. bar contains a
> foo as a public data member. foo contains a function called get_buffer that
> is exported as "__str__" and as the getter of the property 'buffer'
>
> if I construct a foo, I can call that function with no problem.
> if I create a bar and try to call the function on its foo I get an error.
>
> My C++ and B::P code:
>
> class foo
> {
> public:
>
>     foo() {std::strcpy(buffer, "good bye cruel world!\0\0");}
>     char buffer[24];
> };
>
> struct foo_wrapper : foo , wrapper<foo>
> {
>         foo_wrapper(const foo& f);
>         foo_wrapper();
>
>         //property getters
>         std::string get_buffer() const;
> };
>
> foo_wrapper::foo_wrapper(const foo& exch): foo(exch), wrapper<foo>(){}
>
> foo_wrapper::foo_wrapper(): foo(), wrapper<foo>(){}
>
> std::string foo_wrapper::get_buffer() const
> {
>     return std::string(foo::buffer);
> }
>
>
> class bar
> {
> public:
>     foo f;
> };
>
> class bar_wrapper: public bar, public wrapper<bar>
> {
> public:
>     bar_wrapper();
>     bar_wrapper(const bar &);
> };
>
> bar_wrapper::bar_wrapper() : bar(), wrapper<bar>(){}
>
> bar_wrapper::bar_wrapper(const bar &bb ) : bar(bb), wrapper<bar>(){}
>
> BOOST_PYTHON_MODULE(BusyBox)
> {
>
>     class_<foo_wrapper>("foo")
>         .def(init<>())
>         .add_property("buffer", &foo_wrapper::get_buffer)
>         .def("__str__", &foo_wrapper::get_buffer)
>         ;
>
>     class_<bar_wrapper>("bar")
>         .def_readwrite("f", &bar::f)
>         ;
> }
>
> A REPL session that demos the errors:>>> from BusyBox import *
> >>> f1 = foo()
> >>>
> >>> f1.buffer
> 'good bye cruel world!'
> >>> str(f1)
> 'good bye cruel world!'
> >>>
> >>> b1 = bar()
> >>>
> >>> b1.f.buffer #Generates error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> Boost.Python.ArgumentError: Python argument types in
>     None.None(foo)
> did not match C++ signature:
>     None(struct foo_wrapper {lvalue})
> >>> str(b1.f)   #Generates error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> Boost.Python.ArgumentError: Python argument types in
>     foo.__str__(foo)
> did not match C++ signature:
>     __str__(struct foo_wrapper {lvalue})
> >>>
> >>> f2 = b1.f
> >>>
> >>> b2.buffer   #Generates error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'b2' is not defined
> >>> str(f2)     #Generates error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> Boost.Python.ArgumentError: Python argument types in
>     foo.__str__(foo)
> did not match C++ signature:
>     __str__(struct foo_wrapper {lvalue})
> >>>
>
> I feel like I must be doing something stupid. What is wrong?
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071126/b78af4b2/attachment.htm>


More information about the Cplusplus-sig mailing list