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