]> vaikene.ee Git - evaf/blob - src/libs/Common/event.h
44396c1af7bd00dd04982b5a2dd3a6d8eb11da35
[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 /**
69 * Returns the event ID value
70 */
71 inline uint id() const { return mId; }
72
73 /**
74 * Returns the 32-bit integer value
75 */
76 inline quint32 value() const { return mValue; }
77
78 /**
79 * Returns the shared data object attached to the event
80 *
81 * The shared data object is valid only while in the event handler. Once the event loop
82 * is finished, the event and shared data attached to it are destroyed. To keep the shared
83 * data object, use QExplicitlySharedDataPointer<> to claim ownership.
84 */
85 inline QSharedData * data() const { return mData != nullptr ? mData.data() : nullptr; }
86
87
88 private:
89
90 /// Event ID value
91 uint mId;
92
93 /// A 32-bit integer value associated to the event
94 quint32 mValue;
95
96 /// Shared data object attached to the event
97 QExplicitlySharedDataPointer<QSharedData> mData;
98
99 };
100
101 } // namespace eVaf::Common
102 } // namespace eVaf
103
104 #endif // event.h