• 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
enumdefinition.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 2009 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 #include "enumdefinition.h"
24 
25 #include <QScriptValue>
26 #include <QScriptValueIterator>
27 #include <limits>
28 
29 #include "../../script/scriptlogger.h"
30 
31 QMap<AllPrimitiveTypes, QString> EnumDefinition::parseEnumValues(const QScriptValue& val,
32  const LoggerWithContext& logger, PrimitiveDataType type)
33 {
34  QMap<AllPrimitiveTypes, QString> enumValues;
35 
36  QScriptValueIterator it(val);
37  while (it.hasNext())
38  {
39  it.next();
40  QScriptValue val = it.value();
41  QPair<AllPrimitiveTypes, QString> conv;
42  if (val.isNumber())
43  {
44  double num = val.toNumber();
45  conv = convertToEnumEntry(it.name(), num, logger, type);
46  }
47  else
48  {
49  QString numStr = val.toString();
50  conv = convertToEnumEntry(it.name(), numStr, logger, type);
51  }
52  if (conv == QPair<AllPrimitiveTypes, QString>())
53  continue;
54  enumValues.insert(conv.first, conv.second);
55  }
56  return enumValues;
57 }
58 
59 QPair<AllPrimitiveTypes, QString> EnumDefinition::convertToEnumEntry(const QString& name,
60  const QVariant& value, const LoggerWithContext& logger, PrimitiveDataType type)
61 {
62  Q_ASSERT(!name.isEmpty());
63  //name must not be empty, else default constructed return would be valid!
64  quint64 maxValue = 0.0;
65  qint64 minValue;
66  switch (type.value)
67  {
68  case Type_Bitfield:
69  //assume all 64 bits are used, we do not have the necessary information here
70  maxValue = std::numeric_limits<quint64>::max();
71  minValue = std::numeric_limits<qint64>::min();
72  break;
73  case Type_Bool8:
74  //fall through
75  case Type_UInt8:
76  maxValue = std::numeric_limits<quint8>::max();
77  minValue = 0;
78  break;
79  case Type_Bool16:
80  //fall through
81  case Type_UInt16:
82  maxValue = std::numeric_limits<quint16>::max();
83  minValue = 0;
84  break;
85  case Type_Bool32:
86  //fall through
87  case Type_UInt32:
88  maxValue = std::numeric_limits<quint32>::max();
89  minValue = 0;
90  break;
91  case Type_Bool64:
92  //fall through
93  case Type_UInt64:
94  maxValue = std::numeric_limits<quint64>::max();
95  minValue = 0;
96  break;
97  case Type_Int8:
98  maxValue = std::numeric_limits<qint8>::max();
99  minValue = std::numeric_limits<qint8>::min();
100  break;
101  case Type_Int16:
102  maxValue = std::numeric_limits<qint16>::max();
103  minValue = std::numeric_limits<qint16>::min();
104  break;
105  case Type_Int32:
106  maxValue = std::numeric_limits<qint32>::max();
107  minValue = std::numeric_limits<qint32>::min();
108  break;
109  case Type_Int64:
110  maxValue = std::numeric_limits<qint64>::max();
111  minValue = std::numeric_limits<qint64>::min();
112  break;
113  default:
114  logger.warn() << type << "is an invalid type for an enumeration, no values were parsed";
115  return QPair<AllPrimitiveTypes, QString>();
116  }
117 
118  AllPrimitiveTypes intValue;
119  if (value.type() == QVariant::Double)
120  {
121  const double num = value.toDouble();
122  //this is the largest double which maps to an integer exactly, ...993 is not representable anymore
123  //...992 would still be representable, however it may be that ...993 was rounded down to that, be safe
124  //and force the user to write such large numbers as strings
125  if (num <= 9007199254740991.0)
126  {
127  intValue = qint64(num);
128  }
129  else
130  {
131  logger.warn() << "The value" << num << "in enum" << name
132  << " is larger than the biggest double value that can represent"
133  " any smaller integer exactly, skipping it.\n"
134  "Write the value as a string so it can be converted"
135  "to an integer exactly.";
136  return QPair<AllPrimitiveTypes, QString>();
137  }
138  }
139  else
140  {
141  const QString valueString = value.toString();
142  bool ok = false;
143  if (valueString.startsWith(QLatin1String("0x")))
144  {
145  intValue = valueString.mid(2).toULongLong(&ok, 16);
146  }
147  else
148  {
149  if (type == Type_UInt64 || type == Type_Bool64)
150  intValue = valueString.toULongLong(&ok, 10);
151  else
152  intValue = valueString.toLongLong(&ok, 10);
153  }
154  if (!ok)
155  {
156  QString errMessage = QString(QLatin1String("Could not convert '%1' to an enum "
157  "constant, name was: %2")).arg(valueString, name);
158  logger.warn() << errMessage;
159  return QPair<AllPrimitiveTypes, QString>();
160  }
161  }
162  quint64 asUnsigned = intValue.value<quint64>();
163  if (asUnsigned > maxValue)
164  {
165  QString errMessage = QLatin1String("Enumerator %1: %2 is larger than the maximum "
166  "possible for type %3 (%4)");
167  errMessage = errMessage.arg(name, QString::number(asUnsigned),
168  PrimitiveType::standardTypeName(type), QString::number(maxValue));
169  logger.warn() << errMessage;
170  return QPair<AllPrimitiveTypes, QString>();
171  }
172  qint64 asSigned = intValue.value<qint64>();
173  if (minValue != 0 && asSigned < minValue)
174  {
175  QString errMessage = QLatin1String("Enumerator %1: %2 is smaller than the minimum "
176  "possible for type %3 (%4)");
177  errMessage = errMessage.arg(name, QString::number(asSigned),
178  PrimitiveType::standardTypeName(type), QString::number(minValue));
179  logger.warn() << errMessage;
180  return QPair<AllPrimitiveTypes, QString>();
181  }
182  return QPair<AllPrimitiveTypes, QString>(intValue, name);
183 
184 }
LoggerWithContext
Definition: scriptlogger.h:94
EnumDefinition::name
const QString & name() const
Definition: enumdefinition.h:86
Type_Bool32
Definition: primitivedatatype.h:40
LoggerWithContext::warn
QDebug warn() const
Definition: scriptlogger.h:98
Type_Int64
Definition: primitivedatatype.h:44
Type_Bool16
Definition: primitivedatatype.h:37
enumdefinition.h
EnumDefinition::convertToEnumEntry
static QPair< AllPrimitiveTypes, QString > convertToEnumEntry(const QString &name, const QVariant &value, const LoggerWithContext &logger, PrimitiveDataType type)
Definition: enumdefinition.cpp:59
Type_UInt32
Definition: primitivedatatype.h:42
PrimitiveDataType
Definition: primitivedatatype.h:54
Type_UInt16
Definition: primitivedatatype.h:39
Type_UInt64
Definition: primitivedatatype.h:45
Type_Bool64
Definition: primitivedatatype.h:43
Type_Int16
Definition: primitivedatatype.h:38
Type_Bitfield
Definition: primitivedatatype.h:48
PrimitiveType::standardTypeName
QString standardTypeName(PrimitiveDataType type)
Definition: primitivedatatype.cpp:76
EnumDefinition::parseEnumValues
static QMap< AllPrimitiveTypes, QString > parseEnumValues(const QScriptValue &val, const LoggerWithContext &logger, PrimitiveDataType type=Type_UInt64)
Definition: enumdefinition.cpp:31
Type_Int32
Definition: primitivedatatype.h:41
AllPrimitiveTypes::value
T value() const
Type_Int8
Definition: primitivedatatype.h:34
AllPrimitiveTypes
This union holds the value of one primitive datatype.
Definition: allprimitivetypes.h:70
Type_UInt8
Definition: primitivedatatype.h:35
Type_Bool8
Definition: primitivedatatype.h:33
PrimitiveDataType::value
PrimitiveDataTypeEnum value
Definition: primitivedatatype.h:56
EnumDefinition::type
PrimitiveDataType type() const
Definition: enumdefinition.h:81
QMap< AllPrimitiveTypes, QString >
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