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 
29 struct 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 
60 template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
61 
63 template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
64 
66 template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
67 
69 template<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 
86 template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
87 
89 static inline uint32 ROTATE_LEFT_32(const uint32 x, const uint32 r) {
90  return (x >> (32 - r)) | (x << r);
91 }
92 
94 static 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
99 
100 template<typename... A> constexpr size_t NUMARGS(A&&...) { return sizeof...(A); }
101 #else
102 
103 using 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 
124 template<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 
146 namespace Common {
147 
148 class String;
149 class U32String;
150 
159 template<class T>
160 constexpr remove_reference_t<T> &&move(T &&t) noexcept {
161  return static_cast<remove_reference_t<T> &&>(t);
162 }
163 
164 template<class T>
165 constexpr T&& forward(remove_reference_t<T> &&t) noexcept {
166  return static_cast<T &&>(t);
167 }
168 
169 template<class T>
170 constexpr T&& forward(remove_reference_t<T> &t) noexcept {
171  return static_cast<T &&>(t);
172 }
173 
177 template<class T1, class T2>
178 struct 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 
225 extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
226 
227 
240 bool parseBool(const String &val, bool &valAsBool);
241 
242 
253 bool isAscii(int c);
254 
265 bool isAlnum(int c);
266 
277 bool isAlpha(int c);
278 
289 bool isDigit(int c);
290 
301 bool isXDigit(int c);
302 
313 bool isLower(int c);
314 
330 bool isSpace(int c);
331 
342 bool isUpper(int c);
343 
355 bool isPrint(int c);
356 
365 bool isPunct(int c);
366 
374 bool isCntrl(int c);
375 
383 bool isGraph(int c);
384 
400 bool isBlank(int c);
401 
402 
414 Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
415 
420 namespace DateTime {
424  TimeDate int64ToTimeDate(int64 integer);
425 
429  int64_t dateTimeToInt64(const TimeDate &timeDate);
430 
434  Common::String formatTime(int64 integer);
435 
439  int64 getTime();
440 }
441 
444 } // End of namespace Common
445 
446 #endif
Definition: util.h:178
Definition: system.h:111
void ARRAYCLEAR(T(&array) [N], const T &value=T())
Definition: util.h:124
Definition: str.h:59
bool isAlpha(int c)
bool isAscii(int c)
bool isSpace(int c)
bool isPrint(int c)
bool parseBool(const String &val, bool &valAsBool)
void SWAP(T &a, T &b)
Definition: util.h:86
T CLIP(T v, T amin, T amax)
Definition: util.h:69
void hexdump(const byte *data, int len, int bytesPerLine=16, int startOffset=0)
bool isXDigit(int c)
constexpr size_t NUMARGS(A &&...)
Definition: util.h:100
int64_t dateTimeToInt64(const TimeDate &timeDate)
bool isLower(int c)
Common::String formatTime(int64 integer)
bool isCntrl(int c)
Definition: algorithm.h:29
bool isGraph(int c)
bool isUpper(int c)
Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut)
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
T MIN(T a, T b)
Definition: util.h:63
T MAX(T a, T b)
Definition: util.h:66
T ABS(T x)
Definition: util.h:60
bool isDigit(int c)
bool isBlank(int c)
bool isAlnum(int c)
bool isPunct(int c)
TimeDate int64ToTimeDate(int64 integer)