kwave  18.07.70
Utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  Utils.h - some commonly used utility functions
3  -------------------
4  begin : Sun Mar 27 2011
5  copyright : (C) 2011 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef KWAVE_UTILS_H
19 #define KWAVE_UTILS_H
20 
21 #include "config.h"
22 
23 #include <limits>
24 
25 #include <QString>
26 #include <QtGlobal>
27 
28 #include "libkwave/Sample.h" // for sample_index_t
29 
30 namespace Kwave
31 {
32 
37  void Q_DECL_EXPORT yield();
38 
49  QString Q_DECL_EXPORT zoom2string(double percent);
50 
64  QString Q_DECL_EXPORT ms2string(double ms, int precision = 6);
65 
72  QString Q_DECL_EXPORT samples2string(sample_index_t samples);
73 
80  QString Q_DECL_EXPORT ms2hms(double ms);
81 
88  QString Q_DECL_EXPORT string2date(const QString &s);
89 
96  template<class T> T round_up(T x, const T s)
97  {
98  T modulo = (x % s);
99  if (modulo) x += (s - modulo);
100  return x;
101  }
102 
109  template <typename T> unsigned int toUint(T x)
110  {
111  const unsigned int max = std::numeric_limits<unsigned int>::max();
112  Q_ASSERT(x >= 0);
113  Q_ASSERT(static_cast<quint64>(x) <= static_cast<quint64>(max));
114 
115  if (x <= 0) return 0;
116  if (static_cast<quint64>(x) > static_cast<quint64>(max)) return max;
117 
118  return static_cast<unsigned int>(x);
119  }
120 
127  template <typename T> int toInt(T x)
128  {
129  const int min = std::numeric_limits<int>::min();
130  const int max = std::numeric_limits<int>::max();
131  Q_ASSERT(static_cast<qint64>(x) >= static_cast<qint64>(min));
132  Q_ASSERT(static_cast<qint64>(x) <= static_cast<qint64>(max));
133 
134  if (static_cast<qint64>(x) < static_cast<qint64>(min)) return min;
135  if (static_cast<qint64>(x) > static_cast<qint64>(max)) return max;
136 
137  return static_cast<int>(x);
138  }
139 
141  QString Q_DECL_EXPORT urlScheme();
142 
143 }
144 
145 #endif /* KWAVE_UTILS_H */
146 
147 //***************************************************************************
148 //***************************************************************************
Definition: App.h:33
QString Q_DECL_EXPORT ms2string(double ms, int precision=6)
Definition: Utils.cpp:66
QString Q_DECL_EXPORT samples2string(sample_index_t samples)
Definition: Utils.cpp:98
QString Q_DECL_EXPORT ms2hms(double ms)
Definition: Utils.cpp:104
quint64 sample_index_t
Definition: Sample.h:28
QString Q_DECL_EXPORT zoom2string(double percent)
Definition: Utils.cpp:46
T round_up(T x, const T s)
Definition: Utils.h:96
QString Q_DECL_EXPORT string2date(const QString &s)
Definition: Utils.cpp:126
int toInt(T x)
Definition: Utils.h:127
void Q_DECL_EXPORT yield()
Definition: Utils.cpp:39
unsigned int toUint(T x)
Definition: Utils.h:109
QString Q_DECL_EXPORT urlScheme()
Definition: Utils.cpp:177