OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
ActionHandleConnector.h
Go to the documentation of this file.
1
5// ###########################################################################################################################################################################################################################################################################################################################
6
7#pragma once
8
9// OpenTwin header
12
13// std header
14#include <list>
15#include <string>
16#include <functional>
17
18#define OT_ADD_HANDLER(___functionName, ___className, ___actionName, ___messageTypeFlags) ot::ActionHandleConnector<___className> ActionHandleConnector##___functionName##=ot::ActionHandleConnector<___className>(___actionName, ___messageTypeFlags, this, &___className::___functionName)
19
26#define OT_HANDLER(___functionName, ___className, ___actionName, ___messageTypeFlags) std::string ___functionName(ot::JsonDocument& _document); OT_ADD_HANDLER(___functionName, ___className, ___actionName, ___messageTypeFlags);
27
29#define OT_ACTIONLIST(...) std::list<std::string>({__VA_ARGS__})
30
31namespace ot {
32
35 template <class T> class __declspec(dllexport) ActionHandleConnector : public ActionHandleConnectorBase {
36 public:
37 typedef std::string(T::* ConnectorMessageRef)(JsonDocument&);
38
40 ActionHandleConnector(const std::string& _actionName, ot::MessageType _messageFlags, ActionHandler* _handler, ConnectorMessageRef _handlerFunction);
41 ActionHandleConnector(const std::initializer_list<const char*>& _actionNames, ot::MessageType _messageFlags, ActionHandler* _handler, ConnectorMessageRef _handlerFunction);
42 ActionHandleConnector(const std::list<std::string>& _actionNames, ot::MessageType _messageFlags, ActionHandler* _handler, ConnectorMessageRef _handlerFunction);
44
45 ActionHandleConnector<T>& operator = (const ActionHandleConnector<T>& _other);
46
47 ActionHandler* getHandler(void) { return m_obj; }
48 ConnectorMessageRef getDispatchReference(void) { return m_func; }
49
50 virtual std::string forwardDispatch(JsonDocument& _doc) override;
51
52 private:
53 ActionHandler* m_obj;
54 ConnectorMessageRef m_func;
55 };
56}
57
58template <class T>
59ot::ActionHandleConnector<T>::ActionHandleConnector(void)
60 : ActionHandleConnectorBase(std::string(), ot::ALL_MESSAGE_TYPES), m_obj(nullptr), m_func(nullptr)
61{}
62
63template <class T>
64ot::ActionHandleConnector<T>::ActionHandleConnector(const std::string& _actionName, ot::MessageType _messageFlags, ActionHandler* _obj, ConnectorMessageRef _func)
65 : ActionHandleConnectorBase(_actionName, _messageFlags), m_obj(_obj), m_func(_func)
66{}
67
68template <class T>
69ot::ActionHandleConnector<T>::ActionHandleConnector(const std::initializer_list<const char*>& _actionNames, ot::MessageType _messageFlags, ActionHandler* _obj, ConnectorMessageRef _func)
70 : ActionHandleConnectorBase(std::list<std::string>(_actionNames), _messageFlags), m_obj(_obj), m_func(_func) {
71}
72
73template <class T>
74ot::ActionHandleConnector<T>::ActionHandleConnector(const std::list<std::string>& _actionNames, ot::MessageType _messageFlags, ActionHandler* _obj, ConnectorMessageRef _func)
75 : ActionHandleConnectorBase(_actionNames, _messageFlags), m_obj(_obj), m_func(_func)
76{}
77
78template <class T>
79ot::ActionHandleConnector<T>::ActionHandleConnector(const ActionHandleConnector<T>& _other)
80 : ActionHandleConnectorBase(_other), m_obj(_other.m_obj), m_func(_other.m_func)
81{}
82
83template <class T>
84ot::ActionHandleConnector<T>& ot::ActionHandleConnector<T>::operator = (const ActionHandleConnector<T>& _other) {
85 ActionHandleConnectorBase::operator=(_other);
86 m_obj = _other.m_obj;
87 m_func = _other.m_func;
88}
89
90template <class T>
91std::string ot::ActionHandleConnector<T>::forwardDispatch(JsonDocument& _doc) {
92 if (m_func) {
93 return std::invoke(m_func, dynamic_cast<T*>(m_obj), _doc);
94 }
95 else {
96 OTAssert(0, "Handler function not set");
97 return std::string();
98 }
99}
std::string string
Definition DocumentAccess.h:13
#define OTAssert(___expression, ___message)
Adds the ability to provide a custom text to the cassert macro (assertion only in debug mode)
Definition OTAssert.h:16
The ActionHandleConnector is used as a connector between the [ActionDispatcher] and a function in a [...
Definition ActionHandler.h:10
Definition Connector.h:8
MessageType
The message types describes how a message should be delivered.
Definition CommunicationTypes.h:10
@ ALL_MESSAGE_TYPES
Definition CommunicationTypes.h:15
STL namespace.