Iterable Class
Helper class to allow collection types to support iterators. More...
Public Types
class | const_iterator |
class | iterator |
Public Functions
iterator | begin() |
const_iterator | begin() const |
const_iterator | cbegin() const |
const_iterator | cend() const |
const_iterator | constBegin() const |
const_iterator | constEnd() const |
iterator | end() |
const_iterator | end() const |
Detailed Description
Helper class to allow collection types to support iterators.
Any class that inherits Iterable will support C++11 range-for loops as well as anything else that works with forward iterators.
Iterable types implement std::forward_iterator_tag functionality.
The purpose of this class is to allow certain C++11 and newer features to work with exising classes in the API. For example, you can iterate over the map's layers:
for (auto* layer : *(map->operationalLayers()))
{
...
}
In addition to the C++11 range-based for looping, you can also use std::for_each. For example:
QStringList layerNames;
std::for_each(operationalLayers->cbegin(), operationalLayers->cend(), [&layerNames](Layer* layer)
{ // gather all the names of the layers
layerNames.append(layer->name());
});
Another example is searching for a layer with a specific name:
auto* opLayers = scene->operationalLayers();
auto it = std::find_if(opLayers->constBegin(), opLayers->constEnd(), [](Layer* l)
{
return l->name() == "MyLayer";
});
if (it == opLayers->constEnd())
return; // no match
// we found a match
Layer* match = *it;
Iterable also works with value objects. For example, you can iterate over the points in an ImmutablePointCollection:
const ImmutablePointCollection points = getPoints();
for (const Point& p : points)
{
// do stuff with the points...
}
Member Function Documentation
iterator Iterable::begin()
Returns a non-const iterator to the first element of the collection.
const_iterator Iterable::begin() const
Returns a const iterator to the first element of the collection.
See also constBegin and cbegin.
const_iterator Iterable::cbegin() const
Returns a const iterator to the first element of the collection.
See also constBegin.
const_iterator Iterable::cend() const
Returns a const iterator one past the last element in the collection.
The cend
iterator should not be dereferenced as it doesn't point to a valid object.
See also constEnd.
const_iterator Iterable::constBegin() const
Returns a const iterator to the first element of the collection.
See also cbegin.
const_iterator Iterable::constEnd() const
Returns a const iterator one past the last element in the collection.
The constEnd
iterator should not be dereferenced as it doesn't point to a valid object.
See also cend.
iterator Iterable::end()
Returns a non-const iterator one past the last element in the collection.
The end
iterator should not be dereferenced as it doesn't point to a valid object.
const_iterator Iterable::end() const
Returns a const iterator one past the last element in the collection.
The end
iterator should not be dereferenced as it doesn't point to a valid object.