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 <string>
17 #include <sstream>
18 #include <vector>
19 
20 #if defined(QT_CORE_LIB)
21  // To be supported in toString().
22  #include <QtCore/QByteArray>
23  #include <QtCore/QString>
24  #include <QtCore/QUrl>
25 #endif
26 
27 #if !defined(NX_KIT_API)
28  #define NX_KIT_API /*empty*/
29 #endif
30 
31 namespace nx {
32 namespace kit {
33 namespace utils {
34 
35 //-------------------------------------------------------------------------------------------------
36 // Strings.
37 
38 inline bool isAsciiPrintable(int c)
39 {
40  return c >= 32 && c <= 126;
41 }
42 
52 NX_KIT_API std::string decodeEscapedString(
53  const std::string& s, std::string* outErrorMessage = nullptr);
54 
61 template<typename T>
62 std::string toString(T value);
63 
67 template<typename... Args>
68 std::string format(const std::string& formatStr, Args... args)
69 {
70  const int size = snprintf(nullptr, 0, formatStr.c_str(), args...) + /*space for \0*/ 1;
71  if (size <= 0)
72  return formatStr; //< No better way to handle out-of-memory-like errors.
73  std::string result(size, '\0');
74  snprintf(&result[0], size, formatStr.c_str(), args...);
75  result.resize(size - /*terminating \0*/ 1);
76  return result;
77 }
78 
79 NX_KIT_API bool fromString(const std::string& s, int* value);
80 NX_KIT_API bool fromString(const std::string& s, double* value);
81 NX_KIT_API bool fromString(const std::string& s, float* value);
82 NX_KIT_API bool fromString(const std::string& s, bool* value);
83 
84 NX_KIT_API void stringReplaceAllChars(std::string* s, char sample, char replacement);
85 NX_KIT_API void stringInsertAfterEach(std::string* s, char sample, const char* insertion);
86 NX_KIT_API void stringReplaceAll(
87  std::string* s, const std::string& sample, const std::string& replacement);
88 
89 //-------------------------------------------------------------------------------------------------
90 // OS support.
91 
96 NX_KIT_API std::string baseName(std::string path);
97 
101 NX_KIT_API std::string getProcessName();
102 
107 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
108 
109 NX_KIT_API bool fileExists(const char* filename);
110 
111 //-------------------------------------------------------------------------------------------------
112 // Aligned allocation.
113 
118 inline size_t alignUp(size_t value, size_t alignment)
119 {
120  if (alignment == 0)
121  return value;
122  const size_t remainder = value % alignment;
123  if (remainder == 0)
124  return value;
125  return value + alignment - remainder;
126 }
127 
129 inline uint8_t* misalignedPtr(void* data)
130 {
131  return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
132 }
133 
142 template<class MallocFunc>
143 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
144 {
145  if (alignment == 0)
146  return nullptr;
147  const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
148  if (!ptr) //< allocation error
149  return ptr;
150 
151  char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
152  const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
153  memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
154  return alignedPtr + misalignment;
155 }
156 
158 inline void* mallocAligned(size_t size, size_t alignment)
159 {
160  // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
161  // deprecated exception specification.
162  return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
163 }
164 
172 template<class FreeFunc>
173 void freeAligned(void* ptr, FreeFunc freeFunc)
174 {
175  if (!ptr)
176  return freeFunc(ptr);
177 
178  ptr = (char*) ptr - sizeof(size_t);
179  size_t misalignment = 0;
180  memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
181  ptr = (char*) ptr - misalignment;
182 
183  freeFunc(ptr);
184 }
185 
187 inline void freeAligned(void* ptr)
188 {
189  // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
190  // deprecated exception specification.
191  return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
192 }
193 
194 //-------------------------------------------------------------------------------------------------
195 // Implementation.
196 
197 // The order of overloads below is important - it defines which will be chosen by inline functions.
198 NX_KIT_API std::string toString(bool b);
199 NX_KIT_API std::string toString(const void* ptr);
200 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
201 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
202 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
203 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
204 NX_KIT_API std::string toString(char c);
205 NX_KIT_API std::string toString(const char* s);
206 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
207 NX_KIT_API std::string toString(wchar_t c);
208 NX_KIT_API std::string toString(const wchar_t* w);
209 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
210 
211 // std::string can contain '\0' inside, hence a dedicated implementation.
212 NX_KIT_API std::string toString(const std::string& s);
213 NX_KIT_API std::string toString(const std::wstring& w);
214 
215 
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:68
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:118
uint8_t * misalignedPtr(void *data)
Definition: utils.h:129
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:143
Definition: apple_utils.h:6
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:173