nx_video_source_sdk  1.0
Video Source SDK
utils.h
Go to the documentation of this file.
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2 
3 #pragma once
4 
12 #include <cstddef>
13 #include <cstring>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <stdint.h>
17 #include <string>
18 #include <sstream>
19 #include <vector>
20 
21 #if defined(QT_CORE_LIB)
22  // To be supported in toString().
23  #include <QtCore/QByteArray>
24  #include <QtCore/QString>
25  #include <QtCore/QUrl>
26 #endif
27 
28 #if !defined(NX_KIT_API)
29  #define NX_KIT_API /*empty*/
30 #endif
31 
32 namespace nx {
33 namespace kit {
34 namespace utils {
35 
36 //-------------------------------------------------------------------------------------------------
37 // Strings.
38 
39 inline bool isAsciiPrintable(int c)
40 {
41  return c >= 32 && c <= 126;
42 }
43 
53 NX_KIT_API std::string decodeEscapedString(
54  const std::string& s, std::string* outErrorMessage = nullptr);
55 
62 template<typename T>
63 std::string toString(T value);
64 
68 template<typename... Args>
69 std::string format(const std::string& formatStr, Args... args)
70 {
71  const int size = snprintf(nullptr, 0, formatStr.c_str(), args...) + /*space for \0*/ 1;
72  if (size <= 0)
73  return formatStr; //< No better way to handle out-of-memory-like errors.
74  std::string result(size, '\0');
75  snprintf(&result[0], size, formatStr.c_str(), args...);
76  result.resize(size - /*terminating \0*/ 1);
77  return result;
78 }
79 
80 NX_KIT_API bool fromString(const std::string& s, int* value);
81 NX_KIT_API bool fromString(const std::string& s, double* value);
82 NX_KIT_API bool fromString(const std::string& s, float* value);
83 NX_KIT_API bool fromString(const std::string& s, bool* value);
84 
85 NX_KIT_API void stringReplaceAllChars(std::string* s, char sample, char replacement);
86 NX_KIT_API void stringInsertAfterEach(std::string* s, char sample, const char* insertion);
87 NX_KIT_API void stringReplaceAll(
88  std::string* s, const std::string& sample, const std::string& replacement);
89 
90 // TODO: Remove when migrating to C++20 - it has std::string::starts_with()/ends_with().
91 NX_KIT_API bool stringStartsWith(const std::string& s, const std::string& prefix);
92 NX_KIT_API bool stringEndsWith(const std::string& s, const std::string& suffix);
93 
94 NX_KIT_API std::string trimString(const std::string& s);
95 
96 //-------------------------------------------------------------------------------------------------
97 // OS support.
98 
103 NX_KIT_API std::string baseName(std::string path);
104 
110 NX_KIT_API std::string absolutePath(
111  const std::string& originDir, const std::string& path);
112 
116 NX_KIT_API std::string getProcessName();
117 
122 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
123 
124 NX_KIT_API bool fileExists(const char* filename);
125 
126 //-------------------------------------------------------------------------------------------------
127 // Aligned allocation.
128 
133 inline size_t alignUp(size_t value, size_t alignment)
134 {
135  if (alignment == 0)
136  return value;
137  const size_t remainder = value % alignment;
138  if (remainder == 0)
139  return value;
140  return value + alignment - remainder;
141 }
142 
144 inline uint8_t* misalignedPtr(void* data)
145 {
146  return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
147 }
148 
157 template<class MallocFunc>
158 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
159 {
160  if (alignment == 0)
161  return nullptr;
162  const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
163  if (!ptr) //< allocation error
164  return ptr;
165 
166  char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
167  const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
168  memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
169  return alignedPtr + misalignment;
170 }
171 
173 inline void* mallocAligned(size_t size, size_t alignment)
174 {
175  // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
176  // deprecated exception specification.
177  return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
178 }
179 
187 template<class FreeFunc>
188 void freeAligned(void* ptr, FreeFunc freeFunc)
189 {
190  if (!ptr)
191  return freeFunc(ptr);
192 
193  ptr = (char*) ptr - sizeof(size_t);
194  size_t misalignment = 0;
195  memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
196  ptr = (char*) ptr - misalignment;
197 
198  freeFunc(ptr);
199 }
200 
202 inline void freeAligned(void* ptr)
203 {
204  // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
205  // deprecated exception specification.
206  return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
207 }
208 
209 //-------------------------------------------------------------------------------------------------
210 // Implementation.
211 
212 // The order of overloads below is important - it defines which will be chosen by inline functions.
213 NX_KIT_API std::string toString(bool b);
214 NX_KIT_API std::string toString(const void* ptr);
215 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
216 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
217 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
218 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
219 NX_KIT_API std::string toString(char c);
220 NX_KIT_API std::string toString(const char* s);
221 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
222 NX_KIT_API std::string toString(wchar_t c);
223 NX_KIT_API std::string toString(const wchar_t* w);
224 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
225 
226 // std::string can contain '\0' inside, hence a dedicated implementation.
227 NX_KIT_API std::string toString(const std::string& s);
228 NX_KIT_API std::string toString(const std::wstring& w);
229 
230 
232 template<typename T>
233 std::string toString(T value)
234 {
235  std::ostringstream outputString;
236  outputString << value;
237  return outputString.str();
238 }
239 
240 #if defined(QT_CORE_LIB)
241 
242 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
243 {
244  return toString(b.toStdString());
245 }
246 
247 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
248 {
249  return toString(s.toUtf8().constData());
250 }
251 
252 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
253 {
254  return toString(u.toEncoded().toStdString());
255 }
256 
257 #endif // defined(QT_CORE_LIB)
258 
259 template<typename P>
260 std::string toString(P* ptr)
261 {
262  return toString((const void*) ptr);
263 }
264 
265 } // namespace utils
266 } // namespace kit
267 } // namespace nx
std::string format(const std::string &formatStr, Args... args)
Definition: utils.h:69
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:133
uint8_t * misalignedPtr(void *data)
Definition: utils.h:144
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:158
Definition: apple_utils.h:6
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:188