1 /*
2   enumdefinition.h
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2016-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_ENUMDEFINITION_H
30 #define GAMMARAY_ENUMDEFINITION_H
31 
32 #include "gammaray_common_export.h"
33 
34 #include "enumvalue.h"
35 
36 #include <QVector>
37 
38 namespace GammaRay {
39 
40 /*! A single element of an EnumDefinition. */
41 class GAMMARAY_COMMON_EXPORT EnumDefinitionElement
42 {
43 public:
44     EnumDefinitionElement() = default;
45     /*! Create a new enum definition element with name @p name and value @p value. */
46     EnumDefinitionElement(int value, const char *name);
47 
48     /*! The numeric value represented of this enum definition element. */
49     int value() const;
50     /*! The name of this enum definition element. */
51     QByteArray name() const;
52 
53 private:
54     friend QDataStream &operator<<(QDataStream &out, const EnumDefinitionElement &elem);
55     friend QDataStream &operator>>(QDataStream &in, EnumDefinitionElement &elem);
56 
57     int m_value = 0;
58     QByteArray m_name;
59 };
60 
61 /*! Target-independent representation of an enum or flag definition. */
62 class GAMMARAY_COMMON_EXPORT EnumDefinition
63 {
64 public:
65     EnumDefinition() = default;
66     /*! Create a new definition for an enum named @p name and internal id @p id. */
67     explicit EnumDefinition(EnumId id, const QByteArray &name);
68 
69     /*! Returns whether this is a valid enum definition.
70      * This means it's a enum definition registered with the EnumRepository
71      * and there is at least one enum definition element present.
72      */
73     bool isValid() const;
74 
75     /*! A unique identifier for this enum definition.
76      * This is used to identify enums between the GammaRay client and server.
77      */
78     EnumId id() const;
79     /*! The name of the enum. */
80     QByteArray name() const;
81     /*! Returns @c true if this enum is used as a flag. */
82     bool isFlag() const;
83 
84     /*! Returns the individual elements of this enum.
85      *  That is, the key/value pairs of its definition.
86      */
87     QVector<EnumDefinitionElement> elements() const;
88 
89     //! @cond internal
90     void setElements(const QVector<EnumDefinitionElement> &elements);
91     void setIsFlag(bool isFlag);
92     //! @endcond
93 
94     /*! Converts the given enum value into a string representation. */
95     QByteArray valueToString(const EnumValue &value) const;
96 
97 private:
98     friend GAMMARAY_COMMON_EXPORT QDataStream &operator<<(QDataStream &out, const EnumDefinition &def);
99     friend GAMMARAY_COMMON_EXPORT QDataStream &operator>>(QDataStream &in, EnumDefinition &def);
100 
101     EnumId m_id = InvalidEnumId;
102     bool m_isFlag = false;
103     QByteArray m_name;
104     QVector<EnumDefinitionElement> m_elements;
105 };
106 
107 ///@cond internal
108 GAMMARAY_COMMON_EXPORT QDataStream &operator<<(QDataStream &out, const EnumDefinition &def);
109 GAMMARAY_COMMON_EXPORT QDataStream &operator>>(QDataStream &in, EnumDefinition &def);
110 ///@endcond
111 
112 }
113 
114 Q_DECLARE_METATYPE(GammaRay::EnumDefinition)
115 QT_BEGIN_NAMESPACE
116 Q_DECLARE_TYPEINFO(GammaRay::EnumDefinitionElement, Q_MOVABLE_TYPE);
117 Q_DECLARE_TYPEINFO(GammaRay::EnumDefinition, Q_MOVABLE_TYPE);
118 QT_END_NAMESPACE
119 
120 #endif // GAMMARAY_ENUMDEFINITION_H
121