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 
40 #define IS_ALIGNED(value, alignment) \
41  ((((size_t)value) & ((alignment) - 1)) == 0)
42 
43 #ifdef ABS
44 #undef ABS
45 #endif
46 
47 #ifdef MIN
48 #undef MIN
49 #endif
50 
51 #ifdef MAX
52 #undef MAX
53 #endif
54 
56 template<typename T> inline T ABS(T x) { return (x >= 0) ? x : -x; }
57 
59 template<typename T> inline T MIN(T a, T b) { return (a < b) ? a : b; }
60 
62 template<typename T> inline T MAX(T a, T b) { return (a > b) ? a : b; }
63 
65 template<typename T> inline T CLIP(T v, T amin, T amax)
66  {
67 #if !defined(RELEASE_BUILD)
68  // Debug builds use this assert to pinpoint
69  // any problematic cases, where amin and amax
70  // are incorrectly ordered
71  // and thus CLIP() would return an invalid result.
72  assert(amin <= amax);
73 #endif
74  if (v < amin) return amin;
75  else if (v > amax) return amax;
76  return v;
77  }
78 
82 template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
83 
84 #ifdef ARRAYSIZE
85 #undef ARRAYSIZE
86 #endif
87 
91 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
92 
96 #define ARRAYEND(x) ((x) + ARRAYSIZE((x)))
97 
101 template<typename T, size_t N> inline void ARRAYCLEAR(T (&array) [N], const T &value = T()) {
102  T * ptr = array;
103  size_t n = N;
104  while(n--)
105  *ptr++ = value;
106 }
107 
111 #if defined(__GNUC__)
112 # define SCUMMVM_CURRENT_FUNCTION __PRETTY_FUNCTION__
113 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
114 # define SCUMMVM_CURRENT_FUNCTION __func__
115 #elif defined(_MSC_VER)
116 # define SCUMMVM_CURRENT_FUNCTION __FUNCTION__
117 #else
118 # define SCUMMVM_CURRENT_FUNCTION "<unknown>"
119 #endif
120 
123 namespace Common {
124 
125 class String;
126 class U32String;
127 
136 template<class T1, class T2>
137 struct Pair {
138  T1 first;
139  T2 second;
140 
141  Pair() {
142  }
143  Pair(T1 first_, T2 second_) : first(first_), second(second_) {
144  }
145 };
146 
151 template<class T> struct remove_cv {
152  typedef T type;
153 };
154 template<class T> struct remove_cv<const T> {
155  typedef T type;
156 };
157 template<class T> struct remove_cv<volatile T> {
158  typedef T type;
159 };
160 template<class T> struct remove_cv<const volatile T> {
161  typedef T type;
162 };
163 
164 template<class T> struct remove_const {
165  typedef T type;
166 };
167 template<class T> struct remove_const<const T> {
168  typedef T type;
169 };
170 
171 template<class T> struct remove_volatile {
172  typedef T type;
173 };
174 template<class T> struct remove_volatile<volatile T> {
175  typedef T type;
176 };
177 
182 template<class T>
184  typedef T type;
185 };
186 template<class T>
187 struct remove_reference<T &> {
188  typedef T type;
189 };
190 template<class T>
191 struct remove_reference<T &&> {
192  typedef T type;
193 };
194 
195 template<class T>
196 using remove_cv_t = typename remove_cv<T>::type;
197 template<class T>
198 using remove_const_t = typename remove_const<T>::type;
199 template<class T>
200 using remove_volatile_t = typename remove_volatile<T>::type;
201 
202 template<class T>
203 using remove_reference_t = typename remove_reference<T>::type;
204 
208 template<class T>
209 constexpr remove_reference_t<T> &&move(T &&t) noexcept {
210  return static_cast<remove_reference_t<T> &&>(t);
211 }
212 
213 template<class T>
214 constexpr T&& forward(remove_reference_t<T> &t) noexcept {
215  return static_cast<T &&>(t);
216 }
217 
227 extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
228 
229 
242 bool parseBool(const String &val, bool &valAsBool);
243 
244 
255 bool isAscii(int c);
256 
267 bool isAlnum(int c);
268 
279 bool isAlpha(int c);
280 
291 bool isDigit(int c);
292 
303 bool isXDigit(int c);
304 
315 bool isLower(int c);
316 
332 bool isSpace(int c);
333 
344 bool isUpper(int c);
345 
357 bool isPrint(int c);
358 
367 bool isPunct(int c);
368 
376 bool isCntrl(int c);
377 
385 bool isGraph(int c);
386 
402 bool isBlank(int c);
403 
404 
416 Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
417 
420 } // End of namespace Common
421 
422 #endif
Definition: util.h:137
void ARRAYCLEAR(T(&array) [N], const T &value=T())
Definition: util.h:101
Definition: str.h:59
Definition: util.h:151
bool isAlpha(int c)
Definition: util.h:183
bool isAscii(int c)
bool isSpace(int c)
Definition: util.h:171
bool isPrint(int c)
bool parseBool(const String &val, bool &valAsBool)
void SWAP(T &a, T &b)
Definition: util.h:82
T CLIP(T v, T amin, T amax)
Definition: util.h:65
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: util.h:164
constexpr remove_reference_t< T > && move(T &&t) noexcept
Definition: util.h:209
Definition: algorithm.h:29
bool isGraph(int c)
bool isUpper(int c)
Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut)
T MIN(T a, T b)
Definition: util.h:59
T MAX(T a, T b)
Definition: util.h:62
T ABS(T x)
Definition: util.h:56
bool isDigit(int c)
bool isBlank(int c)
bool isAlnum(int c)
bool isPunct(int c)