source: pacpusframework/branches/0.1.x/include/Pacpus/kernel/PacpusEvent.h@ 243

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

Fixed: C4996 unchecked iterator warning on MSVC.

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