WidgetRegister.cpp
Go to the documentation of this file.
1#include "WidgetRegister.h"
2
3#include <map>
4#include <memory>
5#include <typeindex>
6
7// DO NOT REMOVE. NEEDS TO BE INCLUDED BEFORE ExpressionException.h for `LocalException() << ` construct below.
9
12
13#include "Widgets.h"
14
15namespace armarx::RemoteGui
16{
17
18 typedef std::map<std::type_index, WidgetHandler*> WidgetRegister;
19
20 template <typename HandlerT>
21 void
23 {
25 static WidgetHandler handler;
26 register_.emplace(typeid(typename WidgetHandler::RemoteWidgetT), &handler);
27 }
28
64
65 const WidgetHandler&
66 getWidgetHandler(WidgetPtr const& desc)
67 {
69 static const WidgetRegister widgetRegister = createWidgetRegister();
70
71 auto descPtr = desc.get();
72 ARMARX_CHECK_EXPRESSION(descPtr != nullptr);
73
74 std::type_info const& typeInfo = typeid(*descPtr);
75
76 auto iter = widgetRegister.find(typeInfo);
77 if (iter == widgetRegister.end())
78 {
79 throw armarx::LocalException()
80 << "Unexpected widget type: " << GetTypeString(typeInfo)
81 << "\n only these types are supported:" << ARMARX_STREAM_PRINTER
82 {
83 for (const auto& [tid, _] : widgetRegister)
84 {
85 out << "\n " << GetTypeString(tid);
86 }
87 };
88 }
89
90 auto& widgetHandler = iter->second;
91 ARMARX_CHECK_EXPRESSION(widgetHandler);
92
93 return *widgetHandler;
94 }
95
96} // namespace armarx::RemoteGui
#define ARMARX_STREAM_PRINTER
use this macro to write output code that is executed when printed and thus not executed if the debug ...
Definition Logging.h:310
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
void registerHandler(WidgetRegister &register_)
std::map< std::type_index, WidgetHandler * > WidgetRegister
WidgetRegister createWidgetRegister()
const WidgetHandler & getWidgetHandler(WidgetPtr const &desc)
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
#define ARMARX_TRACE
Definition trace.h:77