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 | // TODO for serealization prupose (not yet implemented !!!)
|
---|
120 | virtual void customEvent(QEvent* e)
|
---|
121 | {
|
---|
122 | //if(event->type())
|
---|
123 | //TODO get event Type anf call callback function
|
---|
124 |
|
---|
125 | PacpusEvent * event = dynamic_cast<PacpusEvent *>(e);
|
---|
126 | QByteArray buf;
|
---|
127 | QDataStream out(&buf, QIODevice::WriteOnly);
|
---|
128 | event->streamOut(out);
|
---|
129 | // Callback QByteArray
|
---|
130 | }
|
---|
131 |
|
---|
132 | ReadingMode & readingMode()
|
---|
133 | {
|
---|
134 | return m_readingMode;
|
---|
135 | }
|
---|
136 |
|
---|
137 | const ReadingMode & readingMode() const
|
---|
138 | {
|
---|
139 | return m_readingMode;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void setReadingMode(ReadingMode mode)
|
---|
143 | {
|
---|
144 | m_readingMode = mode;
|
---|
145 | }
|
---|
146 |
|
---|
147 | virtual PacpusEvent* getEventTemplate()
|
---|
148 | {
|
---|
149 | // TODO: check
|
---|
150 | return new PacpusEvent(GENERIC_EVENT);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private:
|
---|
154 | ReadingMode m_readingMode;
|
---|
155 |
|
---|
156 | // metode(QByteArray)
|
---|
157 | //QQueue jobQueue_;
|
---|
158 | };
|
---|
159 |
|
---|
160 | class PACPUSLIB_API OutputInterfaceBase
|
---|
161 | : public AbstractInterface
|
---|
162 | {
|
---|
163 | Q_OBJECT
|
---|
164 |
|
---|
165 | public:
|
---|
166 | OutputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0)
|
---|
167 | : AbstractInterface(name, component, parent)
|
---|
168 | {}
|
---|
169 |
|
---|
170 | virtual ~OutputInterfaceBase()
|
---|
171 | {}
|
---|
172 |
|
---|
173 | QStringList getInputConnectedList()
|
---|
174 | {
|
---|
175 | QStringList list;
|
---|
176 | for(QList<ConnectionBase>::iterator it = connections().begin(); it != connections().end(); ++it) {
|
---|
177 | list.append(it->getInterface()->getName());
|
---|
178 | }
|
---|
179 | return list;
|
---|
180 | }
|
---|
181 |
|
---|
182 | // TODO for serealization prupose (not yet implemented !!!)
|
---|
183 | void send(/*const*/ QByteArray & data)
|
---|
184 | {
|
---|
185 | // TODO check at least one Typed connection
|
---|
186 |
|
---|
187 | for(QList<ConnectionBase>::iterator it = connections().begin(); it!=connections().end(); ++it){
|
---|
188 | QDataStream in(&data,QIODevice::ReadOnly);
|
---|
189 | PacpusEvent* event = dynamic_cast<InputInterfaceBase*>(connections().at(0).getInterface())->getEventTemplate();
|
---|
190 | event->streamIn(in);
|
---|
191 | QApplication::postEvent(it->getInterface(),event,it->getPriority());
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | };
|
---|
196 |
|
---|
197 | static bool connectInterface(OutputInterfaceBase* out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode = InputInterfaceBase::GetLast)
|
---|
198 | {
|
---|
199 | if(out->getDataType() == in->getDataType() || out->getDataType() == QString(typeid(QByteArray).name()) || in->getDataType() == QString(typeid(QByteArray).name())) {
|
---|
200 | // Add connection
|
---|
201 | out->addConnection(ConnectionBase(in,priority)); // TODO make connect function
|
---|
202 | in->addConnection(ConnectionBase(out,priority));
|
---|
203 | in->setReadingMode(mode);
|
---|
204 | //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
|
---|
205 | return true;
|
---|
206 | } else {
|
---|
207 | //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " << QString(typeid(QByteArray).name()));
|
---|
208 | return false;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | } // namespace pacpus
|
---|
213 |
|
---|
214 | #endif // IN_OUT_BASE_H
|
---|