Utilities#

pw_containers: Generic collections of objects for embedded devices

In addition to containers, this module includes some types and functions for working with containers and the data within them.

API reference#

Moved: pw_containers_utilities

pw::containers::WrappedIterator#

pw::containers::WrappedIterator is a class that makes it easy to wrap an existing iterator type. It reduces boilerplate by providing operator++, operator--, operator==, operator!=, and the standard iterator aliases (difference_type, value_type, etc.). It does not provide the dereference operator; that must be supplied by a derived class.

Example#

To use it, create a class that derives from WrappedIterator and define operator*() and operator->() as appropriate. The new iterator might apply a transformation to or access a member of the values provided by the original iterator. The following example defines an iterator that multiplies the values in an array by 2.

 1using pw::containers::WrappedIterator;
 2
 3// Multiplies values in a std::array by two.
 4class DoubleIterator : public WrappedIterator<DoubleIterator, const int*, int> {
 5 public:
 6  constexpr DoubleIterator(const int* it) : WrappedIterator(it) {}
 7  int operator*() const { return value() * 2; }
 8};
 9
10// Returns twice the sum of the elements in a array of integers.
11template <size_t kArraySize>
12int DoubleSum(const std::array<int, kArraySize>& c) {
13  int sum = 0;
14  for (DoubleIterator it(c.data()); it != DoubleIterator(c.data() + c.size());
15       ++it) {
16    // The iterator yields doubles instead of the original values.
17    sum += *it;
18  }
19  return sum;
20}

Basic functional programming#

WrappedIterator may be used in concert with FilteredView to create a view that iterates over a matching values in a container and applies a transformation to the values. For example, it could be used with FilteredView to filter a list of packets and yield only one field from the packet.

The combination of FilteredView and WrappedIterator provides some basic functional programming features similar to (though much more cumbersome than) generator expressions (or filter/map) in Python or streams in Java 8. WrappedIterator and FilteredView require no memory allocation, which is helpful when memory is too constrained to process the items into a new container.

pw::containers::to_array#

pw::containers::to_array() is a C++14-compatible implementation of C++20’s std::to_array. In C++20, it is an alias for std::to_array. It converts a C array to a std::array.

pw::all_of#

pw::all_of() is a C++17 compatible implementation of C++20’s std::all_of. In C++20, it is an alias for std::all_of. This backports the constexpr overload of the function.

pw::any_of#

pw::any_of() is a C++17 compatible implementation of C++20’s std::any_of. In C++20, it is an alias for std::any_of. This backports the constexpr overload of the function.

pw::find_if#

pw::find_if() is a C++17 compatible implementation of C++20’s std::find_if. In C++20, it is an alias for std::find_if. This backports the constexpr overload of the function.