EduArt Sensor Ring Library 4.0.0
Loading...
Searching...
No Matches
ActionQueue.hpp
Go to the documentation of this file.
1// Copyright (c) 2026 EduArt Robotik GmbH
2
9
10#pragma once
11
12#include <deque>
13#include <functional>
14#include <string_view>
15#include <unordered_map>
16
19
20namespace eduart {
21
22namespace sensorring {
23
28class SENSORRING_EXPORT ActionQueue {
29public:
30 // Enqueue a standard non-replaceable action
31 void enqueue(Action action) { _queue.push_back({ std::move(action), nullptr, {} }); }
32
33 // Enqueue or update a replaceable action for a specific instance & key
34 void enqueue(const ActionDispatcher* owner, ActionKey key, Action action) {
35 auto lookupKey = std::make_pair(owner, key);
36
37 auto it = indexMap_.find(lookupKey);
38 if (it != indexMap_.end()) {
39 // Action already exists in queue: replace its execution payload in-place
40 it->second->execute = std::move(action);
41 } else {
42 // New action: add to queue and track its position in the map
43 _queue.push_back({ std::move(action), owner, key });
44 indexMap_[lookupKey] = --_queue.end();
45 }
46 }
47
48 // Process all pending commands in FIFO order
49 void processAll() {
50 while (!_queue.empty()) {
51 Command cmd = std::move(_queue.front());
52 _queue.pop_front();
53
54 // Clear map entry if this was a tracked replaceable action
55 if (cmd.isReplaceable()) {
56 indexMap_.erase({ cmd.owner, cmd.key });
57 }
58
59 try {
60 cmd.execute();
61 } catch (std::exception& e) {
62 logger::Logger::getInstance()->log(logger::LogVerbosity::Error, "Exception occurred while executing action: " + std::string(e.what()));
63 }
64 }
65 }
66
67 static void clearGlobalQueue() { _global_queue = nullptr; }
68
69 static void setGlobalQueue(ActionQueue* queue) { _global_queue = queue; }
70
71 static ActionQueue* getGlobalQueue() { return _global_queue; }
72
73private:
74 // Pair hasher for unordered_map lookup
75 struct PairHash {
76 std::size_t operator()(const std::pair<const ActionDispatcher*, ActionKey>& p) const {
77 auto h1 = std::hash<const ActionDispatcher*>{}(p.first);
78 auto h2 = std::hash<ActionKey>{}(p.second);
79 return h1 ^ (h2 << 1);
80 }
81 };
82
83 std::deque<Command> _queue;
84 inline static ActionQueue* _global_queue = nullptr;
85 std::unordered_map<std::pair<const ActionDispatcher*, ActionKey>, std::deque<Command>::iterator, PairHash> indexMap_;
86};
87
88} // namespace sensorring
89
90} // namespace eduart
Command structure for reusable actions.
@ Error
Error conditions.
Definition LogVerbosity.hpp:35
Logger of the sensorring library.
Base class that executes actions either immediately or via a managed queue.
Definition ActionDispatcher.hpp:29
Queue to manage reusable actions.
Definition ActionQueue.hpp:28
static Logger * getInstance() noexcept
Get a pointer to the instance of the Logger singleton.
Definition Command.hpp:26
bool isReplaceable() const
Returns true if this command is replaceable (i.e. has a non-null owner and a non-empty key).
Definition Command.hpp:37
const ActionDispatcher * owner
Owner of the command used to identify replaceable actions. If owner == nullptr, this task is non-repl...
Definition Command.hpp:31
Action execute
Callable to execute the action.
Definition Command.hpp:28
ActionKey key
Key used to identify replaceable actions for a specific owner instance. If key is empty,...
Definition Command.hpp:34