[PATCH] D57618: [ADT] Add a fallible_iterator wrapper.

Lang Hames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 1 14:07:30 PST 2019


lhames created this revision.
lhames added reviewers: dblaikie, rupprecht.
Herald added subscribers: kristina, dexonsmith, mgorny.
Herald added a project: LLVM.

A fallible iterator is one whose increment or decrement operations may fail.
This would usually be supported by replacing the usually iterator increment
and decrement operations (++ / --) with methods that return error:

  class MyFallibleIterator {
  public:
    // ...
    Error inc();
    Errro dec();
    // ...
  };

The downside of this style is that it no longer conforms to the C++ iterator
concept, and can not make use of standard algorithms and features such as
range-based for loops.

The fallible_iterator wrapper takes an iterator written in the style above
and adapts it to (mostly) conform with the C++ iterator concept. It does this
by providing standard ++ and -- operator implementations, returning any errors
generated via a side channel (an Error reference passed into the wrapper at
construction time), and immediately jumping the iterator to a known 'end'
value upon error.

Usage looks like:

  MyFallibleIterator I = ..., E = ...;
  
  Error Err = Error::success();
  for (auto &Elem : make_fallible_range(I, E, Err)) {
    // Loop body is only entered when safe.
    // Early exits from loop body permitted without checking Err.
  }
  if (Err)
    // Handle error.


Repository:
  rL LLVM

https://reviews.llvm.org/D57618

Files:
  include/llvm/ADT/fallible_iterator.h
  unittests/ADT/CMakeLists.txt
  unittests/ADT/FallibleIteratorTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57618.184837.patch
Type: text/x-patch
Size: 16647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190201/0d5cc6b5/attachment.bin>


More information about the llvm-commits mailing list