1 /*
2   enumdefinition.cpp
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 #include "enumdefinition.h"
30 
31 using namespace GammaRay;
32 
EnumDefinitionElement(int value,const char * name)33 EnumDefinitionElement::EnumDefinitionElement(int value, const char *name)
34     : m_value(value)
35     , m_name(name)
36 {
37 }
38 
name() const39 QByteArray EnumDefinitionElement::name() const
40 {
41     return m_name;
42 }
43 
value() const44 int EnumDefinitionElement::value() const
45 {
46     return m_value;
47 }
48 
49 namespace GammaRay {
operator <<(QDataStream & out,const EnumDefinitionElement & elem)50 QDataStream& operator<<(QDataStream &out, const EnumDefinitionElement &elem)
51 {
52     out << elem.m_value << elem.m_name;
53     return out;
54 }
55 
operator >>(QDataStream & in,EnumDefinitionElement & elem)56 QDataStream& operator>>(QDataStream &in, EnumDefinitionElement &elem)
57 {
58     in >> elem.m_value >> elem.m_name;
59     return in;
60 }
61 }
62 
EnumDefinition(EnumId id,const QByteArray & name)63 EnumDefinition::EnumDefinition(EnumId id, const QByteArray &name)
64     : m_id(id)
65     , m_name(name)
66 {
67 }
68 
isValid() const69 bool EnumDefinition::isValid() const
70 {
71     return m_id != InvalidEnumId && !m_name.isEmpty() && !m_elements.isEmpty();
72 }
73 
id() const74 EnumId EnumDefinition::id() const
75 {
76     return m_id;
77 }
78 
name() const79 QByteArray EnumDefinition::name() const
80 {
81     return m_name;
82 }
83 
isFlag() const84 bool EnumDefinition::isFlag() const
85 {
86     return m_isFlag;
87 }
88 
setIsFlag(bool isFlag)89 void EnumDefinition::setIsFlag(bool isFlag)
90 {
91     m_isFlag = isFlag;
92 }
93 
elements() const94 QVector<EnumDefinitionElement> EnumDefinition::elements() const
95 {
96     return m_elements;
97 }
98 
setElements(const QVector<EnumDefinitionElement> & elements)99 void EnumDefinition::setElements(const QVector<EnumDefinitionElement>& elements)
100 {
101     m_elements = elements;
102 }
103 
valueToString(const EnumValue & value) const104 QByteArray EnumDefinition::valueToString(const EnumValue& value) const
105 {
106     Q_ASSERT(value.id() == id());
107     if (isFlag()) {
108         QByteArray r;
109         int handledFlags = 0;
110         for (const auto &elem : m_elements) {
111             if ((elem.value() & value.value()) == elem.value() && elem.value() != 0) {
112                 r += elem.name() + '|';
113                 handledFlags |= elem.value();
114             }
115         }
116         if (value.value() & ~handledFlags)
117             r += "flag 0x" + QByteArray::number(value.value() & ~handledFlags, 16) + '|';
118 
119         if (!r.isEmpty()) {
120             r.chop(1);
121         } else {
122             // check for dedicated 0-values
123             Q_ASSERT(value.value() == 0);
124             for (const auto &elem : m_elements) {
125                 if (elem.value() == 0)
126                     return elem.name();
127             }
128             return "<none>";
129         }
130         return r;
131     } else {
132         for (const auto &elem : m_elements) {
133             if (elem.value() == value.value())
134                 return elem.name();
135         }
136         return "unknown (" + QByteArray::number(value.value()) + ')';
137     }
138 }
139 
140 namespace GammaRay {
operator <<(QDataStream & out,const EnumDefinition & def)141 QDataStream& operator<<(QDataStream &out, const EnumDefinition &def)
142 {
143     out << def.m_id << def.m_isFlag << def.m_name << def.m_elements;
144     return out;
145 }
146 
operator >>(QDataStream & in,EnumDefinition & def)147 QDataStream& operator>>(QDataStream &in, EnumDefinition &def)
148 {
149     in >>  def.m_id >> def.m_isFlag >> def.m_name >> def.m_elements;
150     return in;
151 }
152 
153 }
154