EduArt Sensor Ring Library 3.0.0
Loading...
Searching...
No Matches
DeviceGroup.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 EduArt Robotik GmbH
2
9
10#pragma once
11
12#include <chrono>
13#include <functional>
14#include <future>
15#include <vector>
16
17#include "sensorring/platform/SensorringExport.hpp"
18
19#include "IDevice.hpp"
20
21namespace eduart {
22
23namespace device {
24
29class SENSORRING_EXPORT DeviceGroup {
30
31public:
36 DeviceGroup(std::vector<device::IDevice*> devices);
37
39 ~DeviceGroup() = default;
40
45 std::vector<device::IDevice*> getDevices() const;
46
51 unsigned int getDeviceCount() const;
52
62 template <typename Response, typename Predicate> static bool waitForAll(std::vector<std::future<Response> >& futures, std::chrono::steady_clock::duration timeout, Predicate&& success_predicate) noexcept;
63
68 void invokeForEachDevice(std::function<void(device::IDevice*)> callback) const;
69
75 template <typename T> std::vector<T*> getDevicesOfType() const;
76
82 template <typename T> void invokeForEachDeviceOfType(std::function<void(T*)> callback) const;
83
90 template <typename T> static DeviceGroup createFromDevicesOfType(std::vector<device::IDevice*> devices);
91
92private:
93 std::vector<device::IDevice*> _devices;
94};
95
96// Template implementations
97template <typename T> std::vector<T*> DeviceGroup::getDevicesOfType() const {
98 std::vector<T*> devices;
99 for (auto& device : _devices) {
100 if (auto* cast = dynamic_cast<T*>(device)) {
101 devices.push_back(cast);
102 }
103 }
104 return devices;
105}
106
107template <typename T> void DeviceGroup::invokeForEachDeviceOfType(std::function<void(T*)> callback) const {
108 for (auto& device : _devices) {
109 if (auto* cast = dynamic_cast<T*>(device)) {
110 callback(cast);
111 }
112 }
113}
114
115template <typename T> DeviceGroup DeviceGroup::createFromDevicesOfType(std::vector<device::IDevice*> devices) {
116 std::vector<device::IDevice*> filtered;
117 for (auto& device : devices) {
118 if (dynamic_cast<T*>(device)) {
119 filtered.push_back(device);
120 }
121 }
122 return DeviceGroup(std::move(filtered));
123}
124
125template <typename Response, typename Predicate> bool DeviceGroup::waitForAll(std::vector<std::future<Response> >& futures, std::chrono::steady_clock::duration timeout, Predicate&& success_predicate) noexcept {
126 if (futures.empty())
127 return true;
128 const auto deadline = std::chrono::steady_clock::now() + timeout;
129
130 // First, wait for all futures to become ready (or until timeout).
131 for (auto& fut : futures) {
132 if (fut.wait_until(deadline) != std::future_status::ready) {
133 return false; // At least one future did not complete in time.
134 }
135 }
136
137 // Then, consume results and apply the predicate.
138 bool all_ok = true;
139 for (auto& fut : futures) {
140 if (!success_predicate(fut.get())) {
141 all_ok = false;
142 }
143 }
144 return all_ok;
145}
146
147} // namespace device
148
149} // namespace eduart
void invokeForEachDevice(std::function< void(device::IDevice *)> callback) const
Invokes callback once per device in the group.
static bool waitForAll(std::vector< std::future< Response > > &futures, std::chrono::steady_clock::duration timeout, Predicate &&success_predicate) noexcept
Waits until all futures are ready within the given timeout, then checks each result with a predicate.
Definition DeviceGroup.hpp:125
static DeviceGroup createFromDevicesOfType(std::vector< device::IDevice * > devices)
Builds a DeviceGroup containing only devices of type T from the given list.
Definition DeviceGroup.hpp:115
~DeviceGroup()=default
Destructor.
std::vector< T * > getDevicesOfType() const
Returns devices that are of type T (dynamic_cast).
Definition DeviceGroup.hpp:97
unsigned int getDeviceCount() const
Returns the number of devices in the group.
std::vector< device::IDevice * > getDevices() const
Returns all devices in the group.
void invokeForEachDeviceOfType(std::function< void(T *)> callback) const
Invokes callback for each device that is of type T.
Definition DeviceGroup.hpp:107
DeviceGroup(std::vector< device::IDevice * > devices)
Constructs a group from the given device pointers.
Thin base interface for all concrete devices in the SensorRing.
Definition IDevice.hpp:18