Torc  0.1
torccoreutils.h
Go to the documentation of this file.
1 #ifndef TORCCOREUTILS_H
2 #define TORCCOREUTILS_H
3 
4 // Qt
5 #include <QFile>
6 #include <QMetaEnum>
7 #include <QDateTime>
8 
9 namespace TorcCoreUtils
10 {
11  QDateTime DateTimeFromString (const QString &String);
12  quint64 GetMicrosecondCount (void);
13  void QtMessage (QtMsgType Type, const QMessageLogContext &Context, const QString &Message);
14  bool HasZlib (void);
15  QByteArray GZipCompress (QByteArray &Source);
16  QByteArray GZipCompressFile (QFile &Source);
17 
18  template <typename T> QString EnumToLowerString(T Value)
19  {
20  return QString(QMetaEnum::fromType<T>().valueToKey(Value)).toLower();
21  }
22 
23  template <typename T> QString EnumToString(T Value)
24  {
25  return QMetaEnum::fromType<T>().valueToKey(Value);
26  }
27 
28  template <typename T> int StringToEnum(const QString &Value, bool CaseSensitive = false)
29  {
30  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
31  if (CaseSensitive)
32  return metaEnum.keyToValue(Value.toLatin1());
33  QByteArray value = Value.toLower().toLatin1();
34  for (int count = metaEnum.keyCount() - 1 ; count >= 0; --count)
35  if (qstrcmp(value, QByteArray(metaEnum.key(count)).toLower()) == 0)
36  return metaEnum.value(count);
37  return -1; // for consistency with QMetaEnum::keyToValue
38  }
39 
40  template <typename T> QStringList EnumList()
41  {
42  const QMetaEnum metaEnum = QMetaEnum::fromType<T>();
43  QStringList result;
44  result.reserve(metaEnum.keyCount());
45  for (int count = metaEnum.keyCount() - 1 ; count >= 0; --count)
46  result << QString(metaEnum.key(count)).toLower();
47  return result;
48  }
49 }
50 
51 #endif // TORCCOREUTILS_H
QStringList EnumList()
Definition: torccoreutils.h:40
bool HasZlib(void)
Return true if zlib support is available.
quint64 GetMicrosecondCount(void)
Get the current system clock time in microseconds.
QDateTime DateTimeFromString(const QString &String)
Parse a QDateTime from the given QString.
QString EnumToLowerString(T Value)
Definition: torccoreutils.h:18
QByteArray GZipCompressFile(QFile &Source)
Compress the given file using GZip.
QByteArray GZipCompress(QByteArray &Source)
Compress the supplied data using GZip.
void QtMessage(QtMsgType Type, const QMessageLogContext &Context, const QString &Message)
A handler routine for Qt messages.
QString EnumToString(T Value)
Definition: torccoreutils.h:23
int StringToEnum(const QString &Value, bool CaseSensitive=false)
Definition: torccoreutils.h:28