1 | #ifndef IN_OUT_INTERFACE_H
|
---|
2 | #define IN_OUT_INTERFACE_H
|
---|
3 |
|
---|
4 | #include <Pacpus/kernel/InputOutputBase.h>
|
---|
5 | #include <Pacpus/kernel/Log.h>
|
---|
6 |
|
---|
7 | #include <QApplication>
|
---|
8 | #include <QByteArray>
|
---|
9 | #include <QCoreApplication>
|
---|
10 | #include <typeinfo>
|
---|
11 |
|
---|
12 | namespace pacpus {
|
---|
13 |
|
---|
14 | template <typename T, class C>
|
---|
15 | class InputInterface
|
---|
16 | : public InputInterfaceBase
|
---|
17 | {
|
---|
18 | public:
|
---|
19 | typedef T data_type;
|
---|
20 | typedef C component_type;
|
---|
21 | typedef void (C::*process_data_function_type)(const T &);
|
---|
22 |
|
---|
23 | InputInterface(QString name, C * component, process_data_function_type processMethod)
|
---|
24 | : InputInterfaceBase(name, component, component)
|
---|
25 | , method(processMethod)
|
---|
26 | {}
|
---|
27 |
|
---|
28 | ~InputInterface()
|
---|
29 | {}
|
---|
30 |
|
---|
31 | std::size_t getDataSize() const
|
---|
32 | {
|
---|
33 | return sizeof(T);
|
---|
34 | }
|
---|
35 |
|
---|
36 | const std::type_info & getDataType() const
|
---|
37 | {
|
---|
38 | return typeid(T);
|
---|
39 | }
|
---|
40 |
|
---|
41 | // FIXME: what's the purpose of this function?
|
---|
42 | PacpusEvent * getEventTemplate()
|
---|
43 | {
|
---|
44 | return new PacpusTypedEvent<T>(TYPED_EVENT);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // FIXME: what's the purpose of this function?
|
---|
48 | void customEvent(QEvent * event)
|
---|
49 | {
|
---|
50 | // TODO check component state started
|
---|
51 | //if (_component) get state
|
---|
52 | switch (event->type()) {
|
---|
53 | case TYPED_EVENT:
|
---|
54 | {
|
---|
55 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
56 |
|
---|
57 | LOG_DEBUG("Receiver: " << getSignature());
|
---|
58 |
|
---|
59 | if (typedEvent->timerange() < 500 && readingMode() == TimeBounded) {
|
---|
60 | LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
|
---|
61 | readingMode() = NeverSkip;
|
---|
62 | }
|
---|
63 |
|
---|
64 | switch (readingMode()) {
|
---|
65 | case TimeBounded:
|
---|
66 |
|
---|
67 | if (road_time() - typedEvent->time() > typedEvent->timerange()) {
|
---|
68 | LOG_DEBUG("Data skipped, receiver: " << this->getSignature());
|
---|
69 | break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
73 | break;
|
---|
74 |
|
---|
75 | case GetLast:
|
---|
76 |
|
---|
77 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
78 | // delete all remaining events
|
---|
79 | QCoreApplication::removePostedEvents(this, TYPED_EVENT);
|
---|
80 | break;
|
---|
81 |
|
---|
82 | case NeverSkip:
|
---|
83 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
84 | break;
|
---|
85 |
|
---|
86 | default:
|
---|
87 | LOG_WARN("Unknown reading mode " << readingMode());
|
---|
88 | }
|
---|
89 | break;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Add here new event type if needed
|
---|
93 |
|
---|
94 | default:
|
---|
95 | LOG_WARN("Unknown event ID " << event->type());
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | event->accept();
|
---|
99 | }
|
---|
100 |
|
---|
101 | // TODO for Pulling mode (not yet implemented !!!)
|
---|
102 | T & getData() {
|
---|
103 | T data;
|
---|
104 | // TODO ask output data;
|
---|
105 |
|
---|
106 | return data;
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected:
|
---|
110 | process_data_function_type method;
|
---|
111 | };
|
---|
112 |
|
---|
113 | template <typename T, class C>
|
---|
114 | class OutputInterface
|
---|
115 | : public OutputInterfaceBase
|
---|
116 | {
|
---|
117 | public:
|
---|
118 | typedef T data_type;
|
---|
119 | typedef C component_type;
|
---|
120 |
|
---|
121 | OutputInterface(QString name, C * component)
|
---|
122 | : OutputInterfaceBase(name, component, component)
|
---|
123 | {}
|
---|
124 |
|
---|
125 | ~OutputInterface()
|
---|
126 | {}
|
---|
127 |
|
---|
128 | // Used by Components to send data througth typed output
|
---|
129 | void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0);
|
---|
130 |
|
---|
131 | std::size_t getDataSize() const
|
---|
132 | {
|
---|
133 | return sizeof(T);
|
---|
134 | }
|
---|
135 |
|
---|
136 | const std::type_info & getDataType() const
|
---|
137 | {
|
---|
138 | return typeid(T);
|
---|
139 | }
|
---|
140 | };
|
---|
141 |
|
---|
142 | template <typename T, class C>
|
---|
143 | void OutputInterface<T, C>::send(const T & data, road_time_t t, road_timerange_t tr)
|
---|
144 | {
|
---|
145 | // FIXME Data Shared
|
---|
146 | //QSharedPointer<T> sharedPointer = new T(data);
|
---|
147 |
|
---|
148 | for (QList<ConnectionBase>::iterator it = connections().begin(), itend = connections().end(); it != itend; ++it) {
|
---|
149 | // Event is deleted by the event loop handler
|
---|
150 | QApplication::postEvent(it->getInterface(), new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority());
|
---|
151 | //qDebug() << "sender " << it->getInterface()->getSignature() << " Data & " << &data << " ";
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | } // namespace pacpus
|
---|
156 |
|
---|
157 | #endif // IN_OUT_INTERFACE_H
|
---|