1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/*
** Copyright (c) 2021-2022 LunarG, Inc.
** Copyright (c) 2022 Valve Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/
/// @file Facilities for the conversion of types to strings, oriented towards
/// JSON Lines output.
#ifndef GFXRECON_TO_STRING_H
#define GFXRECON_TO_STRING_H
#include "format/format.h"
#include "util/defines.h"
#include <codecvt>
#include <locale>
#include <sstream>
#include <string>
#include <utility>
GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(util)
enum ToStringFlagBits
{
kToString_Unformatted = 0,
kToString_Formatted = 1,
kToString_Default = kToString_Unformatted,
};
typedef uint32_t ToStringFlags;
/// @brief A template ToString to take care of simple POD cases like the many
/// types of integers and the 32 bit and 64 bit floating point types.
template <typename T>
inline std::string
ToString(const T& obj, ToStringFlags toStringFlags = kToString_Default, uint32_t tabCount = 0, uint32_t tabSize = 4)
{
GFXRECON_UNREFERENCED_PARAMETER(toStringFlags);
GFXRECON_UNREFERENCED_PARAMETER(tabCount);
GFXRECON_UNREFERENCED_PARAMETER(tabSize);
return std::to_string(obj);
}
/// @brief A template that exists only to allow the ToStrings for 32 bit sets of
/// flags to be template specializations.
/// It is never called and its return value of "0" is a meaningless placeholder
/// to allow compilation to succeed.
/// @note There seems to be no reason for those ToStrings to be template
/// function specializations since a caller has to explicitly spell out a type
/// to call one of them and there is no function resolution based on argument
/// types going on.
template <typename T>
inline std::string ToString(uint32_t apiFlags,
ToStringFlags toStringFlags = kToString_Default,
uint32_t tabCount = 0,
uint32_t tabSize = 4)
{
GFXRECON_UNREFERENCED_PARAMETER(apiFlags);
GFXRECON_UNREFERENCED_PARAMETER(toStringFlags);
GFXRECON_UNREFERENCED_PARAMETER(tabCount);
GFXRECON_UNREFERENCED_PARAMETER(tabSize);
return "0";
}
template <typename HandleIdType>
inline std::string HandleIdToString(HandleIdType handleId)
{
std::stringstream strStrm;
if (handleId)
{
strStrm << "\"0x" << reinterpret_cast<const void*>(handleId) << "\"";
}
else
{
strStrm << "\"NULL\"";
}
return strStrm.str();
}
template <>
inline std::string HandleIdToString(format::HandleId handleId)
{
return std::to_string(handleId);
}
inline std::string WCharArrayToString(const wchar_t* pStr)
{
auto str = pStr ? std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>().to_bytes(pStr) : std::string();
for (auto i = str.find('\\'); i != std::string::npos; i = str.find('\\'))
{
str = str.replace(i, 1, "-");
}
return str;
}
template <typename BitmaskType, typename FlagsType>
inline std::string BitmaskToString(FlagsType flags)
{
std::string str;
FlagsType index = 0;
while (flags)
{
if (flags & 1)
{
if (!str.empty())
{
str.append("|");
}
str.append(ToString(static_cast<BitmaskType>(1 << index)));
}
++index;
flags >>= 1;
}
if (str.empty())
{
str.append(ToString(static_cast<BitmaskType>(0)));
}
return str;
}
inline std::string Bool32ToString(const /* Don't take the header dependency for one typedef: VkBool32*/ uint32_t b)
{
return b ? "true" : "false";
}
template <typename PtrType>
inline std::string PtrToString(PtrType* ptr)
{
std::stringstream strStrm;
strStrm << "0x" << std::hex << reinterpret_cast<uintptr_t>(ptr);
return strStrm.str();
}
template <typename PtrType>
inline std::string PtrToString(PtrType ptr)
{
std::stringstream strStrm;
strStrm << "0x" << std::hex << ptr;
return strStrm.str();
}
inline std::string GetNewlineString(ToStringFlags toStringFlags)
{
return (toStringFlags & kToString_Formatted) ? std::string("\n") : std::string();
}
inline std::string GetWhitespaceString(ToStringFlags toStringFlags, uint32_t count = 1)
{
return (toStringFlags & kToString_Formatted) ? std::string((size_t)count, ' ') : std::string();
}
inline std::string GetTabString(ToStringFlags toStringFlags, uint32_t tabCount, uint32_t tabSize = 4)
{
return GetWhitespaceString(toStringFlags, tabCount * tabSize);
}
template <typename ToStringFunctionType>
inline std::string
ObjectToString(ToStringFlags toStringFlags, uint32_t& tabCount, uint32_t tabSize, ToStringFunctionType toStringFunction)
{
std::stringstream strStrm;
const auto nl = GetNewlineString(toStringFlags);
strStrm << '{';
strStrm << nl;
++tabCount;
toStringFunction(strStrm);
--tabCount;
strStrm << nl;
strStrm << GetTabString(toStringFlags, tabCount, tabSize);
strStrm << '}';
return strStrm.str();
}
inline void FieldToString(std::stringstream& strStrm,
bool firstField,
const char* fieldName,
ToStringFlags toStringFlags,
uint32_t tabCount,
uint32_t tabSize,
const std::string& fieldString)
{
if (!firstField)
{
strStrm << ',' << GetNewlineString(toStringFlags);
}
strStrm << GetTabString(toStringFlags, tabCount, tabSize);
strStrm << "\"" << fieldName << "\":";
strStrm << GetWhitespaceString(toStringFlags);
strStrm << fieldString;
}
template <typename ObjectType, typename ValidateArrayFunctionType, typename ToStringFunctionType>
inline std::string ArrayToString(size_t count,
const ObjectType* pObjs,
ToStringFlags toStringFlags,
uint32_t tabCount,
uint32_t tabSize,
ValidateArrayFunctionType validateArrayFunction,
ToStringFunctionType toStringFunction)
{
if (!(count && validateArrayFunction()))
{
return "null";
}
std::stringstream strStrm;
strStrm << '[';
strStrm << GetNewlineString(toStringFlags);
for (uint32_t i = 0; i < count; ++i)
{
if (i)
{
strStrm << ',' << GetNewlineString(toStringFlags);
}
strStrm << GetTabString(toStringFlags, tabCount + 1, tabSize) << toStringFunction(i);
}
strStrm << GetNewlineString(toStringFlags) << GetTabString(toStringFlags, tabCount, tabSize);
strStrm << ']';
return strStrm.str();
}
template <typename T>
inline std::string ArrayToString(size_t count,
const T* pObjs,
ToStringFlags toStringFlags = kToString_Default,
uint32_t tabCount = 0,
uint32_t tabSize = 4)
{
return ArrayToString(
count,
pObjs,
toStringFlags,
tabCount,
tabSize,
[&]() { return pObjs != nullptr; },
[&](size_t i) { return ToString(pObjs[i], toStringFlags, tabCount + 1, tabSize); });
}
template <typename T>
inline std::string Array2DToString(size_t m,
size_t n,
const T* const* pObjs,
ToStringFlags toStringFlags = kToString_Default,
uint32_t tabCount = 0,
uint32_t tabSize = 4)
{
std::stringstream strStrm;
for (size_t i = 0; i < m; ++i)
{
strStrm << ArrayToString(n, pObjs[i], toStringFlags, tabCount, tabSize);
}
return strStrm.str();
}
/// Replace special characters with their escaped versions.
/// @note forward slash / solidus is not escaped as that is optional and leads
/// to ugliness such as dates with escaped solidus separators.
/// @note Slashes for explicit unicode code points will be erroneously escaped
/// but Vulkan-derived C-strings should not have those embedded.
inline void JSONEscape(const char c, std::string& out)
{
char out_c = c;
switch (c)
{
case '\"':
case '\\':
out.push_back('\\');
break;
case '\b':
out.push_back('\\');
out_c = 'b';
break;
case '\f':
out.push_back('\\');
out_c = 'f';
break;
case '\n':
out.push_back('\\');
out_c = 'n';
break;
case '\r':
out.push_back('\\');
out_c = 'r';
break;
case '\t':
out.push_back('\\');
out_c = 't';
break;
}
out.push_back(out_c);
}
/// Replace special characters in strings with their escaped versions.
/// <https://www.json.org/json-en.html>
inline void JSONEscape(const char* cstr, std::string& escaped)
{
if (cstr)
{
while (char c = *cstr++)
{
JSONEscape(c, escaped);
}
}
}
inline std::string JSONEscape(const std::string& str)
{
std::string escaped;
for (const auto c : str)
{
JSONEscape(c, escaped);
}
return escaped;
}
/// @brief A single point for the conversion of C-style strings to the JSON
/// string type or null.
inline std::string CStrToString(const char* const cstr)
{
std::string str;
if (cstr != nullptr)
{
str.push_back('"');
JSONEscape(cstr, str);
str.push_back('"');
}
else
{
str.assign("null");
}
return str;
}
/// @brief Convert an array of c-style string pointers into a JSON array of
/// JSON strings or nulls.
inline std::string CStrArrayToString(size_t count,
const char* const* ppStrs,
ToStringFlags toStringFlags = kToString_Default,
uint32_t tabCount = 0,
uint32_t tabSize = 4)
{
return ArrayToString(
count,
ppStrs,
toStringFlags,
tabCount,
tabSize,
[&]() { return ppStrs != nullptr; },
[&](uint32_t i) { return CStrToString(ppStrs[i]); });
}
template <typename EnumType>
inline std::string EnumArrayToString(size_t count,
const EnumType* pObjs,
util::ToStringFlags toStringFlags = util::kToString_Default,
uint32_t tabCount = 0,
uint32_t tabSize = 4)
{
using namespace util;
return ArrayToString(
count,
pObjs,
toStringFlags,
tabCount,
tabSize,
[&]() { return pObjs != nullptr; },
[&](size_t i) { return '"' + ToString(pObjs[i], toStringFlags, tabCount + 1, tabSize) + '"'; });
}
/// @brief Make a copy of the input string with double quotes at start and end.
inline std::string Quote(const std::string& str)
{
std::string quoted{ '"' };
quoted += str;
quoted += '"';
return quoted;
}
/// @brief Make a copy of the input string with double quotes at start and end.
inline std::string Quote(const char* const str)
{
std::string quoted{ '"' };
if (str != nullptr)
{
quoted += str;
}
quoted += '"';
return quoted;
}
GFXRECON_END_NAMESPACE(util)
GFXRECON_END_NAMESPACE(gfxrecon)
#endif // GFXRECON_TO_STRING_H
|