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 
137 template<class T> struct remove_cv {
138  typedef T type;
139 };
140 template<class T> struct remove_cv<const T> {
141  typedef T type;
142 };
143 template<class T> struct remove_cv<volatile T> {
144  typedef T type;
145 };
146 template<class T> struct remove_cv<const volatile T> {
147  typedef T type;
148 };
149 
150 template<class T> struct remove_const {
151  typedef T type;
152 };
153 template<class T> struct remove_const<const T> {
154  typedef T type;
155 };
156 
157 template<class T> struct remove_volatile {
158  typedef T type;
159 };
160 template<class T> struct remove_volatile<volatile T> {
161  typedef T type;
162 };
163 
168 template<class T>
170  typedef T type;
171 };
172 template<class T>
173 struct remove_reference<T &> {
174  typedef T type;
175 };
176 template<class T>
177 struct remove_reference<T &&> {
178  typedef T type;
179 };
180 
181 template<class T>
182 using remove_cv_t = typename remove_cv<T>::type;
183 template<class T>
184 using remove_const_t = typename remove_const<T>::type;
185 template<class T>
186 using remove_volatile_t = typename remove_volatile<T>::type;
187 
188 template<class T>
189 using remove_reference_t = typename remove_reference<T>::type;
190 
194 template<class T>
195 constexpr remove_reference_t<T> &&move(T &&t) noexcept {
196  return static_cast<remove_reference_t<T> &&>(t);
197 }
198 
199 template<class T>
200 constexpr T&& forward(remove_reference_t<T> &&t) noexcept {
201  return static_cast<T &&>(t);
202 }
203 
204 template<class T>
205 constexpr T&& forward(remove_reference_t<T> &t) noexcept {
206  return static_cast<T &&>(t);
207 }
208 
212 template<class T1, class T2>
213 struct Pair {
214  T1 first;
215  T2 second;
216 
217  Pair() {
218  }
219 
220  Pair(const Pair &other) : first(other.first), second(other.second) {
221  }
222 
223  Pair(Pair &&other) : first(Common::move(other.first)), second(Common::move(other.second)) {
224  }
225 
226  Pair(const T1 &first_, const T2 &second_) : first(first_), second(second_) {
227  }
228 
229  Pair(T1 &&first_, T2 &&second_) : first(Common::move(first_)), second(Common::move(second_)) {
230  }
231 
232  Pair(T1 &&first_, const T2 &second_) : first(Common::move(first_)), second(second_) {
233  }
234 
235  Pair(const T1 &first_, T2 &&second_) : first(first_), second(Common::move(second_)) {
236  }
237 
238  Pair &operator=(const Pair &other) {
239  this->first = other.first;
240  this->second = other.second;
241  return *this;
242  }
243 
244  Pair &operator=(Pair &&other) {
245  this->first = Common::move(other.first);
246  this->second = Common::move(other.second);
247  return *this;
248  }
249 };
250 
260 extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
261 
262 
275 bool parseBool(const String &val, bool &valAsBool);
276 
277 
288 bool isAscii(int c);
289 
300 bool isAlnum(int c);
301 
312 bool isAlpha(int c);
313 
324 bool isDigit(int c);
325 
336 bool isXDigit(int c);
337 
348 bool isLower(int c);
349 
365 bool isSpace(int c);
366 
377 bool isUpper(int c);
378 
390 bool isPrint(int c);
391 
400 bool isPunct(int c);
401 
409 bool isCntrl(int c);
410 
418 bool isGraph(int c);
419 
435 bool isBlank(int c);
436 
437 
449 Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
450 
453 } // End of namespace Common
454 
455 #endif
Definition: util.h:213
void ARRAYCLEAR(T(&array) [N], const T &value=T())
Definition: util.h:101
Definition: str.h:59
Definition: util.h:137
bool isAlpha(int c)
Definition: util.h:169
bool isAscii(int c)
bool isSpace(int c)
Definition: util.h:157
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:150
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: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)