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

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

Minor: fixed warnings.

  • Property svn:executable set to *
File size: 4.3 KB
Line 
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
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 road_time_t const& time() const
57 {
58 return m_time;
59 }
60
61 road_timerange_t& timerange()
62 {
63 return m_timerange;
64 }
65
66 road_timerange_t const& timerange() const
67 {
68 return m_timerange;
69 }
70
71protected:
72 road_time_t m_time;
73 road_timerange_t m_timerange;
74};
75
76template <typename T>
77class /*PACPUSLIB_API*/ TypedData
78{
79public:
80 TypedData(const T & data)
81 : m_data(data)
82 {
83 }
84
85 ~TypedData()
86 {
87 }
88
89 T & data()
90 {
91 return m_data;
92 }
93
94 const T & data() const
95 {
96 return m_data;
97 }
98
99 std::size_t size() const
100 {
101 return sizeof(T);
102 }
103
104private:
105 T m_data;
106};
107
108template<class T>
109class /*PACPUSLIB_API*/ PacpusTypedEvent
110 : public PacpusEvent
111 , public TypedData<T>
112{
113public:
114 // FIXME: why we need `data = T()` ???
115 PacpusTypedEvent(PacpusEventType type, const T & data/* = T()*/, road_time_t t = road_time(), road_timerange_t tr = 0)
116 : PacpusEvent(type, t, tr)
117 , TypedData<T>(data)
118 {
119 }
120
121 /// Conversion constructor from another PacpusTypedEvent,
122 /// when T is convertible to S
123 template <typename S>
124 PacpusTypedEvent(PacpusTypedEvent<S> const& other)
125 : PacpusEvent(static_cast<PacpusEventType>(other.type()), other.time(), other.timerange())
126 , TypedData<T>(other.data())
127 {
128 }
129
130 ~PacpusTypedEvent()
131 {
132 }
133
134 QDataStream & streamOut(QDataStream & out)
135 {
136 // FIXME Stream Data errors
137 return out << (quint64) time() << timerange() /*<< m_data*/;
138 }
139
140 QDataStream & streamIn(QDataStream & in)
141 {
142 return in >> (quint64&) time() >> timerange() /*>> m_data*/;
143 }
144};
145
146class PACPUSLIB_API GenericData
147{
148public:
149 GenericData(char const* data, size_t size)
150 : m_data(NULL)
151 , m_dataSize(size)
152 {
153 m_data = new char[m_dataSize];
154 ::std::copy(data, data + m_dataSize, // source
155#ifdef _MSC_VER
156 ::stdext::checked_array_iterator<char *>(m_data, m_dataSize) // destination
157#else
158 m_data // destination
159#endif
160 );
161 }
162
163 ~GenericData()
164 {
165 delete[] m_data;
166 }
167
168 char * data()
169 {
170 return m_data;
171 }
172
173 const char * data() const
174 {
175 return m_data;
176 }
177
178 std::size_t size() const
179 {
180 return m_dataSize;
181 }
182
183private:
184 char * m_data;
185 std::size_t m_dataSize;
186};
187
188class PACPUSLIB_API PacpusGenericEvent
189 : public PacpusEvent
190 , public GenericData
191{
192public:
193 PacpusGenericEvent(PacpusEventType type, const char * data, size_t size)
194 : PacpusEvent(type)
195 , GenericData(data, size)
196 {
197 }
198
199 virtual ~PacpusGenericEvent()
200 {
201 }
202};
203
204} // namespace pacpus
205
206PACPUSLIB_API inline QDataStream & operator<<(QDataStream & out, pacpus::PacpusEvent& /*ev*/)
207{
208 /*return ev.streamOut(out);*/
209 return out;
210}
211
212PACPUSLIB_API inline QDataStream & operator>>(QDataStream & in, pacpus::PacpusEvent& /*ev*/)
213{
214 /*return ev.streamIn(in);*/
215 return in;
216}
217
218#endif // PACPUS_EVENT_H
Note: See TracBrowser for help on using the repository browser.