EduArt Sensor Ring Library 4.0.0
Loading...
Searching...
No Matches
ActionDispatcher.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
20
21namespace eduart {
22
23namespace sensorring {
24
29class SENSORRING_EXPORT ActionDispatcher {
30public:
31 virtual ~ActionDispatcher() = default;
32
33 void setQueue(ActionQueue* queue) { _queue = queue; }
34
35 void clearQueue() { _queue = nullptr; }
36
37 bool isManaged() const { return _queue != nullptr; }
38
39 void execute(Action action) {
40 if (_queue) {
41 _queue->enqueue(this, "", std::move(action));
42 } else {
43 try {
44 action();
45 } catch (std::exception& e) {
46 logger::Logger::getInstance()->log(logger::LogVerbosity::Error, "Exception occurred while executing action: " + std::string(e.what()));
47 }
48 }
49 }
50
51 void execute(std::string_view key, Action action) {
52 if (_queue) {
53 _queue->enqueue(this, key, std::move(action));
54 } else {
55 try {
56 action();
57 } catch (std::exception& e) {
58 logger::Logger::getInstance()->log(logger::LogVerbosity::Error, "Exception occurred while executing action: " + std::string(e.what()));
59 }
60 }
61 }
62
63 static void globalExecute(Action action) {
64 if (ActionQueue::getGlobalQueue()) {
65 ActionQueue::getGlobalQueue()->enqueue(std::move(action));
66 } else {
67 try {
68 action();
69 } catch (std::exception& e) {
70 logger::Logger::getInstance()->log(logger::LogVerbosity::Error, "Exception occurred while executing global action: " + std::string(e.what()));
71 }
72 }
73 }
74
75private:
76 ActionQueue* _queue = nullptr;
77};
78
79} // namespace sensorring
80
81} // namespace eduart
Queue to manage reusable actions.
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.