[89] | 1 | #ifndef IN_OUT_INTERFACE_H
|
---|
| 2 | #define IN_OUT_INTERFACE_H
|
---|
| 3 |
|
---|
[95] | 4 | #include <Pacpus/kernel/Log.h>
|
---|
[89] | 5 | #include <Pacpus/kernel/inputOutputBase.h>
|
---|
| 6 |
|
---|
| 7 | #include <typeinfo>
|
---|
| 8 | #include <QDebug>
|
---|
| 9 | #include <QApplication>
|
---|
| 10 |
|
---|
| 11 | #include <QByteArray>
|
---|
| 12 |
|
---|
| 13 | #define ADD_INPUT(name,ComponentType, DataType, functionName) input.insert(name,new InputInterface<DataType,ComponentType> (name,this,&ComponentType::functionName))
|
---|
| 14 | #define ADD_OUTPUT(name,ComponentType, DataType) output.insert(name,new OutputInterface<DataType,ComponentType> (name,this))
|
---|
| 15 | #define GET_OUTPUT(name,ComponentType, DataType) dynamic_cast<OutputInterface<DataType,ComponentType> *> (output.value(name))
|
---|
| 16 |
|
---|
| 17 | namespace pacpus {
|
---|
| 18 |
|
---|
| 19 | template <class T, class C>
|
---|
| 20 | class InputInterface : public InputInterfaceBase
|
---|
| 21 | {
|
---|
| 22 | public:
|
---|
| 23 | InputInterface(QString name, C * component, void (C::*m)(const T&)):InputInterfaceBase(name,component,component), method(m) {}
|
---|
| 24 | ~InputInterface() {}
|
---|
| 25 |
|
---|
| 26 | enum ReadingMode {
|
---|
| 27 | NeverSkip,
|
---|
| 28 | TimeBounded,
|
---|
| 29 | GetLast
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | void setReadingMode(ReadingMode mode) { readingMode_ = mode;}
|
---|
| 33 | size_t getDataSize() {return sizeof(T);}
|
---|
| 34 | QString getDataType() {return QString(typeid(T).name());}
|
---|
| 35 |
|
---|
| 36 | protected:
|
---|
| 37 | ReadingMode readingMode_;
|
---|
| 38 | int boundingTime_;
|
---|
| 39 |
|
---|
| 40 | public:
|
---|
| 41 |
|
---|
[96] | 42 | PacpusEvent getEventTemplate() {return PacpusTypedEvent<T>(TYPED_EVENT); }
|
---|
| 43 |
|
---|
[89] | 44 | void customEvent(QEvent* event) {
|
---|
| 45 |
|
---|
| 46 | switch (event->type()) {
|
---|
| 47 |
|
---|
| 48 | // from Component to Component (T->T)
|
---|
| 49 | case TYPED_EVENT: {
|
---|
| 50 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
| 51 | //qDebug() << "E1 ";
|
---|
| 52 | //qDebug() << "recived 2 thread " << QThread::currentThread();
|
---|
| 53 | if(typedEvent->tr_ == 0 && readingMode_ == TimeBounded) {
|
---|
| 54 | //LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
|
---|
| 55 | readingMode_ = NeverSkip;}
|
---|
| 56 |
|
---|
| 57 | switch (readingMode_){
|
---|
| 58 | case TimeBounded:
|
---|
| 59 | if(road_time() - typedEvent->t_> typedEvent->tr_)
|
---|
| 60 | break;
|
---|
| 61 | (dynamic_cast<C*>(_component)->*method)(typedEvent->_data);
|
---|
| 62 | break;
|
---|
| 63 | case GetLast:
|
---|
| 64 | (dynamic_cast<C*>(_component)->*method)(typedEvent->_data);
|
---|
| 65 | QCoreApplication::removePostedEvents(this,TYPED_EVENT); // delete all remining events
|
---|
| 66 | break;
|
---|
| 67 | case NeverSkip:
|
---|
| 68 | default:
|
---|
| 69 | (dynamic_cast<C*>(_component)->*method)(typedEvent->_data);
|
---|
| 70 | }
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // from Connection interface to Component (G->T)
|
---|
[96] | 75 | /* case GENERIC_EVENT2: {
|
---|
[89] | 76 | PacpusTypedEvent<QByteArray> * genericEvent = dynamic_cast<PacpusTypedEvent<QByteArray> *> (event);
|
---|
| 77 | T data;
|
---|
| 78 | QByteArray& buf = (QByteArray&) genericEvent->_data;
|
---|
| 79 | QDataStream in(&buf,QIODevice::ReadOnly);
|
---|
| 80 |
|
---|
| 81 | (dynamic_cast<C*>(_component)->*method)(data); // copy 8 X
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // from Component to Connection interface (T->G) (Typed in QByteArray)
|
---|
| 86 | case GENERIC_EVENT3: {
|
---|
| 87 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
| 88 | (dynamic_cast<C*>(_component)->*method)(typedEvent->_data); // copy 3 X
|
---|
[96] | 89 |
|
---|
[89] | 90 | break;
|
---|
[96] | 91 | }*/
|
---|
[89] | 92 |
|
---|
| 93 | default:
|
---|
| 94 |
|
---|
[96] | 95 | qDebug() << "Unknown event ID " << event->type();
|
---|
[89] | 96 | break;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | protected:
|
---|
| 103 | void (C::*method)(const T&);
|
---|
| 104 |
|
---|
| 105 | };
|
---|
| 106 |
|
---|
| 107 | template <class T, class C>
|
---|
| 108 | class OutputInterface : public OutputInterfaceBase
|
---|
| 109 | {
|
---|
| 110 | public:
|
---|
| 111 | OutputInterface(QString name, C * component):OutputInterfaceBase(name,component,component) {}
|
---|
| 112 | ~OutputInterface() {}
|
---|
| 113 |
|
---|
| 114 | // Used by Components to send data througth typed output
|
---|
| 115 | void send(const T & data) {
|
---|
| 116 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it){
|
---|
| 117 |
|
---|
[96] | 118 | //if(it->getInterface()->getDataType() != QString(typeid(QByteArray).name()))
|
---|
| 119 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data),it->getPriority());
|
---|
| 120 | /*else {
|
---|
[89] | 121 | QByteArray buf;
|
---|
| 122 | QDataStream out(&buf,QIODevice::ReadWrite);
|
---|
[96] | 123 | PacpusTypedEvent<QByteArray> * ev =new PacpusTypedEvent<QByteArray>(GENERIC_EVENT3,buf);
|
---|
| 124 | QApplication::postEvent(it->getInterface(),ev,it->getPriority()); // Copy 2 (ctor)
|
---|
| 125 | }*/
|
---|
[89] | 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // Used by Connection Interfaces only to pose generic event
|
---|
[96] | 130 | /* void sendGenericData(char * data, size_t size) {
|
---|
[89] | 131 | QByteArray buf(data,size); // copy 5
|
---|
| 132 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
|
---|
[96] | 133 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<QByteArray>(GENERIC_EVENT2,buf),it->getPriority()); // Copy 6(ctor)
|
---|
| 134 |
|
---|
| 135 |
|
---|
[89] | 136 | }
|
---|
[96] | 137 | */
|
---|
[89] | 138 | size_t getDataSize() {return sizeof(T);}
|
---|
| 139 | QString getDataType() {return QString(typeid(T).name());}
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | } // namespace pacpus
|
---|
| 144 |
|
---|
| 145 | #endif // IN_OUT_INTERFACE_H
|
---|