source: pacpusframework/trunk/include/Pacpus/kernel/PacpusEvent.h@ 255

Last change on this file since 255 was 206, checked in by Marek Kurdej, 11 years ago

Major: cleaned connection interfaces.

  • Property svn:executable set to *
File size: 4.3 KB
RevLine 
[206]1#ifndef PACPUS_EVENT_H
2#define PACPUS_EVENT_H
3
[196]4#include <Pacpus/kernel/PacpusLibConfig.h>
[206]5#include <Pacpus/kernel/road_time.h>
6
7#include <algorithm>
8#include <QDataStream>
9#include <QEvent>
10
11namespace pacpus {
12
13// registerEventType // TODO
14
15// FIXME: move to another header file
16enum PacpusEventType {
17 TYPED_EVENT = 1000,
18 GENERIC_EVENT = 2000,
19 GENERIC_EVENT2 = 2002,
20 GENERIC_EVENT3 = 2003,
21 CONFIG_EVENT = 3000
22};
23
24class 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
29public:
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 }
70protected:
71 road_time_t m_time;
72 road_timerange_t m_timerange;
73};
74
75template <typename T>
76class /*PACPUSLIB_API*/ TypedData
77{
78public:
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 std::size_t size() const
99 {
100 return sizeof(T);
101 }
102
103private:
104 T m_data;
105};
106
107template<class T>
108class /*PACPUSLIB_API*/ PacpusTypedEvent
109 : public PacpusEvent
110 , public TypedData<T>
111{
112public:
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
120 /// Conversion constructor from another PacpusTypedEvent,
121 /// when T is convertible to S
122 template <typename S>
123 PacpusTypedEvent(const PacpusTypedEvent<S> & other)
124 : PacpusEvent(static_cast<PacpusEventType>(other.type()), other.time(), other.timerange())
125 , TypedData<T>(other.data())
126 {
127 }
128
129 ~PacpusTypedEvent()
130 {
131 }
132
133 QDataStream & streamOut(QDataStream & out)
134 {
135 // FIXME Stream Data errors
136 return out << (quint64) time() << timerange() /*<< m_data*/;
137 }
138
139 QDataStream & streamIn(QDataStream & in)
140 {
141 return in >> (quint64&) time() >> timerange() /*>> m_data*/;
142 }
143};
144
145class PACPUSLIB_API GenericData
146{
147public:
148 GenericData(const char * data, size_t size)
149 : m_data(NULL)
150 , m_dataSize(size)
151 {
152 m_data = new char[m_dataSize];
153 ::std::copy(data, data + m_dataSize, // source
154#ifdef _MSC_VER
155 ::stdext::checked_array_iterator<char *>(m_data, m_dataSize) // destination
156#else
157 m_data // destination
158#endif
159 );
160 }
161
162 ~GenericData()
163 {
164 delete[] m_data;
165 }
166
167 char * data()
168 {
169 return m_data;
170 }
171
172 const char * data() const
173 {
174 return m_data;
175 }
176
177 std::size_t size() const
178 {
179 return m_dataSize;
180 }
181
182private:
183 char * m_data;
184 std::size_t m_dataSize;
185};
186
187class PACPUSLIB_API PacpusGenericEvent
188 : public PacpusEvent
189 , public GenericData
190{
191public:
192 PacpusGenericEvent(PacpusEventType type, const char * data, size_t size)
193 : PacpusEvent(type)
194 , GenericData(data, size)
195 {
196 }
197
198 virtual ~PacpusGenericEvent()
199 {
200 }
201};
202
203} // namespace pacpus
204
205PACPUSLIB_API inline QDataStream & operator<<(QDataStream & out, pacpus::PacpusEvent & ev)
206{
207 /*return ev.streamOut(out);*/
208 return out;
209}
210
211PACPUSLIB_API inline QDataStream & operator>>(QDataStream & in, pacpus::PacpusEvent & ev)
212{
213 /*return ev.streamIn(in);*/
214 return in;
215}
216
217#endif // PACPUS_EVENT_H
Note: See TracBrowser for help on using the repository browser.