ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
86 #ifdef ARRAYSIZE
87 #undef ARRAYSIZE
88 #endif
89 
93 #define ARRAYSIZE(x) ((int)(sizeof(x) / sizeof(x[0])))
94 
98 #define ARRAYEND(x) ((x) + ARRAYSIZE((x)))
99 
103 template<typename T, size_t N> inline void ARRAYCLEAR(T (&array) [N], const T &value = T()) {
104  T * ptr = array;
105  size_t n = N;
106  while(n--)
107  *ptr++ = value;
108 }
109 
113 #if defined(__GNUC__)
114 # define SCUMMVM_CURRENT_FUNCTION __PRETTY_FUNCTION__
115 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
116 # define SCUMMVM_CURRENT_FUNCTION __func__
117 #elif defined(_MSC_VER)
118 # define SCUMMVM_CURRENT_FUNCTION __FUNCTION__
119 #else
120 # define SCUMMVM_CURRENT_FUNCTION "<unknown>"
121 #endif
122 
125 namespace Common {
126 
127 class String;
128 class U32String;
129 
138 template<class T>
139 constexpr remove_reference_t<T> &&move(T &&t) noexcept {
140  return static_cast<remove_reference_t<T> &&>(t);
141 }
142 
143 template<class T>
144 constexpr T&& forward(remove_reference_t<T> &&t) noexcept {
145  return static_cast<T &&>(t);
146 }
147 
148 template<class T>
149 constexpr T&& forward(remove_reference_t<T> &t) noexcept {
150  return static_cast<T &&>(t);
151 }
152 
156 template<class T1, class T2>
157 struct Pair {
158  T1 first;
159  T2 second;
160 
161  Pair() {
162  }
163 
164  Pair(const Pair &other) : first(other.first), second(other.second) {
165  }
166 
167  Pair(Pair &&other) : first(Common::move(other.first)), second(Common::move(other.second)) {
168  }
169 
170  Pair(const T1 &first_, const T2 &second_) : first(first_), second(second_) {
171  }
172 
173  Pair(T1 &&first_, T2 &&second_) : first(Common::move(first_)), second(Common::move(second_)) {
174  }
175 
176  Pair(T1 &&first_, const T2 &second_) : first(Common::move(first_)), second(second_) {
177  }
178 
179  Pair(const T1 &first_, T2 &&second_) : first(first_), second(Common::move(second_)) {
180  }
181 
182  Pair &operator=(const Pair &other) {
183  this->first = other.first;
184  this->second = other.second;
185  return *this;
186  }
187 
188  Pair &operator=(Pair &&other) {
189  this->first = Common::move(other.first);
190  this->second = Common::move(other.second);
191  return *this;
192  }
193 };
194 
204 extern void hexdump(const byte * data, int len, int bytesPerLine = 16, int startOffset = 0);
205 
206 
219 bool parseBool(const String &val, bool &valAsBool);
220 
221 
232 bool isAscii(int c);
233 
244 bool isAlnum(int c);
245 
256 bool isAlpha(int c);
257 
268 bool isDigit(int c);
269 
280 bool isXDigit(int c);
281 
292 bool isLower(int c);
293 
309 bool isSpace(int c);
310 
321 bool isUpper(int c);
322 
334 bool isPrint(int c);
335 
344 bool isPunct(int c);
345 
353 bool isCntrl(int c);
354 
362 bool isGraph(int c);
363 
379 bool isBlank(int c);
380 
381 
393 Common::String getHumanReadableBytes(uint64 bytes, const char *&unitsOut);
394 
397 } // End of namespace Common
398 
399 #endif
Definition: util.h:157
void ARRAYCLEAR(T(&array) [N], const T &value=T())
Definition: util.h:103
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)