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 //-------------------------------------------------------------------------------------------------
91 // OS support.
92 
97 NX_KIT_API std::string baseName(std::string path);
98 
102 NX_KIT_API std::string getProcessName();
103 
108 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
109 
110 NX_KIT_API bool fileExists(const char* filename);
111 
112 //-------------------------------------------------------------------------------------------------
113 // Aligned allocation.
114 
119 inline size_t alignUp(size_t value, size_t alignment)
120 {
121  if (alignment == 0)
122  return value;
123  const size_t remainder = value % alignment;
124  if (remainder == 0)
125  return value;
126  return value + alignment - remainder;
127 }
128 
130 inline uint8_t* misalignedPtr(void* data)
131 {
132  return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
133 }
134 
143 template<class MallocFunc>
144 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
145 {
146  if (alignment == 0)
147  return nullptr;
148  const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
149  if (!ptr) //< allocation error
150  return ptr;
151 
152  char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
153  const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
154  memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
155  return alignedPtr + misalignment;
156 }
157 
159 inline void* mallocAligned(size_t size, size_t alignment)
160 {
161  // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
162  // deprecated exception specification.
163  return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
164 }
165 
173 template<class FreeFunc>
174 void freeAligned(void* ptr, FreeFunc freeFunc)
175 {
176  if (!ptr)
177  return freeFunc(ptr);
178 
179  ptr = (char*) ptr - sizeof(size_t);
180  size_t misalignment = 0;
181  memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
182  ptr = (char*) ptr - misalignment;
183 
184  freeFunc(ptr);
185 }
186 
188 inline void freeAligned(void* ptr)
189 {
190  // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
191  // deprecated exception specification.
192  return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
193 }
194 
195 //-------------------------------------------------------------------------------------------------
196 // Implementation.
197 
198 // The order of overloads below is important - it defines which will be chosen by inline functions.
199 NX_KIT_API std::string toString(bool b);
200 NX_KIT_API std::string toString(const void* ptr);
201 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
202 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
203 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
204 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
205 NX_KIT_API std::string toString(char c);
206 NX_KIT_API std::string toString(const char* s);
207 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
208 NX_KIT_API std::string toString(wchar_t c);
209 NX_KIT_API std::string toString(const wchar_t* w);
210 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
211 
212 // std::string can contain '\0' inside, hence a dedicated implementation.
213 NX_KIT_API std::string toString(const std::string& s);
214 NX_KIT_API std::string toString(const std::wstring& w);
215 
216 // For unknown types, use their operator<<().
217 template<typename T>
218 std::string toString(T value)
219 {
220  std::ostringstream outputString;
221  outputString << value;
222  return outputString.str();
223 }
224 
225 #if defined(QT_CORE_LIB)
226 
227 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
228 {
229  return toString(b.toStdString());
230 }
231 
232 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
233 {
234  return toString(s.toUtf8().constData());
235 }
236 
237 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
238 {
239  return toString(u.toEncoded().toStdString());
240 }
241 
242 #endif // defined(QT_CORE_LIB)
243 
244 template<typename P>
245 std::string toString(P* ptr)
246 {
247  return toString((const void*) ptr);
248 }
249 
250 } // namespace utils
251 } // namespace kit
252 } // 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:119
uint8_t * misalignedPtr(void *data)
Definition: utils.h:130
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:144
Definition: apple_utils.h:6
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:174