• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • datatypes
  • primitive
flagdatainformation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2011 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 
24 #include "flagdatainformation.h"
25 #include <QVarLengthArray>
26 #include <KLocalizedString>
27 
28 
29 
30 FlagDataInformation::FlagDataInformation(const QString& name, PrimitiveDataInformation* type,
31  EnumDefinition::Ptr enumDef, DataInformation* parent)
32  : EnumDataInformation(name, type, enumDef, parent)
33 {
34  Q_ASSERT_X(type->type() != Type_Double && type->type() != Type_Float && type->type() != Type_Invalid,
35  "FlagDataInformation::FlagDataInformation", "Bitflags only work with integers!");
36 }
37 
38 typedef QPair<QString, quint64> FlagPair;
39 
40 template<typename T, int len>
41 static void removeFromArray(QVarLengthArray<T, len>& array, int index) {
42  Q_ASSERT(index >= 0 && index < array.size());
43  int max = array.size() - 1;
44  for (int i = index; i < max; ++i) {
45  array[i] = array[i + 1];
46  }
47  array.removeLast();
48 }
49 
50 QString FlagDataInformation::valueStringImpl() const
51 {
52  Q_ASSERT(mWasAbleToRead);
53  QMapIterator<AllPrimitiveTypes, QString> iter(mEnum->values());
54  //I doubt more than 10 flags will be set very often -> only then do we need a malloc
55  QVarLengthArray<FlagPair, 10> arr;
56  const quint64 value = mValue->value().value<quint64>();
57  while(iter.hasNext()) {
58  iter.next();
59  const quint64 flag = iter.key().value<quint64>();
60  if ((value & flag) == flag)
61  {
62  //flag is set
63  arr.append(qMakePair(iter.value(), flag));
64  }
65  }
66 
67  //now we have all flags, check if some overlap
68  for (int i = 0; i < arr.size(); ++i)
69  {
70  const quint64 firstFlag = arr.at(i).second;
71  for (int j = 0; j < arr.size();)
72  {
73  if (j == i)
74  {
75  j++;
76  continue;
77  }
78  //check if they overlap
79  quint64 secondFlag = arr.at(j).second;
80  if ((firstFlag & secondFlag) == secondFlag)
81  {
82  //they overlap, remove the second flag
83  removeFromArray(arr, j);
84  if (j < i)
85  i--; // i was pushed back by one as well
86  }
87  else
88  j++;
89  }
90  }
91 
92  //if array has zero elements just return the value in hexadecimal
93  if (arr.size() == 0)
94  {
95  return i18n("0x%1 (no matching flags)", QString::number(value, 16));
96  }
97 
98 
99  //make sure all we also show remaining bits at the end
100  quint64 usedBits = 0;
101  QString result;
102  for (int i = 0, size = arr.size(); i < size; ++i)
103  {
104  if (i != 0)
105  result += QLatin1String(" | ");
106  result += arr.at(i).first;
107  usedBits |= arr.at(i).second;
108  }
109  //TODO remove a NONE flag if others present (value = 0)
110 
111  //TODO set invalid if value not completely covered by flags
112 
113  if (usedBits != value)
114  {
115  quint64 missing = value & ~usedBits;
116  result += QLatin1String(" | 0x") + QString::number(missing, 16);
117  }
118 
119  return result;
120 }
121 
122 QString FlagDataInformation::typeNameImpl() const
123 {
124  return i18nc("Displayed in the type column. first comes the name "
125  "of the enum, then the underlying type (e.g. uint32)",
126  "flag %1 (%2)", mEnum->name(), mValue->typeName());
127 }
removeFromArray
static void removeFromArray(QVarLengthArray< T, len > &array, int index)
Definition: flagdatainformation.cpp:41
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
EnumDataInformation
Definition: enumdatainformation.h:29
PrimitiveDataInformationWrapper::value
virtual AllPrimitiveTypes value() const
Definition: primitivedatainformation.h:102
FlagDataInformation::FlagDataInformation
FlagDataInformation(const QString &name, PrimitiveDataInformation *type, EnumDefinition::Ptr enumDef, DataInformation *parent=0)
Definition: flagdatainformation.cpp:30
PrimitiveDataInformation::type
virtual PrimitiveDataType type() const =0
EnumDefinition::Ptr
QSharedDataPointer< EnumDefinition > Ptr
Definition: enumdefinition.h:40
DataInformation::mWasAbleToRead
bool mWasAbleToRead
Definition: datainformation.h:243
PrimitiveDataInformationWrapper::size
virtual BitCount32 size() const
the size in bits of this element
Definition: primitivedatainformation.h:94
Type_Float
Definition: primitivedatatype.h:46
PrimitiveDataInformationWrapper::mValue
QScopedPointer< PrimitiveDataInformation > mValue
Definition: primitivedatainformation.h:116
Type_Invalid
!! DO NOT CHANGE ORDER OF ITEMS !!!
Definition: primitivedatatype.h:31
FlagPair
QPair< QString, quint64 > FlagPair
Definition: flagdatainformation.cpp:38
flagdatainformation.h
EnumDataInformation::mEnum
EnumDefinition::Ptr mEnum
Definition: enumdatainformation.h:47
Type_Double
Definition: primitivedatatype.h:47
PrimitiveDataInformation
A base class for all primitive data elements (e.g.
Definition: primitivedatainformation.h:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal