RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
requestDispatcher.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_REQUESTDISPATCHER_HPP
14#define RTCTK_COMPONENTFRAMEWORK_REQUESTDISPATCHER_HPP
15
17#include <rtctk/config.hpp>
18
19#include <any>
20#include <functional>
21#include <initializer_list>
22#include <type_traits>
23#include <typeindex>
24#include <unordered_map>
25#include <utility>
26#include <vector>
27
29
30template <class Adapter, typename... Args>
32public:
33 using DispatchFunction = std::function<void(Adapter*, std::any&, Args... args)>;
34 using LookupTable = std::unordered_map<std::type_index, DispatchFunction>;
35 using TableEntry = typename LookupTable::value_type;
36
37 class RTCTK_API TableEntries final : public std::vector<TableEntry> {
38 public:
39 using std::vector<TableEntry>::vector;
40
42 TableEntries rhs_copy(rhs);
43 return this->operator+=(std::move(rhs_copy));
44 }
45
47 for (auto&& entry : std::forward<TableEntries>(rhs)) {
48 this->emplace_back(std::move(entry));
49 }
50 return *this;
51 }
52
54 TableEntries rhs_copy(rhs);
55 return this->operator+(std::move(rhs_copy));
56 }
57
59 TableEntries result;
60 result += *this;
61 result += std::forward<TableEntries>(rhs);
62 return result;
63 }
64 };
65
66 template <typename T>
67 static TableEntries Bind(void (Adapter::*method)(T&, Args...)) {
68 std::type_index type = typeid(T);
69 DispatchFunction function = [method](Adapter* adapter, std::any& request, Args... args) {
70 std::invoke(method, adapter, std::any_cast<T&>(request), args...);
71 };
72 return {{type, std::move(function)}};
73 }
74
75 RequestDispatcher() = default;
76 RequestDispatcher(const RequestDispatcher& rhs) = default;
77 RequestDispatcher(RequestDispatcher&& rhs) noexcept = default;
78 ~RequestDispatcher() = default;
80 RequestDispatcher& operator=(RequestDispatcher&& rhs) noexcept = default;
81
82 RequestDispatcher(std::initializer_list<TableEntries> table_entries) {
83 for (const auto& entry_list : table_entries) {
84 for (const auto& entry : entry_list) {
85 m_lut.emplace(entry);
86 }
87 }
88 }
89
90 void Dispatch(Adapter* adapter, std::any& request, Args... args) const {
91 auto iter = m_lut.find(request.type());
92 if (iter == m_lut.end()) {
93 throw CII_MAKE_EXCEPTION(UnsupportedTypeException, request.type());
94 }
95 auto function = iter->second;
96 function(adapter, request, args...);
97 }
98
100 RequestDispatcher rhs_copy(rhs);
101 return this->operator+=(std::move(rhs_copy));
102 }
103
105 m_lut.merge(std::forward<LookupTable>(rhs.m_lut));
106 return *this;
107 }
108
110 RequestDispatcher rhs_copy(rhs);
111 return this->operator+(std::move(rhs_copy));
112 }
113
115 RequestDispatcher result;
116 result += *this;
117 result += rhs;
118 return result;
119 }
120
121private:
122 LookupTable m_lut;
123};
124
125} // namespace rtctk::componentFramework
126
127#endif // RTCTK_COMPONENTFRAMEWORK_REQUESTDISPATCHER_HPP
Class used to parse default command line arguments.
Definition rtcComponentArgs.hpp:33
TableEntries & operator+=(const TableEntries &rhs)
Definition requestDispatcher.hpp:41
TableEntries operator+(const TableEntries &rhs) const
Definition requestDispatcher.hpp:53
TableEntries operator+(TableEntries &&rhs) const
Definition requestDispatcher.hpp:58
TableEntries & operator+=(TableEntries &&rhs)
Definition requestDispatcher.hpp:46
Definition requestDispatcher.hpp:31
void Dispatch(Adapter *adapter, std::any &request, Args... args) const
Definition requestDispatcher.hpp:90
RequestDispatcher(RequestDispatcher &&rhs) noexcept=default
static TableEntries Bind(void(Adapter::*method)(T &, Args...))
Definition requestDispatcher.hpp:67
RequestDispatcher & operator=(const RequestDispatcher &rhs)=default
RequestDispatcher & operator+=(const RequestDispatcher &rhs)
Definition requestDispatcher.hpp:99
RequestDispatcher operator+(const RequestDispatcher &rhs) const
Definition requestDispatcher.hpp:109
std::unordered_map< std::type_index, DispatchFunction > LookupTable
Definition requestDispatcher.hpp:34
typename LookupTable::value_type TableEntry
Definition requestDispatcher.hpp:35
RequestDispatcher & operator=(RequestDispatcher &&rhs) noexcept=default
RequestDispatcher(std::initializer_list< TableEntries > table_entries)
Definition requestDispatcher.hpp:82
std::function< void(Adapter *, std::any &, Args... args)> DispatchFunction
Definition requestDispatcher.hpp:33
RequestDispatcher operator+(RequestDispatcher &&rhs) const
Definition requestDispatcher.hpp:114
RequestDispatcher & operator+=(RequestDispatcher &&rhs)
Definition requestDispatcher.hpp:104
RequestDispatcher(const RequestDispatcher &rhs)=default
The UnsupportedTypeException is thrown whenever an attempt is made to use an unsupported type in the ...
Definition exceptions.hpp:239
Project-wide configuration header.
#define RTCTK_API
Helper to indicate that a class or function must be exported in the public symbol table.
Definition config.hpp:33
Provides macros and utilities for exception handling.
Definition commandReplier.cpp:22
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
DataPointPath operator+(DataPointPath lhs, const DataPointPath &rhs)
Definition dataPointPath.hpp:418