pw_containers#
Generic collections of objects for embedded devices
Stable C++17 Code Size Impact: 200 to 3300 bytes
Familiar: Pigweed containers resemble standard library containers.
Comprehensive: You can pick containers for speed, size, or in-between.
Embedded-friendly: No dynamic allocation needed!
1class Publisher {
2 public:
3 using Subscriber = pw::Function<void(const Message&)>;
4 static constexpr size_t kMaxSubscribers = 10;
5
6 pw::Status Subscribe(Subscriber&& subscriber) {
7 // Check if the vector's fixed capacity has been exhausted.
8 if (subscribers_.full()) {
9 return pw::Status::ResourceExhausted();
10 }
11
12 // Add the subscriber to the vector.
13 subscribers_.emplace_back(std::move(subscriber));
14 return pw::OkStatus();
15 }
16
17 void Publish(const Message& message) {
18 // Iterate over the vector.
19 for (auto& subscriber : subscribers_) {
20 subscriber(message);
21 }
22 }
23
24 private:
25 pw::Vector<Subscriber, kMaxSubscribers> subscribers_;
26};
Much like the standard containers library, the pw_containers
module provides
embedded-friendly container class templates and algorithms. These allow
developers to implement data structures for embedded devices that are more
resource-constrained than conventional applications. These containers include:
Zephyr#
To enable pw_containers
for Zephyr add CONFIG_PIGWEED_CONTAINERS=y
to
the project’s configuration.