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