vtk-m/docs/changelog/custom_portal_iterators.md
Allison Vacanti 813f5a422f Fixup custom portal iterator logic.
The convenience functions `ArrayPortalToIteratorBegin()` and
`ArrayPortalToIteratorEnd()` wouldn't detect specializations of
`ArrayPortalToIterators<PortalType>` since the specializations aren't
visible when the `Begin`/`End` functions are declared.

Since the CUDA iterators rely on a specialization, the convenience
functions would not compile on CUDA.

Now, instead of specializing `ArrayPortalToIterators` to provide custom
iterators for a particular portal, the portal may advertise custom
iterators by defining `IteratorType`, `GetIteratorBegin()`, and
`GetIteratorEnd()`. `ArrayPortalToIterators` will detect such portals
and automatically switch to using the specialized portals.

This eliminates the need for the specializations to be visible to the
convenience functions and allows them to be usable on CUDA.
2019-12-17 15:39:51 -05:00

1.1 KiB

Portals may advertise custom iterators

The ArrayPortalToIterator utilities are used to produce STL-style iterators from vtk-m's ArrayHandle portals. By default, a facade class is constructed around the portal API, adapting it to an iterator interface.

However, some portals use iterators internally, or may be able to construct a lightweight iterator easily. For these, it is preferable to directly use the specialized iterators instead of going through the generic facade. A portal may now declare the following optional API to advertise that it has custom iterators:

struct MyPortal
{
  using IteratorType = ...; // alias to the portal's specialized iterator type
  IteratorType GetIteratorBegin(); // Return the begin iterator
  IteratorType GetIteratorEnd(); // Return the end iterator

  // ...rest of ArrayPortal API...
};

If these members are present, ArrayPortalToIterators will forward the portal's specialized iterators instead of constructing a facade. This works when using the ArrayPortalToIterators class directly, and also with the ArrayPortalToIteratorBegin and ArrayPortalToIteratorEnd convenience functions.