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