1 | #ifndef IN_OUT_BASE_H
|
---|
2 | #define IN_OUT_BASE_H
|
---|
3 |
|
---|
4 | #include <Pacpus/kernel/pacpus.h>
|
---|
5 | #include <Pacpus/kernel/ConnectionBase.h>
|
---|
6 | #include <Pacpus/kernel/PacpusEvent.h>
|
---|
7 |
|
---|
8 | #include <QApplication>
|
---|
9 | #include <QList>
|
---|
10 | #include <QString>
|
---|
11 | #include <QStringList>
|
---|
12 | #include <typeinfo>
|
---|
13 |
|
---|
14 | namespace pacpus {
|
---|
15 |
|
---|
16 | class ComponentBase;
|
---|
17 |
|
---|
18 | class PACPUSLIB_API AbstractInterface
|
---|
19 | : public QObject
|
---|
20 | {
|
---|
21 | Q_OBJECT
|
---|
22 |
|
---|
23 | protected:
|
---|
24 | AbstractInterface(QString name, ComponentBase * component, QObject * parent = 0)
|
---|
25 | : m_name(name)
|
---|
26 | , m_component(component)
|
---|
27 | , QObject(parent)
|
---|
28 | {}
|
---|
29 |
|
---|
30 | ~AbstractInterface()
|
---|
31 | {}
|
---|
32 |
|
---|
33 | public:
|
---|
34 | QString getSignature();
|
---|
35 | QString getName()
|
---|
36 | {
|
---|
37 | return m_name;
|
---|
38 | }
|
---|
39 |
|
---|
40 | virtual QString getDataType() = 0;
|
---|
41 |
|
---|
42 | ComponentBase * getComponent()
|
---|
43 | {
|
---|
44 | return m_component;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void addConnection(ConnectionBase connection)
|
---|
48 | {
|
---|
49 | m_connections.append(connection);
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool removeConnection(ConnectionBase connection)
|
---|
53 | {
|
---|
54 | return m_connections.removeOne(connection);
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool hasConnection()
|
---|
58 | {
|
---|
59 | return m_connections.size() > 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected:
|
---|
63 | QList<ConnectionBase> & connections()
|
---|
64 | {
|
---|
65 | return m_connections;
|
---|
66 | }
|
---|
67 |
|
---|
68 | const QList<ConnectionBase> & connections() const
|
---|
69 | {
|
---|
70 | return m_connections;
|
---|
71 | }
|
---|
72 |
|
---|
73 | QString name()
|
---|
74 | {
|
---|
75 | return m_name;
|
---|
76 | }
|
---|
77 |
|
---|
78 | ComponentBase * component()
|
---|
79 | {
|
---|
80 | return m_component;
|
---|
81 | }
|
---|
82 |
|
---|
83 | const ComponentBase * component() const
|
---|
84 | {
|
---|
85 | return m_component;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private:
|
---|
89 | QString m_name;
|
---|
90 | ComponentBase * m_component;
|
---|
91 | QList<ConnectionBase> m_connections;
|
---|
92 | };
|
---|
93 |
|
---|
94 | class PACPUSLIB_API InputInterfaceBase
|
---|
95 | : public AbstractInterface
|
---|
96 | {
|
---|
97 | Q_OBJECT
|
---|
98 | protected:
|
---|
99 | InputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0)
|
---|
100 | : AbstractInterface(name, component, parent)
|
---|
101 | {}
|
---|
102 |
|
---|
103 | public:
|
---|
104 | //InputInterfaceBase(QString name, ComponentBase * component,int a, QObject * parent = 0):AbstractInterface(name,component,parent) {} // TODO add ctor with function pointer
|
---|
105 |
|
---|
106 | enum ReadingMode {
|
---|
107 | NeverSkip,
|
---|
108 | TimeBounded,
|
---|
109 | GetLast
|
---|
110 | };
|
---|
111 |
|
---|
112 | virtual ~InputInterfaceBase()
|
---|
113 | {}
|
---|
114 |
|
---|
115 | virtual void customEvent(QEvent* e)
|
---|
116 | {
|
---|
117 | //if(event->type())
|
---|
118 | //TODO get event Type anf call callback function
|
---|
119 |
|
---|
120 | PacpusEvent * event = dynamic_cast<PacpusEvent *>(e);
|
---|
121 | QByteArray buf;
|
---|
122 | QDataStream out(&buf, QIODevice::WriteOnly);
|
---|
123 | event->streamOut(out);
|
---|
124 | // Callback QByteArray
|
---|
125 | }
|
---|
126 |
|
---|
127 | ReadingMode & readingMode()
|
---|
128 | {
|
---|
129 | return m_readingMode;
|
---|
130 | }
|
---|
131 |
|
---|
132 | const ReadingMode & readingMode() const
|
---|
133 | {
|
---|
134 | return m_readingMode;
|
---|
135 | }
|
---|
136 |
|
---|
137 | void setReadingMode(ReadingMode mode)
|
---|
138 | {
|
---|
139 | m_readingMode = mode;
|
---|
140 | }
|
---|
141 |
|
---|
142 | virtual PacpusEvent* getEventTemplate()
|
---|
143 | {
|
---|
144 | // TODO: check
|
---|
145 | return new PacpusEvent(GENERIC_EVENT);
|
---|
146 | }
|
---|
147 |
|
---|
148 | private:
|
---|
149 | ReadingMode m_readingMode;
|
---|
150 |
|
---|
151 | // metode(QByteArray)
|
---|
152 | //QQueue jobQueue_;
|
---|
153 | };
|
---|
154 |
|
---|
155 | class PACPUSLIB_API OutputInterfaceBase
|
---|
156 | : public AbstractInterface
|
---|
157 | {
|
---|
158 | Q_OBJECT
|
---|
159 |
|
---|
160 | public:
|
---|
161 | OutputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0)
|
---|
162 | : AbstractInterface(name, component, parent)
|
---|
163 | {}
|
---|
164 |
|
---|
165 | virtual ~OutputInterfaceBase()
|
---|
166 | {}
|
---|
167 |
|
---|
168 | QStringList getInputConnectedList()
|
---|
169 | {
|
---|
170 | QStringList list;
|
---|
171 | for(QList<ConnectionBase>::iterator it = connections().begin(); it != connections().end(); ++it) {
|
---|
172 | list.append(it->getInterface()->getName());
|
---|
173 | }
|
---|
174 | return list;
|
---|
175 | }
|
---|
176 |
|
---|
177 | void send(/*const*/ QByteArray & data)
|
---|
178 | {
|
---|
179 | // TODO check at least one Typed connection
|
---|
180 |
|
---|
181 | for(QList<ConnectionBase>::iterator it = connections().begin(); it!=connections().end(); ++it){
|
---|
182 | QDataStream in(&data,QIODevice::ReadOnly);
|
---|
183 | PacpusEvent* event = dynamic_cast<InputInterfaceBase*>(connections().at(0).getInterface())->getEventTemplate();
|
---|
184 | event->streamIn(in);
|
---|
185 | QApplication::postEvent(it->getInterface(),event,it->getPriority());
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | };
|
---|
190 |
|
---|
191 | static bool connectInterface(OutputInterfaceBase* out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode = InputInterfaceBase::GetLast)
|
---|
192 | {
|
---|
193 | if(out->getDataType() == in->getDataType() || out->getDataType() == QString(typeid(QByteArray).name()) || in->getDataType() == QString(typeid(QByteArray).name())) {
|
---|
194 | // Add connection
|
---|
195 | out->addConnection(ConnectionBase(in,priority)); // TODO make connect function
|
---|
196 | in->addConnection(ConnectionBase(out,priority));
|
---|
197 | in->setReadingMode(mode);
|
---|
198 | //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
|
---|
199 | return true;
|
---|
200 | } else {
|
---|
201 | //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " << QString(typeid(QByteArray).name()));
|
---|
202 | return false;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | } // namespace pacpus
|
---|
207 |
|
---|
208 | #endif // IN_OUT_BASE_H
|
---|