[C++-sig] Re: Re: Re: Re: Re: custom iterator object

David Abrahams dave at boost-consulting.com
Tue Nov 12 15:10:15 CET 2002


"Mike Rovner" <mike at bindkey.com> writes:

> "David Abrahams" <dave at boost-consulting.com> wrote in message
> news:u8yzzxxjb.fsf at boost-consulting.com...
>> > I have problem _creating that iterator_ from another object.
>> > For creating an iterator I have to call member function.
>> > For calling it I have to have a C++ object reference, i.e.
>> >
>> > Scheme& s;
>> > MyIter s.*pm();
>> >
>> > Now I'm trying to wrap that class (that shall return an iterator):
>> >
>> >   class_<Scheme>("Scheme")
>> >     .def("__iter__", mem_fun_ref(&Scheme::GetIter))
>>
>> Why aren't you just using
>>
>>      .def("__iter__", &Scheme::GetIter)
>>
>> here? What is mem_fun_ref supposed to buy you?
>
> I'm sorry again. I'm using mem_fun_ref because
>
> class Scheme {
> //has only
>    CustomIter GetIter();
> }

What is CustomIter? I've never seen this before.  I'm sorry, but I
can't be expected to understand your code if you don't give me a
complete picture.

> and I use
>
> class MyIter {
>    MyIter(CustomIter& init);
>    T next();
> }
>
> class_<MyIter>("_myiter")
>   .def("__iter__", identity)
>   .def("next, &MyIter::next)
> ;
>
> instead of CustomIter to convert it to Python iterator protocol.

So, you want CustomIter objects to be converted to python by first
building a MyIter object around them?

> So mem_fun_ref returns some
> mem_fun_ref_t<T,Scheme> object instance with operator() which when called
> with Scheme& produces MyIter object.

Not unless there's a "typedef MyIter CustomIter;" you haven't shown
me. According to the code here, that isn't what mem_fun_ref is
doing. It's making a mem_fun_ref<CustomIter,Scheme> object which when
called with Scheme& produces a CustomIter object.

So it's hard to see what advantage it would give you, even if it could
work (which it can't).

> That wrapped MyIter object shall be return value from wrapped
> Scheme::GetIter() member function.
>
> I already figured out that if I want call .def with object it shall
> be 'object'. So I need to wrap mem_fun_ref_t<T,Scheme> into callable
> python object and instantiate it, right?

I guess so... though everything else you've said makes this sound misguided.

> Wrapping is easy:
>
> typedef mem_fun_ref_t<T,Scheme> Gen;
> class_<Gen>("_gen").def("__call__", detail::operator_<Gen>()); // is it correct?

No, you're not supposed to touch anything in boost::python::detail::.

> but how to instantiate that new object?

I still don't know what you're driving at.
-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list