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