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

Explicitly copy way.nodes() into vector to have proper table meta functions #3468

Merged
merged 1 commit into from
Jan 4, 2017

Conversation

oxidase
Copy link
Contributor

@oxidase oxidase commented Dec 17, 2016

Issue

For #3452 added index, length and call metafunctions to WayNodeList, so it is possible to access node references in lua as

  local nodes = way:get_nodes()
  for index = 1, #nodes do
     print (nodes[index]:id())
  end

or

  for i,node in ipairs(way:get_nodes()()) do
     print (i, node:id())
  end

The return value of call is a table, so for node in way:get_nodes() do does not work. It should be checked what call should return to have an "iterable" object.

EDIT: the original commit defines explicitly meta functions for WayNodeList. Also __pairs and __ipairs functions can not be explicitly defined for a usertype. Just copying NodeRef to vector solves the issue by using implicitly container_usertype_metatable.

Tasklist

  • add regression / cucumber cases (see docs/testing.md)
  • review
  • adjust for comments

@oxidase oxidase added the Review label Dec 17, 2016
@oxidase oxidase changed the title Added index, length and call metafunctions to WayNodeList Explicitly copy way.nodes() into vector to have proper table meta functions Dec 17, 2016
@daniel-j-h
Copy link
Member

This looks like a good workaround to let users still do what they want to.

Still we break profiles here by not providing a 100% replacement with Luabind .. hrm.

@oxidase
Copy link
Contributor Author

oxidase commented Dec 19, 2016

I guess to have the old behavior WayNodeList should return bogus IteratorX

        sol::meta_function::call,
        [](const osmium::WayNodeList &list) { return IteratorX{list.begin(), list.end()}; }

but i did not find how to make working. https://github.com/Project-OSRM/osrm-backend/blob/master/third_party/sol2/sol2/sol.hpp#L11334 is how it works with sol __pairs/__ipairs

@TheMarex
Copy link
Member

Reading the issues around this, I think only supporting stateful iterators for Lua 5.2+ is fine. There is a workaround for old lua versions, and really we don't want to be slowed down by Lua 5.1 that much.

@oxidase can you rebase and merge?

@oxidase
Copy link
Contributor Author

oxidase commented Dec 23, 2016

@TheMarex rebased. But just one note - the change will not allow for node in way:get_nodes() do with a stateful iterator. Possible are for _, node in ipairs(way:get_nodes()) do with a state-less iterator or for i = 1, #way:get_nodes() do with an index.

I simply would rely on sol2 implementation: if the lua table interface for std::vector will implement sol::meta_function::call than for node in way:get_nodes() do will work without changes in OSRM code.

@MoKob
Copy link

MoKob commented Jan 3, 2017

@daniel-j-h @oxidase from the conversation this looks like it is ready to merge, any objections here?

@oxidase
Copy link
Contributor Author

oxidase commented Jan 3, 2017

@MoKob PR makes a copy of NodeRefList as a vector<NodeRef> that is not good. It should be checked first if a correct metatable for NodeRefList can be created automatically.

@MoKob
Copy link

MoKob commented Jan 3, 2017

As long as this check is pending, I am re-tagging this as work in progress to avoid confusion. Feel free to directly move to ready-to-merge when checks should turn out fine.

@oxidase oxidase force-pushed the sol-metafunctions branch from 980b2a5 to 1b37df9 Compare January 3, 2017 10:48
@oxidase
Copy link
Contributor Author

oxidase commented Jan 3, 2017

updated PR to wrap a NodeRefList const reference by sol::as_table. So copying is avoided, but life time of way:get_nodes() returned value must be in scope of way.

Although the original issue #3452 is not completely resolved, because the returned WayNodeList value has sol table interface that supports only stateless iterators, i think PR can be merged now.

@oxidase
Copy link
Contributor Author

oxidase commented Jan 3, 2017

updated PR due to windows build fail: decltype(way.nodes()) is resolved by sol to sol.osmium::WayNodeList * lua type that has no table metafunctions. With in-place lambda type is correctly resolved.

@MoKob MoKob merged commit 2640a31 into master Jan 4, 2017
@MoKob MoKob deleted the sol-metafunctions branch January 4, 2017 07:29
@MoKob MoKob removed the Review label Jan 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants