nx_metadata_sdk  1.0
Metadata 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 
104 NX_KIT_API std::string absolutePath(
105  const std::string& originDir, const std::string& path);
106 
110 NX_KIT_API std::string getProcessName();
111 
116 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
117 
118 NX_KIT_API bool fileExists(const char* filename);
119 
120 //-------------------------------------------------------------------------------------------------
121 // Aligned allocation.
122 
127 inline size_t alignUp(size_t value, size_t alignment)
128 {
129  if (alignment == 0)
130  return value;
131  const size_t remainder = value % alignment;
132  if (remainder == 0)
133  return value;
134  return value + alignment - remainder;
135 }
136 
138 inline uint8_t* misalignedPtr(void* data)
139 {
140  return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
141 }
142 
151 template<class MallocFunc>
152 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
153 {
154  if (alignment == 0)
155  return nullptr;
156  const auto ptr = (char*) mallocFunc(size + alignment + sizeof(alignment));
157  if (!ptr) //< allocation error
158  return ptr;
159 
160  char* const alignedPtr = ptr + sizeof(alignment); //< Leaving place to save misalignment.
161  const size_t misalignment = alignment - (uintptr_t) alignedPtr % alignment;
162  memcpy(ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
163  return alignedPtr + misalignment;
164 }
165 
167 inline void* mallocAligned(size_t size, size_t alignment)
168 {
169  // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
170  // deprecated exception specification.
171  return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
172 }
173 
181 template<class FreeFunc>
182 void freeAligned(void* ptr, FreeFunc freeFunc)
183 {
184  if (!ptr)
185  return freeFunc(ptr);
186 
187  ptr = (char*) ptr - sizeof(size_t);
188  size_t misalignment = 0;
189  memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
190  ptr = (char*) ptr - misalignment;
191 
192  freeFunc(ptr);
193 }
194 
196 inline void freeAligned(void* ptr)
197 {
198  // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
199  // deprecated exception specification.
200  return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
201 }
202 
203 //-------------------------------------------------------------------------------------------------
204 // Implementation.
205 
206 // The order of overloads below is important - it defines which will be chosen by inline functions.
207 NX_KIT_API std::string toString(bool b);
208 NX_KIT_API std::string toString(const void* ptr);
209 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
210 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
211 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
212 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
213 NX_KIT_API std::string toString(char c);
214 NX_KIT_API std::string toString(const char* s);
215 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
216 NX_KIT_API std::string toString(wchar_t c);
217 NX_KIT_API std::string toString(const wchar_t* w);
218 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
219 
220 // std::string can contain '\0' inside, hence a dedicated implementation.
221 NX_KIT_API std::string toString(const std::string& s);
222 NX_KIT_API std::string toString(const std::wstring& w);
223 
224 
226 template<typename T>
227 std::string toString(T value)
228 {
229  std::ostringstream outputString;
230  outputString << value;
231  return outputString.str();
232 }
233 
234 #if defined(QT_CORE_LIB)
235 
236 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
237 {
238  return toString(b.toStdString());
239 }
240 
241 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
242 {
243  return toString(s.toUtf8().constData());
244 }
245 
246 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
247 {
248  return toString(u.toEncoded().toStdString());
249 }
250 
251 #endif // defined(QT_CORE_LIB)
252 
253 template<typename P>
254 std::string toString(P* ptr)
255 {
256  return toString((const void*) ptr);
257 }
258 
259 } // namespace utils
260 } // namespace kit
261 } // 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:127
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:152
Definition: apple_utils.h:6
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:182
uint8_t * misalignedPtr(void *data)
Definition: utils.h:138