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

Add recursive_generator<T> coroutine type #6

Closed
lewissbaker opened this issue Apr 17, 2017 · 2 comments
Closed

Add recursive_generator<T> coroutine type #6

lewissbaker opened this issue Apr 17, 2017 · 2 comments
Assignees

Comments

@lewissbaker
Copy link
Owner

Add a new recursive_generator<T> type that allows for efficient enumeration over a sequence that is defined recursively.

This type would allow you to pass either a T or a recursive_generator<T> to the co_yield expression.
The increment operator on the iterator would then directly resume the leaf-most coroutine rather than the having to resume each coroutine in the stack until the leaf is resumed and then later suspend every coroutine on the stack for each item.

eg.

generator<directory_entry> list_directory(std::string path);

recursive_generator<directory_entry> recursive_list_directory(std::string path)
{
  for (auto& entry : list_directory(path))
  {
    co_yield entry;
    if (entry.is_directory())
    {
      co_yield recursive_list_directory(entry.path());
    }
  }
}

void usage()
{
  for (auto& entry : recursive_list_directory("foo/bar"))
  {
    std::cout << entry.path() << "\n";
  }
}
@lewissbaker
Copy link
Owner Author

The iteration should be cancellable at any point by simply destructing the owning recursive_generator<T> object. This should in turn destruct all nested coroutine frames recursively as the automatic variables within each coroutine fame, which should include the recursive_generator for the nested sequence, are destructed as the coroutine is destroyed.

@lewissbaker lewissbaker self-assigned this Jun 15, 2017
@lewissbaker
Copy link
Owner Author

Commit 6247291 added an initial version of recursive_generator.

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

1 participant