1 | #ifndef PACPUS_EVENT_H
|
---|
2 | #define PACPUS_EVENT_H
|
---|
3 |
|
---|
4 | #include <Pacpus/kernel/PacpusLibConfig.h>
|
---|
5 | #include <Pacpus/kernel/road_time.h>
|
---|
6 |
|
---|
7 | #include <algorithm>
|
---|
8 | #include <QDataStream>
|
---|
9 | #include <QEvent>
|
---|
10 |
|
---|
11 | namespace pacpus {
|
---|
12 |
|
---|
13 | // registerEventType // TODO
|
---|
14 |
|
---|
15 | // FIXME: move to another header file
|
---|
16 | enum PacpusEventType {
|
---|
17 | TYPED_EVENT = 1000,
|
---|
18 | GENERIC_EVENT = 2000,
|
---|
19 | GENERIC_EVENT2 = 2002,
|
---|
20 | GENERIC_EVENT3 = 2003,
|
---|
21 | CONFIG_EVENT = 3000
|
---|
22 | };
|
---|
23 |
|
---|
24 | class PACPUSLIB_API PacpusEvent
|
---|
25 | : public QEvent
|
---|
26 | {
|
---|
27 | Q_GADGET // permits to use signals and slots (using Q_SIGNAL, Q_SLOT) without inheriting from QObject
|
---|
28 |
|
---|
29 | public:
|
---|
30 | PacpusEvent(PacpusEventType type, road_time_t t = road_time(), road_timerange_t tr = 0)
|
---|
31 | : QEvent(QEvent::Type(type))
|
---|
32 | , m_time(t)
|
---|
33 | , m_timerange(tr)
|
---|
34 | {}
|
---|
35 |
|
---|
36 | virtual ~PacpusEvent()
|
---|
37 | {}
|
---|
38 |
|
---|
39 | // TODO: should we make it virtual pure ???
|
---|
40 | virtual QDataStream& streamOut(QDataStream& out)
|
---|
41 | {
|
---|
42 | return out;
|
---|
43 | }
|
---|
44 |
|
---|
45 | // TODO: should we make it virtual pure ???
|
---|
46 | virtual QDataStream& streamIn(QDataStream& in)
|
---|
47 | {
|
---|
48 | return in;
|
---|
49 | }
|
---|
50 |
|
---|
51 | road_time_t & time()
|
---|
52 | {
|
---|
53 | return m_time;
|
---|
54 | }
|
---|
55 |
|
---|
56 | const road_time_t & time() const
|
---|
57 | {
|
---|
58 | return m_time;
|
---|
59 | }
|
---|
60 |
|
---|
61 | road_timerange_t & timerange()
|
---|
62 | {
|
---|
63 | return m_timerange;
|
---|
64 | }
|
---|
65 |
|
---|
66 | const road_timerange_t & timerange() const
|
---|
67 | {
|
---|
68 | return m_timerange;
|
---|
69 | }
|
---|
70 | protected:
|
---|
71 | road_time_t m_time;
|
---|
72 | road_timerange_t m_timerange;
|
---|
73 | };
|
---|
74 |
|
---|
75 | template <typename T>
|
---|
76 | class /*PACPUSLIB_API*/ TypedData
|
---|
77 | {
|
---|
78 | public:
|
---|
79 | TypedData(const T& data)
|
---|
80 | : m_data(data)
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | ~TypedData()
|
---|
85 | {
|
---|
86 | }
|
---|
87 |
|
---|
88 | T & data()
|
---|
89 | {
|
---|
90 | return m_data;
|
---|
91 | }
|
---|
92 |
|
---|
93 | const T & data() const
|
---|
94 | {
|
---|
95 | return m_data;
|
---|
96 | }
|
---|
97 |
|
---|
98 | size_t size() const
|
---|
99 | {
|
---|
100 | return sizeof(T);
|
---|
101 | }
|
---|
102 |
|
---|
103 | private:
|
---|
104 | T m_data;
|
---|
105 | };
|
---|
106 |
|
---|
107 | template<class T>
|
---|
108 | class /*PACPUSLIB_API*/ PacpusTypedEvent
|
---|
109 | : public PacpusEvent
|
---|
110 | , public TypedData<T>
|
---|
111 | {
|
---|
112 | public:
|
---|
113 | // FIXME: why we need `data = T()` ???
|
---|
114 | PacpusTypedEvent(PacpusEventType type, const T & data = T(), road_time_t t = road_time(), road_timerange_t tr = 0 )
|
---|
115 | : PacpusEvent(type, t, tr)
|
---|
116 | , TypedData<T>(data)
|
---|
117 | {}
|
---|
118 |
|
---|
119 | ~PacpusTypedEvent()
|
---|
120 | {}
|
---|
121 |
|
---|
122 | QDataStream& streamOut(QDataStream& out)
|
---|
123 | {
|
---|
124 | // FIXME Stream Data errors
|
---|
125 | return out << (quint64) time() << timerange() /*<< m_data*/;
|
---|
126 | }
|
---|
127 |
|
---|
128 | QDataStream& streamIn(QDataStream& in)
|
---|
129 | {
|
---|
130 | return in >> (quint64&) time() >> timerange() /*>> m_data*/;
|
---|
131 | }
|
---|
132 | };
|
---|
133 |
|
---|
134 | class PACPUSLIB_API GenericData
|
---|
135 | {
|
---|
136 | public:
|
---|
137 | GenericData(const char* data, size_t size)
|
---|
138 | : m_data(NULL)
|
---|
139 | , m_dataSize(size)
|
---|
140 | {
|
---|
141 | m_data = new char[m_dataSize];
|
---|
142 | ::std::copy(data, data + m_dataSize, // source
|
---|
143 | #ifdef _MSC_VER
|
---|
144 | ::stdext::checked_array_iterator<char *>(m_data, m_dataSize) // destination
|
---|
145 | #else
|
---|
146 | m_data // destination
|
---|
147 | #endif
|
---|
148 | );
|
---|
149 | }
|
---|
150 |
|
---|
151 | ~GenericData()
|
---|
152 | {
|
---|
153 | delete[] m_data;
|
---|
154 | }
|
---|
155 |
|
---|
156 | char * data()
|
---|
157 | {
|
---|
158 | return m_data;
|
---|
159 | }
|
---|
160 |
|
---|
161 | const char * data() const
|
---|
162 | {
|
---|
163 | return m_data;
|
---|
164 | }
|
---|
165 |
|
---|
166 | size_t size() const
|
---|
167 | {
|
---|
168 | return m_dataSize;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private:
|
---|
172 | char* m_data;
|
---|
173 | size_t m_dataSize;
|
---|
174 | };
|
---|
175 |
|
---|
176 | class PACPUSLIB_API PacpusGenericEvent
|
---|
177 | : public PacpusEvent
|
---|
178 | , public GenericData
|
---|
179 | {
|
---|
180 | public:
|
---|
181 | PacpusGenericEvent(PacpusEventType type, const char* data, size_t size)
|
---|
182 | : PacpusEvent(type)
|
---|
183 | , GenericData(data, size)
|
---|
184 | {
|
---|
185 | }
|
---|
186 |
|
---|
187 | virtual ~PacpusGenericEvent()
|
---|
188 | {
|
---|
189 | }
|
---|
190 | };
|
---|
191 |
|
---|
192 | } // namespace pacpus
|
---|
193 |
|
---|
194 | PACPUSLIB_API inline QDataStream& operator<< (QDataStream& out, pacpus::PacpusEvent& ev)
|
---|
195 | {
|
---|
196 | /*return ev.streamOut(out);*/
|
---|
197 | return out;
|
---|
198 | }
|
---|
199 |
|
---|
200 | PACPUSLIB_API inline QDataStream& operator>> (QDataStream& in, pacpus::PacpusEvent& ev)
|
---|
201 | {
|
---|
202 | /*return ev.streamIn(in);*/
|
---|
203 | return in;
|
---|
204 | }
|
---|
205 |
|
---|
206 | #endif // PACPUS_EVENT_H
|
---|