ScummVM API documentation
util.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef COMMON_UTIL_H
23#define COMMON_UTIL_H
24
25#include "common/scummsys.h"
26
27#include "common/type_traits.h"
28
29struct TimeDate;
30
44#define IS_ALIGNED(value, alignment) \
45 ((((size_t)value) & ((alignment) - 1)) == 0)
46
47#ifdef ABS
48#undef ABS
49#endif
50
51#ifdef MIN
52#undef MIN
53#endif
54
55#ifdef MAX
56#undef MAX
57#endif
58
60template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
61
63template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
64
66template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
67
69template<typename T> inline T CLIP(T v, T amin, T amax)
70 {
71#if !defined(RELEASE_BUILD)
72 // Debug builds use this assert to pinpoint
73 // any problematic cases, where amin and amax
74 // are incorrectly ordered
75 // and thus CLIP() would return an invalid result.
76 assert(amin <= amax);
77#endif
78 if (v < amin) return amin;
79 else if (v > amax) return amax;
80 return v;
81 }
82
86template<typename T> inline void SWAP(T &a, T &b);
87
89static inline uint32 ROTATE_LEFT_32(const uint32 x, const uint32 r) {
90 return (x >> (32 - r)) | (x << r);
91}
92
94static inline uint32 ROTATE_RIGHT_32(const uint32 x, const uint32 r) {
95 return (x << (32 - r)) | (x >> r);
96}
97
98#if !defined(_MSC_VER) || _MSC_VER >= 1910
100template<typename... A> constexpr size_t NUMARGS(A&&...) { return sizeof...(A); }
101#else
103using int_c_array = int[]; // MSVC 2015 doesn't like the template method above
104#define NUMARGS(...) (sizeof(int_c_array{__VA_ARGS__})/sizeof(int))
105#endif
106
107#ifdef ARRAYSIZE
108#undef ARRAYSIZE
109#endif
110
114#define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
115
119#define ARRAYEND(x) ((x) + ARRAYSIZE((x)))
120
124template<typename T, size_t N> inline void ARRAYCLEAR(T (&array) [N], const T &value = T()) {
125 T * ptr = array;
126 size_t n = N;
127 while(n--)
128 *ptr++ = value;
129}
130
134#if defined(__GNUC__)
135# define SCUMMVM_CURRENT_FUNCTION __PRETTY_FUNCTION__
136#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
137# define SCUMMVM_CURRENT_FUNCTION __func__
138#elif defined(_MSC_VER)
139# define SCUMMVM_CURRENT_FUNCTION __FUNCTION__
140#else
141# define SCUMMVM_CURRENT_FUNCTION "<unknown>"
142#endif
143
146namespace Common {
147
148class String;
149class U32String;
150
159template<class T>
160constexpr remove_reference_t<T> &&move(T &&t) noexcept {
161 return static_cast<remove_reference_t<T> &&>(t);
162}
163
164template<class T>
165constexpr T&& forward(remove_reference_t<T> &&t) noexcept {
166 return static_cast<T &&>(t);
167}
168
169template<class T>
170constexpr T&& forward(remove_reference_t<T> &t) noexcept {
171 return static_cast<T &&>(t);
172}
173
177template<class T1, class T2>
178struct Pair {
179 T1 first;
180 T2 second;
181
182 constexpr Pair() {
183 }
184
185 constexpr Pair(const Pair &other) : first(other.first), second(other.second) {
186 }
187
188 constexpr Pair(Pair &&other) : first(Common::move(other.first)), second(Common::move(other.second)) {
189 }
190
191 constexpr Pair(const T1 &first_, const T2 &second_) : first(first_), second(second_) {
192 }
193
194 constexpr Pair(T1 &&first_, T2 &&second_) : first(Common::move(first_)), second(Common::move(second_)) {
195 }
196
197 constexpr Pair(T1 &&first_, const T2 &second_) : first(Common::move(first_)), second(second_) {
198 }
199
200 constexpr Pair(const T1 &first_, T2 &&second_) : first(first_), second(Common::move(second_)) {
201 }
202
203 Pair &operator=(const Pair &other) {
204 this->first = other.first;
205 this->second = other.second;
206 return *this;
207 }
208
209 Pair &operator=(Pair &&other) {
210 this->first = Common::move(other.first);
211 this->second = Common::move(other.second);
212 return *this;
213 }
214};
215
225extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
226
227
240bool parseBool(const String &val, bool &valAsBool);
241
242
253bool isAscii(int c);
254
265bool isAlnum(int c);
266
277bool isAlpha(int c);
278
289bool isDigit(int c);
290
301bool isXDigit(int c);
302
313bool isLower(int c);
314
330bool isSpace(int c);
331
342bool isUpper(int c);
343
355bool isPrint(int c);
356
365bool isPunct(int c);
366
374bool isCntrl(int c);
375
383bool isGraph(int c);
384
400bool isBlank(int c);
401
402
414Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
415
420namespace DateTime {
424 TimeDate int64ToTimeDate(int64 integer);
425
429 int64_t dateTimeToInt64(const TimeDate &timeDate);
430
435
439 int64 getTime();
440}
441
442template<typename T, bool useMove = Common::is_move_constructible<T>::v && Common::is_move_assignable<T>::v>
443struct SWAP_helper {};
444
445template<typename T>
446struct SWAP_helper<T, true> {
447 static void swap(T &a, T &b) {
448 T tmp = Common::move(a);
449 a = Common::move(b);
450 b = Common::move(tmp);
451 }
452};
453
454template<typename T>
455struct SWAP_helper<T, false> {
456 static void swap(T &a, T &b) {
457 T tmp = a;
458 a = b;
459 b = tmp;
460 }
461};
462
465} // End of namespace Common
466
467template<typename T> inline void SWAP(T &a, T &b) { Common::SWAP_helper<T>::swap(a, b); }
468
469#endif
Definition: str.h:59
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
T CLIP(T v, T amin, T amax)
Definition: util.h:69
constexpr size_t NUMARGS(A &&...)
Definition: util.h:100
bool isSpace(int c)
bool isAlpha(int c)
Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut)
bool isBlank(int c)
T ABS(T x)
Definition: util.h:60
bool isGraph(int c)
bool isAscii(int c)
bool isCntrl(int c)
void SWAP(T &a, T &b)
Definition: util.h:467
bool isXDigit(int c)
bool isAlnum(int c)
bool isPunct(int c)
T MIN(T a, T b)
Definition: util.h:63
bool isDigit(int c)
bool isLower(int c)
bool isPrint(int c)
bool isUpper(int c)
bool parseBool(const String &val, bool &valAsBool)
void ARRAYCLEAR(T(&array)[N], const T &value=T())
Definition: util.h:124
T MAX(T a, T b)
Definition: util.h:66
void hexdump(const byte *data, int len, int bytesPerLine=16, int startOffset=0)
TimeDate int64ToTimeDate(int64 integer)
int64_t dateTimeToInt64(const TimeDate &timeDate)
Common::String formatTime(int64 integer)
Definition: algorithm.h:29
Definition: util.h:178
Definition: util.h:443
Definition: type_traits.h:149
Definition: system.h:111