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 
42 #define IS_ALIGNED(value, alignment) \
43  ((((size_t)value) & ((alignment) - 1)) == 0)
44 
45 #ifdef ABS
46 #undef ABS
47 #endif
48 
49 #ifdef MIN
50 #undef MIN
51 #endif
52 
53 #ifdef MAX
54 #undef MAX
55 #endif
56 
58 template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
59 
61 template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
62 
64 template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
65 
67 template<typename T> inline T CLIP(T v, T amin, T amax)
68  {
69 #if !defined(RELEASE_BUILD)
70  // Debug builds use this assert to pinpoint
71  // any problematic cases, where amin and amax
72  // are incorrectly ordered
73  // and thus CLIP() would return an invalid result.
74  assert(amin <= amax);
75 #endif
76  if (v < amin) return amin;
77  else if (v > amax) return amax;
78  return v;
79  }
80 
84 template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
85 
87 static inline uint32 ROTATE_LEFT_32(const uint32 x, const uint32 r) {
88  return (x >> (32 - r)) | (x << r);
89 }
90 
92 static inline uint32 ROTATE_RIGHT_32(const uint32 x, const uint32 r) {
93  return (x << (32 - r)) | (x >> r);
94 }
95 
96 #ifdef ARRAYSIZE
97 #undef ARRAYSIZE
98 #endif
99 
103 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
104 
108 #define ARRAYEND(x) ((x) + ARRAYSIZE((x)))
109 
113 template<typename T, size_t N> inline void ARRAYCLEAR(T (&array) [N], const T &value = T()) {
114  T * ptr = array;
115  size_t n = N;
116  while(n--)
117  *ptr++ = value;
118 }
119 
123 #if defined(__GNUC__)
124 # define SCUMMVM_CURRENT_FUNCTION __PRETTY_FUNCTION__
125 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
126 # define SCUMMVM_CURRENT_FUNCTION __func__
127 #elif defined(_MSC_VER)
128 # define SCUMMVM_CURRENT_FUNCTION __FUNCTION__
129 #else
130 # define SCUMMVM_CURRENT_FUNCTION "<unknown>"
131 #endif
132 
135 namespace Common {
136 
137 class String;
138 class U32String;
139 
148 template<class T>
149 constexpr remove_reference_t<T> &&move(T &&t) noexcept {
150  return static_cast<remove_reference_t<T> &&>(t);
151 }
152 
153 template<class T>
154 constexpr T&& forward(remove_reference_t<T> &&t) noexcept {
155  return static_cast<T &&>(t);
156 }
157 
158 template<class T>
159 constexpr T&& forward(remove_reference_t<T> &t) noexcept {
160  return static_cast<T &&>(t);
161 }
162 
166 template<class T1, class T2>
167 struct Pair {
168  T1 first;
169  T2 second;
170 
171  Pair() {
172  }
173 
174  Pair(const Pair &other) : first(other.first), second(other.second) {
175  }
176 
177  Pair(Pair &&other) : first(Common::move(other.first)), second(Common::move(other.second)) {
178  }
179 
180  Pair(const T1 &first_, const T2 &second_) : first(first_), second(second_) {
181  }
182 
183  Pair(T1 &&first_, T2 &&second_) : first(Common::move(first_)), second(Common::move(second_)) {
184  }
185 
186  Pair(T1 &&first_, const T2 &second_) : first(Common::move(first_)), second(second_) {
187  }
188 
189  Pair(const T1 &first_, T2 &&second_) : first(first_), second(Common::move(second_)) {
190  }
191 
192  Pair &operator=(const Pair &other) {
193  this->first = other.first;
194  this->second = other.second;
195  return *this;
196  }
197 
198  Pair &operator=(Pair &&other) {
199  this->first = Common::move(other.first);
200  this->second = Common::move(other.second);
201  return *this;
202  }
203 };
204 
214 extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
215 
216 
229 bool parseBool(const String &val, bool &valAsBool);
230 
231 
242 bool isAscii(int c);
243 
254 bool isAlnum(int c);
255 
266 bool isAlpha(int c);
267 
278 bool isDigit(int c);
279 
290 bool isXDigit(int c);
291 
302 bool isLower(int c);
303 
319 bool isSpace(int c);
320 
331 bool isUpper(int c);
332 
344 bool isPrint(int c);
345 
354 bool isPunct(int c);
355 
363 bool isCntrl(int c);
364 
372 bool isGraph(int c);
373 
389 bool isBlank(int c);
390 
391 
403 Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
404 
407 } // End of namespace Common
408 
409 #endif
Definition: util.h:167
void ARRAYCLEAR(T(&array) [N], const T &value=T())
Definition: util.h:113
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:84
T CLIP(T v, T amin, T amax)
Definition: util.h:67
void hexdump(const byte *data, int len, int bytesPerLine=16, int startOffset=0)
bool isXDigit(int c)
bool isLower(int c)
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:61
T MAX(T a, T b)
Definition: util.h:64
T ABS(T x)
Definition: util.h:58
bool isDigit(int c)
bool isBlank(int c)
bool isAlnum(int c)
bool isPunct(int c)