]> vaikene.ee Git - evaf/blob - src/libs/Common/event.h
Warning fixes and copyright update.
[evaf] / src / libs / Common / event.h
1 /**
2 * @file Common/event.h
3 * @brief Base class for all the events
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __COMMON_EVENT_H
21 #define __COMMON_EVENT_H
22
23 #include "libcommon.h"
24
25 #include <QObject>
26 #include <QEvent>
27 #include <QSharedData>
28 #include <QExplicitlySharedDataPointer>
29
30 namespace eVaf {
31 namespace Common {
32
33 /**
34 * Event class for all the eVaf events
35 * @code@include <Common/Event>@endcode
36 *
37 * The Event class is an event container for all the eVaf events.
38 */
39 class COMMON_EXPORT Event : public QEvent
40 {
41 public:
42
43 /// Event type for eVaf events
44 static QEvent::Type const eVafEvent;
45
46 /**
47 * Creates the event object
48 * @param eventId The ID of the event
49 * @param intValue A 32-bit integer value
50 * @param dataObj Shared data object attached to the event
51 *
52 * Creates the event object with the given event ID value and attaches optional data to the event.
53 *
54 * The optional 32-bit integer value can be used to store information that fits into the value and
55 * is not reference-counted.
56 *
57 * The optional reference-counted shared data object is owned by the event and the reference counter
58 * is decreased by 1 when the event object is destroyed. If nobody else claimed the ownership, then
59 * the shared data object is destroyed too.
60 */
61 Event(uint eventId, quint32 intValue = 0, QSharedData * dataObj = nullptr)
62 : QEvent(eVafEvent)
63 , mId(eventId)
64 , mValue(intValue)
65 , mData(dataObj)
66 {}
67
68 virtual ~Event();
69
70 /**
71 * Returns the event ID value
72 */
73 inline uint id() const { return mId; }
74
75 /**
76 * Returns the 32-bit integer value
77 */
78 inline quint32 value() const { return mValue; }
79
80 /**
81 * Returns the shared data object attached to the event
82 *
83 * The shared data object is valid only while in the event handler. Once the event loop
84 * is finished, the event and shared data attached to it are destroyed. To keep the shared
85 * data object, use QExplicitlySharedDataPointer<> to claim ownership.
86 */
87 inline QSharedData * data() const { return mData != nullptr ? mData.data() : nullptr; }
88
89
90 private:
91
92 /// Event ID value
93 uint mId;
94
95 /// A 32-bit integer value associated to the event
96 quint32 mValue;
97
98 /// Shared data object attached to the event
99 QExplicitlySharedDataPointer<QSharedData> mData;
100
101 };
102
103 } // namespace eVaf::Common
104 } // namespace eVaf
105
106 #endif // event.h