Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define_iterator not using passed begin/end #103

Closed
hsitter opened this issue Nov 6, 2017 · 1 comment
Closed

define_iterator not using passed begin/end #103

hsitter opened this issue Nov 6, 2017 · 1 comment

Comments

@hsitter
Copy link
Contributor

hsitter commented Nov 6, 2017

define_iterator has two arguments for member function pointers to get the beginning respectively end of an iterator. They don't do anything though.

The pointers are passed into the Iterator_Impl and held as members but not ever used. Instead, the Iterator_Impl always calls

    Iterator_T it = obj->begin();
    Iterator_T end = obj->end();

making the originally passed pointers utterly useless.
This is a problem when one needs multiple iterators on the same entity or to wrap a pre-existing data structure which has non-standard begin/end functions.

e.g.

    Data_Type<pkgCache> rb_cPkgCache =
      define_class<pkgCache>("PkgCache")
      .define_iterator(&pkgCache::PkgBegin, &pkgCache::PkgEnd);

results in build error

/home/me/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rice-2.1.1/ruby/lib/include/rice/detail/Iterator.hpp:51:32: error: ‘class pkgCache’ has no member named ‘begin’
     Iterator_T it = obj->begin();
                                ^
/home/me/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rice-2.1.1/ruby/lib/include/rice/detail/Iterator.hpp:52:31: error: ‘class pkgCache’ has no member named ‘end’
     Iterator_T end = obj->end();
                               ^

as the T in question actually doesn't implement begin/end.

Currently, the only way to deal with this seems to be manually implementing a .each or stacking a proxy class on top. Latter still has the problem that one cannot have a second iterator.

@jasonroelofs
Copy link
Collaborator

I see what you mean. Thanks for the report. Doesn't look like a tough thing to fix, I'll see what I can do. If you do get a chance to try a fix yourself I'm happy to review as well.

hsitter added a commit to hsitter/rice that referenced this issue Nov 7, 2017
previously we'd ignore the passed ptrs and always call `begin()`/`end()`
making the ptrs entirely useless.
the new calls properly go to the passed functions on the object we want to
iter on.

adjusted iter test to make sure this continues working

Fixes ruby-rice#103
hsitter added a commit to hsitter/rice that referenced this issue Nov 8, 2017
previously we'd ignore the passed ptrs and always call `begin()`/`end()`
making the ptrs entirely useless.
the new calls properly go to the passed functions on the object we want to
iter on.

adjusted iter test to make sure this continues working

Fixes ruby-rice#103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants