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 <typeinfo>
|
---|
10 | #include <QList>
|
---|
11 | #include <QStringList>
|
---|
12 |
|
---|
13 | namespace pacpus {
|
---|
14 |
|
---|
15 | class ComponentBase;
|
---|
16 |
|
---|
17 | class PACPUSLIB_API AbstractInterface : public QObject
|
---|
18 | {
|
---|
19 | Q_OBJECT
|
---|
20 | protected:
|
---|
21 | AbstractInterface(QString name, ComponentBase * component, QObject * parent = 0):_name(name),_component(component),QObject(parent) {}
|
---|
22 | ~AbstractInterface(){}
|
---|
23 | public:
|
---|
24 | QString getSignature();
|
---|
25 | QString getName() {return _name;}
|
---|
26 | virtual QString getDataType() = 0;
|
---|
27 | ComponentBase * getComponent() {return _component;}
|
---|
28 |
|
---|
29 | void addConnection(ConnectionBase connection) { _connection.append(connection);}
|
---|
30 | bool removeConnection(ConnectionBase connection) { return _connection.removeOne(connection);}
|
---|
31 |
|
---|
32 | bool hasConnection() { return _connection.size() > 0;}
|
---|
33 |
|
---|
34 | protected:
|
---|
35 | QString _name;
|
---|
36 | ComponentBase * _component;
|
---|
37 | QList<ConnectionBase> _connection;
|
---|
38 | };
|
---|
39 |
|
---|
40 | class PACPUSLIB_API InputInterfaceBase : public AbstractInterface
|
---|
41 | {
|
---|
42 | Q_OBJECT
|
---|
43 | protected:
|
---|
44 | InputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0):AbstractInterface(name,component,parent) {}
|
---|
45 |
|
---|
46 | public:
|
---|
47 | //InputInterfaceBase(QString name, ComponentBase * component,int a, QObject * parent = 0):AbstractInterface(name,component,parent) {} // TODO add ctor with function pointer
|
---|
48 |
|
---|
49 | virtual ~InputInterfaceBase(){}
|
---|
50 |
|
---|
51 | virtual void customEvent(QEvent* e) {
|
---|
52 |
|
---|
53 | //if(event->type())
|
---|
54 | //TODO get event Type anf call callback function
|
---|
55 |
|
---|
56 | QByteArray buf;
|
---|
57 | QDataStream out(&buf,QIODevice::WriteOnly);
|
---|
58 | out << e;
|
---|
59 | // Callback QByteArray
|
---|
60 | }
|
---|
61 |
|
---|
62 | virtual PacpusEvent getEventTemplate() {return PacpusEvent(GENERIC_EVENT);} // TODO check ??
|
---|
63 |
|
---|
64 | };
|
---|
65 |
|
---|
66 | class PACPUSLIB_API OutputInterfaceBase: public AbstractInterface
|
---|
67 | {
|
---|
68 | Q_OBJECT
|
---|
69 |
|
---|
70 | public:
|
---|
71 | OutputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0):AbstractInterface(name,component,parent) {}
|
---|
72 |
|
---|
73 | virtual ~OutputInterfaceBase(){}
|
---|
74 |
|
---|
75 | QStringList getInputConnectedList() {
|
---|
76 | QStringList list;
|
---|
77 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
|
---|
78 | list.append(it->getInterface()->getName());
|
---|
79 | return list;
|
---|
80 | }
|
---|
81 |
|
---|
82 | void send(/*const*/ QByteArray & data) {
|
---|
83 |
|
---|
84 | // TODo getEvent
|
---|
85 | //PacpusEvent& event = _connection.at(0).getInterface()->
|
---|
86 | QDataStream in(&data,QIODevice::ReadOnly);
|
---|
87 | //in >> event;
|
---|
88 |
|
---|
89 | //for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
|
---|
90 | // QApplication::postEvent(it->getInterface(),&event,it->getPriority());
|
---|
91 |
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | };
|
---|
96 |
|
---|
97 | static bool connectInterface(OutputInterfaceBase* out, InputInterfaceBase * in, int priority)
|
---|
98 | {
|
---|
99 | if(out->getDataType() == in->getDataType() || out->getDataType() == QString(typeid(QByteArray).name()) || in->getDataType() == QString(typeid(QByteArray).name())) {
|
---|
100 |
|
---|
101 | // Add connection
|
---|
102 | out->addConnection(ConnectionBase(in,priority)); // TODO make connect function
|
---|
103 | in->addConnection(ConnectionBase(out,priority));
|
---|
104 | //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
|
---|
105 | return true;
|
---|
106 | } else {
|
---|
107 | //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " << QString(typeid(QByteArray).name()));
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | } // namespace pacpus
|
---|
113 |
|
---|
114 | #endif // IN_OUT_BASE_H
|
---|