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 <cstdlib>
15 #include <memory>
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
47 NX_KIT_API std::string baseName(std::string path);
48
52 NX_KIT_API std::string getProcessName();
53
59 template<typename T>
60 std::string toString(T value);
61
62 template<typename... Args>
63 std::string format(const std::string& formatStr, Args... args)
64 {
65 const size_t size = snprintf(nullptr, 0, formatStr.c_str(), args...) + /* space for '\0' */ 1;
66 if (size < 0)
67 return formatStr; //< No better way to handle out-of-memory-like errors.
68 std::string result(size, '\0');
69 snprintf(&result[0], size, formatStr.c_str(), args...);
70 result.resize(size - /* trailing '\0' */ 1);
71 return result;
72 }
73
74 NX_KIT_API bool fromString(const std::string& s, int* value);
75 NX_KIT_API bool fromString(const std::string& s, double* value);
76 NX_KIT_API bool fromString(const std::string& s, float* value);
77
78 //-------------------------------------------------------------------------------------------------
79 // OS support.
80
85 NX_KIT_API const std::vector<std::string>& getProcessCmdLineArgs();
86
87 //-------------------------------------------------------------------------------------------------
88 // Aligned allocation.
89
94 inline size_t alignUp(size_t value, size_t alignment)
95 {
96 if (alignment == 0)
97 return value;
98 const size_t remainder = value % alignment;
99 if (remainder == 0)
100 return value;
101 return value + alignment - remainder;
102 }
103
105 inline uint8_t* misalignedPtr(void* data)
106 {
107 return (uint8_t*) (17 + alignUp((uintptr_t) data, 32));
108 }
109
118 template<class MallocFunc>
119 void* mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
120 {
121 if (alignment == 0)
122 return 0;
123 void* ptr = mallocFunc(size + alignment + sizeof(alignment));
124 if (!ptr) //< allocation error
125 return ptr;
126
127 void* aligned_ptr = (char*) ptr + sizeof(alignment); //< Leaving place to save misalignment.
128 const size_t misalignment = alignment - (((uintptr_t) aligned_ptr) % alignment);
129 memcpy((char*) ptr + misalignment, &misalignment, sizeof(misalignment)); //< Save misalignment.
130 return (char*) aligned_ptr + misalignment;
131 }
132
134 inline void* mallocAligned(size_t size, size_t alignment)
135 {
136 // NOTE: Lambda is used to suppress a warning that some ::malloc() implementations are using
137 // deprecated exception specification.
138 return mallocAligned<>(size, alignment, [](size_t size) { return ::malloc(size); });
139 }
140
148 template<class FreeFunc>
149 void freeAligned(void* ptr, FreeFunc freeFunc)
150 {
151 if (!ptr)
152 return freeFunc(ptr);
153
154 ptr = (char*) ptr - sizeof(size_t);
155 size_t misalignment = 0;
156 memcpy(&misalignment, ptr, sizeof(misalignment)); //< Retrieve saved misalignment.
157 ptr = (char*) ptr - misalignment;
158
159 freeFunc(ptr);
160 }
161
163 inline void freeAligned(void* ptr)
164 {
165 // NOTE: Lambda is used to suppress a warning that some ::free() implementations are using
166 // deprecated exception specification.
167 return freeAligned<>(ptr, [](void* ptr) { return ::free(ptr); });
168 }
169
170 //-------------------------------------------------------------------------------------------------
171 // Implementation.
172
173 // The order of overloads below is important - it defines which will be chosen by inline functions.
174 NX_KIT_API std::string toString(bool b);
175 NX_KIT_API std::string toString(const void* ptr);
176 inline std::string toString(void* ptr) { return toString(const_cast<const void*>(ptr)); }
177 inline std::string toString(std::nullptr_t ptr) { return toString((const void*) ptr); }
178 inline std::string toString(uint8_t i) { return toString((int) i); } //< Avoid matching as char.
179 inline std::string toString(int8_t i) { return toString((int) i); } //< Avoid matching as char.
180 NX_KIT_API std::string toString(char c);
181 NX_KIT_API std::string toString(const char* s);
182 inline std::string toString(char* s) { return toString(const_cast<const char*>(s)); }
183 inline std::string toString(const std::string& s) { return toString(s.c_str()); }
184 NX_KIT_API std::string toString(wchar_t c);
185 NX_KIT_API std::string toString(const wchar_t* w);
186 inline std::string toString(wchar_t* w) { return toString(const_cast<const wchar_t*>(w)); }
187 inline std::string toString(const std::wstring& w) { return toString(w.c_str()); }
188
189 // For unknown types, use their operator<<().
190 template<typename T>
191 std::string toString(T value)
192 {
193 std::ostringstream outputString;
194 outputString << value;
195 return outputString.str();
196 }
197
198 #if defined(QT_CORE_LIB)
199
200 static inline std::string toString(QByteArray b) //< By value to avoid calling the template impl.
201 {
202 return toString(b.toStdString());
203 }
204
205 static inline std::string toString(QString s) //< By value to avoid calling the template impl.
206 {
207 return toString(s.toUtf8().constData());
208 }
209
210 static inline std::string toString(QUrl u) //< By value to avoid calling the template impl.
211 {
212 return toString(u.toEncoded().toStdString());
213 }
214
215 #endif // defined(QT_CORE_LIB)
216
217 template<typename P>
218 std::string toString(P* ptr)
219 {
220 return toString((const void*) ptr);
221 }
222
223 } // namespace utils
224 } // namespace kit
225 } // namespace nx
nx::kit::utils::alignUp
size_t alignUp(size_t value, size_t alignment)
Definition: utils.h:94
nx::kit::utils::mallocAligned
void * mallocAligned(size_t size, size_t alignment, MallocFunc mallocFunc)
Definition: utils.h:119
nx
Definition: apple_utils.h:6
nx::kit::utils::freeAligned
void freeAligned(void *ptr, FreeFunc freeFunc)
Definition: utils.h:149
nx::kit::utils::misalignedPtr
uint8_t * misalignedPtr(void *data)
Definition: utils.h:105