source: pacpusframework/branches/0.1.x/include/Pacpus/kernel/InputOutputInterface.h@ 356

Last change on this file since 356 was 159, checked in by morasjul, 11 years ago

Somme cleaning in InputOutputInterface.h and InputOutputBase.h

  • Property svn:executable set to *
File size: 3.6 KB
Line 
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#include <QApplication>
7#include <QByteArray>
8#include <QThread>
9#include <typeinfo>
10
11namespace pacpus {
12
13template <typename T, class C>
14class InputInterface
15 : public InputInterfaceBase
16{
17public:
18 InputInterface(QString name, C * component, void (C::*m)(const T&))
19 : InputInterfaceBase(name, component, component)
20 , method(m)
21 {}
22
23 ~InputInterface()
24 {}
25
26 size_t getDataSize()
27 {
28 return sizeof(T);
29 }
30
31 QString getDataType()
32 {
33 return QString(typeid(T).name());
34 }
35
36 PacpusEvent* getEventTemplate()
37 {
38 return new PacpusTypedEvent<T>(TYPED_EVENT);
39 }
40
41 void customEvent(QEvent* event)
42 {
43 // TODO check component state started
44 //if(_component) get state
45 switch (event->type()) {
46 case TYPED_EVENT:
47 {
48
49 PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
50
51 LOG_DEBUG("Receiver " << getSignature() << " thread " << QThread::currentThread());
52
53
54
55 if (typedEvent->timerange() < 500 && readingMode() == TimeBounded) {
56 LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
57 readingMode() = NeverSkip;
58 }
59
60 switch (readingMode()) {
61 case TimeBounded:
62
63 if (road_time() - typedEvent->time() > typedEvent->timerange()) {
64 LOG_DEBUG("Data skip " << this->getSignature());
65 break;
66 }
67
68 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
69 break;
70
71 case GetLast:
72
73 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
74 // delete all remaining events
75 QCoreApplication::removePostedEvents(this, TYPED_EVENT);
76 break;
77
78 case NeverSkip:
79 (dynamic_cast<C*>(component())->*method)(typedEvent->data());
80 break;
81
82 default:
83 LOG_WARN("Unknown reading mode " << readingMode());
84 }
85 break;
86 }
87
88 // Add here new event type if needed
89
90 default:
91 LOG_WARN("Unknown event ID " << event->type());
92 break;
93 }
94 event->accept();
95 }
96
97 // TODO for Pulling mode (not yet implemented !!!)
98 T& getData() {
99 T data;
100 // TODO ask output data;
101
102 return data;
103 }
104
105protected:
106 void (C::*method)(const T&);
107
108};
109
110template <typename T, class C>
111class OutputInterface : public OutputInterfaceBase
112{
113public:
114 OutputInterface(QString name, C * component):OutputInterfaceBase(name,component,component) {}
115 ~OutputInterface() {}
116
117 // Used by Components to send data througth typed output
118 void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0)
119 {
120 // FIXME Data Shared
121 //QSharedPointer<T> sharedPointer = new T(data);
122
123 for (QList<ConnectionBase>::iterator it = connections().begin(); it != connections().end(); ++it) {
124 QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority()); // Event is delete by the event loop handler
125 //qDebug() << "sender " << it->getInterface()->getSignature() << " thread " << QThread::currentThread() << " Data & " << &data << " ";
126 }
127 }
128
129 size_t getDataSize()
130 {
131 return sizeof(T);
132 }
133
134 QString getDataType()
135 {
136 return QString(typeid(T).name());
137 }
138};
139
140
141} // namespace pacpus
142
143#endif // IN_OUT_INTERFACE_H
Note: See TracBrowser for help on using the repository browser.