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