diff --git a/3rdparty/discord-rpc/CMakeLists.txt b/3rdparty/discord-rpc/CMakeLists.txt
new file mode 100644
index 0000000000..0c06da5301
--- /dev/null
+++ b/3rdparty/discord-rpc/CMakeLists.txt
@@ -0,0 +1,37 @@
+set(SRCS
+ include/discord_register.h
+ include/discord_rpc.h
+ src/backoff.h
+ src/connection.h
+ src/discord_rpc.cpp
+ src/msg_queue.h
+ src/rpc_connection.cpp
+ src/rpc_connection.h
+ src/serialization.cpp
+ src/serialization.h
+)
+
+add_library(discord-rpc ${SRCS})
+target_include_directories(discord-rpc PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
+target_link_libraries(discord-rpc rapidjson)
+
+# Needed for macOS compile.
+set_property(TARGET discord-rpc PROPERTY CXX_STANDARD 17)
+set_property(TARGET discord-rpc PROPERTY CXX_STANDARD_REQUIRED ON)
+
+if(WIN32)
+ target_sources(discord-rpc PRIVATE
+ src/connection_win.cpp
+ src/discord_register_win.cpp
+ )
+elseif(APPLE)
+ target_sources(discord-rpc PRIVATE
+ src/connection_unix.cpp
+ src/discord_register_osx.m
+ )
+elseif(UNIX)
+ target_sources(discord-rpc PRIVATE
+ src/connection_unix.cpp
+ src/discord_register_linux.cpp
+ )
+endif()
diff --git a/3rdparty/discord-rpc/LICENSE b/3rdparty/discord-rpc/LICENSE
new file mode 100644
index 0000000000..17fca3d50f
--- /dev/null
+++ b/3rdparty/discord-rpc/LICENSE
@@ -0,0 +1,19 @@
+Copyright 2017 Discord, Inc.
+
+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.
diff --git a/3rdparty/discord-rpc/discord-rpc.vcxproj b/3rdparty/discord-rpc/discord-rpc.vcxproj
new file mode 100644
index 0000000000..c0d710ba29
--- /dev/null
+++ b/3rdparty/discord-rpc/discord-rpc.vcxproj
@@ -0,0 +1,55 @@
+
+
+
+
+
+ {E960DFDF-1BD3-4C29-B251-D1A0919C9B09}
+
+
+
+ StaticLibrary
+ $(DefaultPlatformToolset)
+ MultiByte
+ true
+ true
+ false
+
+
+
+
+
+
+
+
+
+
+
+
+
+ AllRules.ruleset
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ TurnOffAllWarnings
+ $(SolutionDir)3rdparty\rapidjson\include;$(ProjectDir)include;%(AdditionalIncludeDirectories)
+
+
+
+
+
\ No newline at end of file
diff --git a/3rdparty/discord-rpc/discord-rpc.vcxproj.filters b/3rdparty/discord-rpc/discord-rpc.vcxproj.filters
new file mode 100644
index 0000000000..2bd46495cd
--- /dev/null
+++ b/3rdparty/discord-rpc/discord-rpc.vcxproj.filters
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/3rdparty/discord-rpc/include/discord_register.h b/3rdparty/discord-rpc/include/discord_register.h
new file mode 100644
index 0000000000..16fb42f328
--- /dev/null
+++ b/3rdparty/discord-rpc/include/discord_register.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#if defined(DISCORD_DYNAMIC_LIB)
+#if defined(_WIN32)
+#if defined(DISCORD_BUILDING_SDK)
+#define DISCORD_EXPORT __declspec(dllexport)
+#else
+#define DISCORD_EXPORT __declspec(dllimport)
+#endif
+#else
+#define DISCORD_EXPORT __attribute__((visibility("default")))
+#endif
+#else
+#define DISCORD_EXPORT
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command);
+DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId, const char* steamId);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/3rdparty/discord-rpc/include/discord_rpc.h b/3rdparty/discord-rpc/include/discord_rpc.h
new file mode 100644
index 0000000000..3e1441e058
--- /dev/null
+++ b/3rdparty/discord-rpc/include/discord_rpc.h
@@ -0,0 +1,87 @@
+#pragma once
+#include
+
+// clang-format off
+
+#if defined(DISCORD_DYNAMIC_LIB)
+# if defined(_WIN32)
+# if defined(DISCORD_BUILDING_SDK)
+# define DISCORD_EXPORT __declspec(dllexport)
+# else
+# define DISCORD_EXPORT __declspec(dllimport)
+# endif
+# else
+# define DISCORD_EXPORT __attribute__((visibility("default")))
+# endif
+#else
+# define DISCORD_EXPORT
+#endif
+
+// clang-format on
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct DiscordRichPresence {
+ const char* state; /* max 128 bytes */
+ const char* details; /* max 128 bytes */
+ int64_t startTimestamp;
+ int64_t endTimestamp;
+ const char* largeImageKey; /* max 32 bytes */
+ const char* largeImageText; /* max 128 bytes */
+ const char* smallImageKey; /* max 32 bytes */
+ const char* smallImageText; /* max 128 bytes */
+ const char* partyId; /* max 128 bytes */
+ int partySize;
+ int partyMax;
+ const char* matchSecret; /* max 128 bytes */
+ const char* joinSecret; /* max 128 bytes */
+ const char* spectateSecret; /* max 128 bytes */
+ int8_t instance;
+} DiscordRichPresence;
+
+typedef struct DiscordUser {
+ const char* userId;
+ const char* username;
+ const char* discriminator;
+ const char* avatar;
+} DiscordUser;
+
+typedef struct DiscordEventHandlers {
+ void (*ready)(const DiscordUser* request);
+ void (*disconnected)(int errorCode, const char* message);
+ void (*errored)(int errorCode, const char* message);
+ void (*joinGame)(const char* joinSecret);
+ void (*spectateGame)(const char* spectateSecret);
+ void (*joinRequest)(const DiscordUser* request);
+} DiscordEventHandlers;
+
+#define DISCORD_REPLY_NO 0
+#define DISCORD_REPLY_YES 1
+#define DISCORD_REPLY_IGNORE 2
+
+DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
+ DiscordEventHandlers* handlers,
+ int autoRegister,
+ const char* optionalSteamId);
+DISCORD_EXPORT void Discord_Shutdown(void);
+
+/* checks for incoming messages, dispatches callbacks */
+DISCORD_EXPORT void Discord_RunCallbacks(void);
+
+/* If you disable the lib starting its own io thread, you'll need to call this from your own */
+#ifdef DISCORD_DISABLE_IO_THREAD
+DISCORD_EXPORT void Discord_UpdateConnection(void);
+#endif
+
+DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence);
+DISCORD_EXPORT void Discord_ClearPresence(void);
+
+DISCORD_EXPORT void Discord_Respond(const char* userid, /* DISCORD_REPLY_ */ int reply);
+
+DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* handlers);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
diff --git a/3rdparty/discord-rpc/src/backoff.h b/3rdparty/discord-rpc/src/backoff.h
new file mode 100644
index 0000000000..a3e736fb7b
--- /dev/null
+++ b/3rdparty/discord-rpc/src/backoff.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include
+#include
+#include
+#include
+
+struct Backoff {
+ int64_t minAmount;
+ int64_t maxAmount;
+ int64_t current;
+ int fails;
+ std::mt19937_64 randGenerator;
+ std::uniform_real_distribution<> randDistribution;
+
+ double rand01() { return randDistribution(randGenerator); }
+
+ Backoff(int64_t min, int64_t max)
+ : minAmount(min)
+ , maxAmount(max)
+ , current(min)
+ , fails(0)
+ , randGenerator((uint64_t)time(0))
+ {
+ }
+
+ void reset()
+ {
+ fails = 0;
+ current = minAmount;
+ }
+
+ int64_t nextDelay()
+ {
+ ++fails;
+ int64_t delay = (int64_t)((double)current * 2.0 * rand01());
+ current = std::min(current + delay, maxAmount);
+ return current;
+ }
+};
diff --git a/3rdparty/discord-rpc/src/connection.h b/3rdparty/discord-rpc/src/connection.h
new file mode 100644
index 0000000000..a8f99b9f10
--- /dev/null
+++ b/3rdparty/discord-rpc/src/connection.h
@@ -0,0 +1,19 @@
+#pragma once
+
+// This is to wrap the platform specific kinds of connect/read/write.
+
+#include
+#include
+
+// not really connectiony, but need per-platform
+int GetProcessId();
+
+struct BaseConnection {
+ static BaseConnection* Create();
+ static void Destroy(BaseConnection*&);
+ bool isOpen{false};
+ bool Open();
+ bool Close();
+ bool Write(const void* data, size_t length);
+ bool Read(void* data, size_t length);
+};
diff --git a/3rdparty/discord-rpc/src/connection_unix.cpp b/3rdparty/discord-rpc/src/connection_unix.cpp
new file mode 100644
index 0000000000..85dace3ccc
--- /dev/null
+++ b/3rdparty/discord-rpc/src/connection_unix.cpp
@@ -0,0 +1,125 @@
+#include "connection.h"
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+int GetProcessId()
+{
+ return ::getpid();
+}
+
+struct BaseConnectionUnix : public BaseConnection {
+ int sock{-1};
+};
+
+static BaseConnectionUnix Connection;
+static sockaddr_un PipeAddr{};
+#ifdef MSG_NOSIGNAL
+static int MsgFlags = MSG_NOSIGNAL;
+#else
+static int MsgFlags = 0;
+#endif
+
+static const char* GetTempPath()
+{
+ const char* temp = getenv("XDG_RUNTIME_DIR");
+ temp = temp ? temp : getenv("TMPDIR");
+ temp = temp ? temp : getenv("TMP");
+ temp = temp ? temp : getenv("TEMP");
+ temp = temp ? temp : "/tmp";
+ return temp;
+}
+
+/*static*/ BaseConnection* BaseConnection::Create()
+{
+ PipeAddr.sun_family = AF_UNIX;
+ return &Connection;
+}
+
+/*static*/ void BaseConnection::Destroy(BaseConnection*& c)
+{
+ auto self = reinterpret_cast(c);
+ self->Close();
+ c = nullptr;
+}
+
+bool BaseConnection::Open()
+{
+ const char* tempPath = GetTempPath();
+ auto self = reinterpret_cast(this);
+ self->sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (self->sock == -1) {
+ return false;
+ }
+ fcntl(self->sock, F_SETFL, O_NONBLOCK);
+#ifdef SO_NOSIGPIPE
+ int optval = 1;
+ setsockopt(self->sock, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval));
+#endif
+
+ for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
+ snprintf(
+ PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum);
+ int err = connect(self->sock, (const sockaddr*)&PipeAddr, sizeof(PipeAddr));
+ if (err == 0) {
+ self->isOpen = true;
+ return true;
+ }
+ }
+ self->Close();
+ return false;
+}
+
+bool BaseConnection::Close()
+{
+ auto self = reinterpret_cast(this);
+ if (self->sock == -1) {
+ return false;
+ }
+ close(self->sock);
+ self->sock = -1;
+ self->isOpen = false;
+ return true;
+}
+
+bool BaseConnection::Write(const void* data, size_t length)
+{
+ auto self = reinterpret_cast(this);
+
+ if (self->sock == -1) {
+ return false;
+ }
+
+ ssize_t sentBytes = send(self->sock, data, length, MsgFlags);
+ if (sentBytes < 0) {
+ Close();
+ }
+ return sentBytes == (ssize_t)length;
+}
+
+bool BaseConnection::Read(void* data, size_t length)
+{
+ auto self = reinterpret_cast(this);
+
+ if (self->sock == -1) {
+ return false;
+ }
+
+ int res = (int)recv(self->sock, data, length, MsgFlags);
+ if (res < 0) {
+ if (errno == EAGAIN) {
+ return false;
+ }
+ Close();
+ }
+ else if (res == 0) {
+ Close();
+ }
+ return res == (int)length;
+}
diff --git a/3rdparty/discord-rpc/src/connection_win.cpp b/3rdparty/discord-rpc/src/connection_win.cpp
new file mode 100644
index 0000000000..2dd2750c06
--- /dev/null
+++ b/3rdparty/discord-rpc/src/connection_win.cpp
@@ -0,0 +1,128 @@
+#include "connection.h"
+
+#define WIN32_LEAN_AND_MEAN
+#define NOMCX
+#define NOSERVICE
+#define NOIME
+#include
+#include
+
+int GetProcessId()
+{
+ return (int)::GetCurrentProcessId();
+}
+
+struct BaseConnectionWin : public BaseConnection {
+ HANDLE pipe{INVALID_HANDLE_VALUE};
+};
+
+static BaseConnectionWin Connection;
+
+/*static*/ BaseConnection* BaseConnection::Create()
+{
+ return &Connection;
+}
+
+/*static*/ void BaseConnection::Destroy(BaseConnection*& c)
+{
+ auto self = reinterpret_cast(c);
+ self->Close();
+ c = nullptr;
+}
+
+bool BaseConnection::Open()
+{
+ wchar_t pipeName[]{L"\\\\?\\pipe\\discord-ipc-0"};
+ const size_t pipeDigit = sizeof(pipeName) / sizeof(wchar_t) - 2;
+ pipeName[pipeDigit] = L'0';
+ auto self = reinterpret_cast(this);
+ for (;;) {
+ self->pipe = ::CreateFileW(
+ pipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
+ if (self->pipe != INVALID_HANDLE_VALUE) {
+ self->isOpen = true;
+ return true;
+ }
+
+ auto lastError = GetLastError();
+ if (lastError == ERROR_FILE_NOT_FOUND) {
+ if (pipeName[pipeDigit] < L'9') {
+ pipeName[pipeDigit]++;
+ continue;
+ }
+ }
+ else if (lastError == ERROR_PIPE_BUSY) {
+ if (!WaitNamedPipeW(pipeName, 10000)) {
+ return false;
+ }
+ continue;
+ }
+ return false;
+ }
+}
+
+bool BaseConnection::Close()
+{
+ auto self = reinterpret_cast(this);
+ ::CloseHandle(self->pipe);
+ self->pipe = INVALID_HANDLE_VALUE;
+ self->isOpen = false;
+ return true;
+}
+
+bool BaseConnection::Write(const void* data, size_t length)
+{
+ if (length == 0) {
+ return true;
+ }
+ auto self = reinterpret_cast(this);
+ assert(self);
+ if (!self) {
+ return false;
+ }
+ if (self->pipe == INVALID_HANDLE_VALUE) {
+ return false;
+ }
+ assert(data);
+ if (!data) {
+ return false;
+ }
+ const DWORD bytesLength = (DWORD)length;
+ DWORD bytesWritten = 0;
+ return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE &&
+ bytesWritten == bytesLength;
+}
+
+bool BaseConnection::Read(void* data, size_t length)
+{
+ assert(data);
+ if (!data) {
+ return false;
+ }
+ auto self = reinterpret_cast(this);
+ assert(self);
+ if (!self) {
+ return false;
+ }
+ if (self->pipe == INVALID_HANDLE_VALUE) {
+ return false;
+ }
+ DWORD bytesAvailable = 0;
+ if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
+ if (bytesAvailable >= length) {
+ DWORD bytesToRead = (DWORD)length;
+ DWORD bytesRead = 0;
+ if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) {
+ assert(bytesToRead == bytesRead);
+ return true;
+ }
+ else {
+ Close();
+ }
+ }
+ }
+ else {
+ Close();
+ }
+ return false;
+}
diff --git a/3rdparty/discord-rpc/src/discord_register_linux.cpp b/3rdparty/discord-rpc/src/discord_register_linux.cpp
new file mode 100644
index 0000000000..dd92eea0d4
--- /dev/null
+++ b/3rdparty/discord-rpc/src/discord_register_linux.cpp
@@ -0,0 +1,102 @@
+#include "discord_rpc.h"
+#include "discord_register.h"
+#include
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+static bool Mkdir(const char* path)
+{
+ int result = mkdir(path, 0755);
+ if (result == 0) {
+ return true;
+ }
+ if (errno == EEXIST) {
+ return true;
+ }
+ return false;
+}
+
+// we want to register games so we can run them from Discord client as discord-://
+extern "C" DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command)
+{
+ // Add a desktop file and update some mime handlers so that xdg-open does the right thing.
+
+ const char* home = getenv("HOME");
+ if (!home) {
+ return;
+ }
+
+ char exePath[1024];
+ if (!command || !command[0]) {
+ ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath));
+ if (size <= 0 || size >= (ssize_t)sizeof(exePath)) {
+ return;
+ }
+ exePath[size] = '\0';
+ command = exePath;
+ }
+
+ const char* desktopFileFormat = "[Desktop Entry]\n"
+ "Name=Game %s\n"
+ "Exec=%s %%u\n" // note: it really wants that %u in there
+ "Type=Application\n"
+ "NoDisplay=true\n"
+ "Categories=Discord;Games;\n"
+ "MimeType=x-scheme-handler/discord-%s;\n";
+ char desktopFile[2048];
+ int fileLen = snprintf(
+ desktopFile, sizeof(desktopFile), desktopFileFormat, applicationId, command, applicationId);
+ if (fileLen <= 0) {
+ return;
+ }
+
+ char desktopFilename[256];
+ snprintf(desktopFilename, sizeof(desktopFilename), "/discord-%s.desktop", applicationId);
+
+ char desktopFilePath[1024];
+ snprintf(desktopFilePath, sizeof(desktopFilePath), "%s/.local", home);
+ if (!Mkdir(desktopFilePath)) {
+ return;
+ }
+ strcat(desktopFilePath, "/share");
+ if (!Mkdir(desktopFilePath)) {
+ return;
+ }
+ strcat(desktopFilePath, "/applications");
+ if (!Mkdir(desktopFilePath)) {
+ return;
+ }
+ strcat(desktopFilePath, desktopFilename);
+
+ FILE* fp = fopen(desktopFilePath, "w");
+ if (fp) {
+ fwrite(desktopFile, 1, fileLen, fp);
+ fclose(fp);
+ }
+ else {
+ return;
+ }
+
+ char xdgMimeCommand[1024];
+ snprintf(xdgMimeCommand,
+ sizeof(xdgMimeCommand),
+ "xdg-mime default discord-%s.desktop x-scheme-handler/discord-%s",
+ applicationId,
+ applicationId);
+ if (system(xdgMimeCommand) < 0) {
+ fprintf(stderr, "Failed to register mime handler\n");
+ }
+}
+
+extern "C" DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId,
+ const char* steamId)
+{
+ char command[256];
+ sprintf(command, "xdg-open steam://rungameid/%s", steamId);
+ Discord_Register(applicationId, command);
+}
diff --git a/3rdparty/discord-rpc/src/discord_register_osx.m b/3rdparty/discord-rpc/src/discord_register_osx.m
new file mode 100644
index 0000000000..d710102865
--- /dev/null
+++ b/3rdparty/discord-rpc/src/discord_register_osx.m
@@ -0,0 +1,80 @@
+#include
+#include
+
+#import
+
+#include "discord_register.h"
+
+static void RegisterCommand(const char* applicationId, const char* command)
+{
+ // There does not appear to be a way to register arbitrary commands on OSX, so instead we'll save the command
+ // to a file in the Discord config path, and when it is needed, Discord can try to load the file there, open
+ // the command therein (will pass to js's window.open, so requires a url-like thing)
+
+ // Note: will not work for sandboxed apps
+ NSString *home = NSHomeDirectory();
+ if (!home) {
+ return;
+ }
+
+ NSString *path = [[[[[[home stringByAppendingPathComponent:@"Library"]
+ stringByAppendingPathComponent:@"Application Support"]
+ stringByAppendingPathComponent:@"discord"]
+ stringByAppendingPathComponent:@"games"]
+ stringByAppendingPathComponent:[NSString stringWithUTF8String:applicationId]]
+ stringByAppendingPathExtension:@"json"];
+ [[NSFileManager defaultManager] createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
+
+ NSString *jsonBuffer = [NSString stringWithFormat:@"{\"command\": \"%s\"}", command];
+ [jsonBuffer writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil];
+}
+
+static void RegisterURL(const char* applicationId)
+{
+ char url[256];
+ snprintf(url, sizeof(url), "discord-%s", applicationId);
+ CFStringRef cfURL = CFStringCreateWithCString(NULL, url, kCFStringEncodingUTF8);
+
+ NSString* myBundleId = [[NSBundle mainBundle] bundleIdentifier];
+ if (!myBundleId) {
+ fprintf(stderr, "No bundle id found\n");
+ return;
+ }
+
+ NSURL* myURL = [[NSBundle mainBundle] bundleURL];
+ if (!myURL) {
+ fprintf(stderr, "No bundle url found\n");
+ return;
+ }
+
+ OSStatus status = LSSetDefaultHandlerForURLScheme(cfURL, (__bridge CFStringRef)myBundleId);
+ if (status != noErr) {
+ fprintf(stderr, "Error in LSSetDefaultHandlerForURLScheme: %d\n", (int)status);
+ return;
+ }
+
+ status = LSRegisterURL((__bridge CFURLRef)myURL, true);
+ if (status != noErr) {
+ fprintf(stderr, "Error in LSRegisterURL: %d\n", (int)status);
+ }
+}
+
+void Discord_Register(const char* applicationId, const char* command)
+{
+ if (command) {
+ RegisterCommand(applicationId, command);
+ }
+ else {
+ // raii lite
+ @autoreleasepool {
+ RegisterURL(applicationId);
+ }
+ }
+}
+
+void Discord_RegisterSteamGame(const char* applicationId, const char* steamId)
+{
+ char command[256];
+ snprintf(command, 256, "steam://rungameid/%s", steamId);
+ Discord_Register(applicationId, command);
+}
diff --git a/3rdparty/discord-rpc/src/discord_register_win.cpp b/3rdparty/discord-rpc/src/discord_register_win.cpp
new file mode 100644
index 0000000000..0b1c4a13da
--- /dev/null
+++ b/3rdparty/discord-rpc/src/discord_register_win.cpp
@@ -0,0 +1,186 @@
+#include "discord_rpc.h"
+#include "discord_register.h"
+
+#define WIN32_LEAN_AND_MEAN
+#define NOMCX
+#define NOSERVICE
+#define NOIME
+#include
+#include
+#include
+
+/**
+ * Updated fixes for MinGW and WinXP
+ * This block is written the way it does not involve changing the rest of the code
+ * Checked to be compiling
+ * 1) strsafe.h belongs to Windows SDK and cannot be added to MinGW
+ * #include guarded, functions redirected to substitutes
+ * 2) RegSetKeyValueW and LSTATUS are not declared in
+ * The entire function is rewritten
+ */
+#ifdef __MINGW32__
+#include
+/// strsafe.h fixes
+static HRESULT StringCbPrintfW(LPWSTR pszDest, size_t cbDest, LPCWSTR pszFormat, ...)
+{
+ HRESULT ret;
+ va_list va;
+ va_start(va, pszFormat);
+ cbDest /= 2; // Size is divided by 2 to convert from bytes to wide characters - causes segfault
+ // othervise
+ ret = vsnwprintf(pszDest, cbDest, pszFormat, va);
+ pszDest[cbDest - 1] = 0; // Terminate the string in case a buffer overflow; -1 will be returned
+ va_end(va);
+ return ret;
+}
+#else
+#include
+#include
+#endif // __MINGW32__
+
+/// winreg.h fixes
+#ifndef LSTATUS
+#define LSTATUS LONG
+#endif
+#ifdef RegSetKeyValueW
+#undefine RegSetKeyValueW
+#endif
+#define RegSetKeyValueW regset
+static LSTATUS regset(HKEY hkey,
+ LPCWSTR subkey,
+ LPCWSTR name,
+ DWORD type,
+ const void* data,
+ DWORD len)
+{
+ HKEY htkey = hkey, hsubkey = nullptr;
+ LSTATUS ret;
+ if (subkey && subkey[0]) {
+ if ((ret = RegCreateKeyExW(hkey, subkey, 0, 0, 0, KEY_ALL_ACCESS, 0, &hsubkey, 0)) !=
+ ERROR_SUCCESS)
+ return ret;
+ htkey = hsubkey;
+ }
+ ret = RegSetValueExW(htkey, name, 0, type, (const BYTE*)data, len);
+ if (hsubkey && hsubkey != hkey)
+ RegCloseKey(hsubkey);
+ return ret;
+}
+
+static void Discord_RegisterW(const wchar_t* applicationId, const wchar_t* command)
+{
+ // https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
+ // we want to register games so we can run them as discord-://
+ // Update the HKEY_CURRENT_USER, because it doesn't seem to require special permissions.
+
+ wchar_t exeFilePath[MAX_PATH];
+ DWORD exeLen = GetModuleFileNameW(nullptr, exeFilePath, MAX_PATH);
+ wchar_t openCommand[1024];
+
+ if (command && command[0]) {
+ StringCbPrintfW(openCommand, sizeof(openCommand), L"%s", command);
+ }
+ else {
+ // StringCbCopyW(openCommand, sizeof(openCommand), exeFilePath);
+ StringCbPrintfW(openCommand, sizeof(openCommand), L"%s", exeFilePath);
+ }
+
+ wchar_t protocolName[64];
+ StringCbPrintfW(protocolName, sizeof(protocolName), L"discord-%s", applicationId);
+ wchar_t protocolDescription[128];
+ StringCbPrintfW(
+ protocolDescription, sizeof(protocolDescription), L"URL:Run game %s protocol", applicationId);
+ wchar_t urlProtocol = 0;
+
+ wchar_t keyName[256];
+ StringCbPrintfW(keyName, sizeof(keyName), L"Software\\Classes\\%s", protocolName);
+ HKEY key;
+ auto status =
+ RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, nullptr, 0, KEY_WRITE, nullptr, &key, nullptr);
+ if (status != ERROR_SUCCESS) {
+ fprintf(stderr, "Error creating key\n");
+ return;
+ }
+ DWORD len;
+ LSTATUS result;
+ len = (DWORD)lstrlenW(protocolDescription) + 1;
+ result =
+ RegSetKeyValueW(key, nullptr, nullptr, REG_SZ, protocolDescription, len * sizeof(wchar_t));
+ if (FAILED(result)) {
+ fprintf(stderr, "Error writing description\n");
+ }
+
+ len = (DWORD)lstrlenW(protocolDescription) + 1;
+ result = RegSetKeyValueW(key, nullptr, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t));
+ if (FAILED(result)) {
+ fprintf(stderr, "Error writing description\n");
+ }
+
+ result = RegSetKeyValueW(
+ key, L"DefaultIcon", nullptr, REG_SZ, exeFilePath, (exeLen + 1) * sizeof(wchar_t));
+ if (FAILED(result)) {
+ fprintf(stderr, "Error writing icon\n");
+ }
+
+ len = (DWORD)lstrlenW(openCommand) + 1;
+ result = RegSetKeyValueW(
+ key, L"shell\\open\\command", nullptr, REG_SZ, openCommand, len * sizeof(wchar_t));
+ if (FAILED(result)) {
+ fprintf(stderr, "Error writing command\n");
+ }
+ RegCloseKey(key);
+}
+
+extern "C" DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command)
+{
+ wchar_t appId[32];
+ MultiByteToWideChar(CP_UTF8, 0, applicationId, -1, appId, 32);
+
+ wchar_t openCommand[1024];
+ const wchar_t* wcommand = nullptr;
+ if (command && command[0]) {
+ const auto commandBufferLen = sizeof(openCommand) / sizeof(*openCommand);
+ MultiByteToWideChar(CP_UTF8, 0, command, -1, openCommand, commandBufferLen);
+ wcommand = openCommand;
+ }
+
+ Discord_RegisterW(appId, wcommand);
+}
+
+extern "C" DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId,
+ const char* steamId)
+{
+ wchar_t appId[32];
+ MultiByteToWideChar(CP_UTF8, 0, applicationId, -1, appId, 32);
+
+ wchar_t wSteamId[32];
+ MultiByteToWideChar(CP_UTF8, 0, steamId, -1, wSteamId, 32);
+
+ HKEY key;
+ auto status = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", 0, KEY_READ, &key);
+ if (status != ERROR_SUCCESS) {
+ fprintf(stderr, "Error opening Steam key\n");
+ return;
+ }
+
+ wchar_t steamPath[MAX_PATH];
+ DWORD pathBytes = sizeof(steamPath);
+ status = RegQueryValueExW(key, L"SteamExe", nullptr, nullptr, (BYTE*)steamPath, &pathBytes);
+ RegCloseKey(key);
+ if (status != ERROR_SUCCESS || pathBytes < 1) {
+ fprintf(stderr, "Error reading SteamExe key\n");
+ return;
+ }
+
+ DWORD pathChars = pathBytes / sizeof(wchar_t);
+ for (DWORD i = 0; i < pathChars; ++i) {
+ if (steamPath[i] == L'/') {
+ steamPath[i] = L'\\';
+ }
+ }
+
+ wchar_t command[1024];
+ StringCbPrintfW(command, sizeof(command), L"\"%s\" steam://rungameid/%s", steamPath, wSteamId);
+
+ Discord_RegisterW(appId, command);
+}
diff --git a/3rdparty/discord-rpc/src/discord_rpc.cpp b/3rdparty/discord-rpc/src/discord_rpc.cpp
new file mode 100644
index 0000000000..3eb2ec614f
--- /dev/null
+++ b/3rdparty/discord-rpc/src/discord_rpc.cpp
@@ -0,0 +1,511 @@
+#include "discord_rpc.h"
+
+#include "backoff.h"
+#include "discord_register.h"
+#include "msg_queue.h"
+#include "rpc_connection.h"
+#include "serialization.h"
+
+#include
+#include
+#include
+
+#ifndef DISCORD_DISABLE_IO_THREAD
+#include
+#include
+#endif
+
+constexpr size_t MaxMessageSize{16 * 1024};
+constexpr size_t MessageQueueSize{8};
+constexpr size_t JoinQueueSize{8};
+
+struct QueuedMessage {
+ size_t length;
+ char buffer[MaxMessageSize];
+
+ void Copy(const QueuedMessage& other)
+ {
+ length = other.length;
+ if (length) {
+ memcpy(buffer, other.buffer, length);
+ }
+ }
+};
+
+struct User {
+ // snowflake (64bit int), turned into a ascii decimal string, at most 20 chars +1 null
+ // terminator = 21
+ char userId[32];
+ // 32 unicode glyphs is max name size => 4 bytes per glyph in the worst case, +1 for null
+ // terminator = 129
+ char username[344];
+ // 4 decimal digits + 1 null terminator = 5
+ char discriminator[8];
+ // optional 'a_' + md5 hex digest (32 bytes) + null terminator = 35
+ char avatar[128];
+ // Rounded way up because I'm paranoid about games breaking from future changes in these sizes
+};
+
+static RpcConnection* Connection{nullptr};
+static DiscordEventHandlers QueuedHandlers{};
+static DiscordEventHandlers Handlers{};
+static std::atomic_bool WasJustConnected{false};
+static std::atomic_bool WasJustDisconnected{false};
+static std::atomic_bool GotErrorMessage{false};
+static std::atomic_bool WasJoinGame{false};
+static std::atomic_bool WasSpectateGame{false};
+static std::atomic_bool UpdatePresence{false};
+static char JoinGameSecret[256];
+static char SpectateGameSecret[256];
+static int LastErrorCode{0};
+static char LastErrorMessage[256];
+static int LastDisconnectErrorCode{0};
+static char LastDisconnectErrorMessage[256];
+static std::mutex PresenceMutex;
+static std::mutex HandlerMutex;
+static QueuedMessage QueuedPresence{};
+static MsgQueue SendQueue;
+static MsgQueue JoinAskQueue;
+static User connectedUser;
+
+// We want to auto connect, and retry on failure, but not as fast as possible. This does expoential
+// backoff from 0.5 seconds to 1 minute
+static Backoff ReconnectTimeMs(500, 60 * 1000);
+static auto NextConnect = std::chrono::system_clock::now();
+static int Pid{0};
+static int Nonce{1};
+
+#ifndef DISCORD_DISABLE_IO_THREAD
+static void Discord_UpdateConnection(void);
+class IoThreadHolder {
+private:
+ std::atomic_bool keepRunning{true};
+ std::mutex waitForIOMutex;
+ std::condition_variable waitForIOActivity;
+ std::thread ioThread;
+
+public:
+ void Start()
+ {
+ keepRunning.store(true);
+ ioThread = std::thread([&]() {
+ const std::chrono::duration maxWait{500LL};
+ Discord_UpdateConnection();
+ while (keepRunning.load()) {
+ std::unique_lock lock(waitForIOMutex);
+ waitForIOActivity.wait_for(lock, maxWait);
+ Discord_UpdateConnection();
+ }
+ });
+ }
+
+ void Notify() { waitForIOActivity.notify_all(); }
+
+ void Stop()
+ {
+ keepRunning.exchange(false);
+ Notify();
+ if (ioThread.joinable()) {
+ ioThread.join();
+ }
+ }
+
+ ~IoThreadHolder() { Stop(); }
+};
+#else
+class IoThreadHolder {
+public:
+ void Start() {}
+ void Stop() {}
+ void Notify() {}
+};
+#endif // DISCORD_DISABLE_IO_THREAD
+static IoThreadHolder* IoThread{nullptr};
+
+static void UpdateReconnectTime()
+{
+ NextConnect = std::chrono::system_clock::now() +
+ std::chrono::duration{ReconnectTimeMs.nextDelay()};
+}
+
+#ifdef DISCORD_DISABLE_IO_THREAD
+extern "C" DISCORD_EXPORT void Discord_UpdateConnection(void)
+#else
+static void Discord_UpdateConnection(void)
+#endif
+{
+ if (!Connection) {
+ return;
+ }
+
+ if (!Connection->IsOpen()) {
+ if (std::chrono::system_clock::now() >= NextConnect) {
+ UpdateReconnectTime();
+ Connection->Open();
+ }
+ }
+ else {
+ // reads
+
+ for (;;) {
+ JsonDocument message;
+
+ if (!Connection->Read(message)) {
+ break;
+ }
+
+ const char* evtName = GetStrMember(&message, "evt");
+ const char* nonce = GetStrMember(&message, "nonce");
+
+ if (nonce) {
+ // in responses only -- should use to match up response when needed.
+
+ if (evtName && strcmp(evtName, "ERROR") == 0) {
+ auto data = GetObjMember(&message, "data");
+ LastErrorCode = GetIntMember(data, "code");
+ StringCopy(LastErrorMessage, GetStrMember(data, "message", ""));
+ GotErrorMessage.store(true);
+ }
+ }
+ else {
+ // should have evt == name of event, optional data
+ if (evtName == nullptr) {
+ continue;
+ }
+
+ auto data = GetObjMember(&message, "data");
+
+ if (strcmp(evtName, "ACTIVITY_JOIN") == 0) {
+ auto secret = GetStrMember(data, "secret");
+ if (secret) {
+ StringCopy(JoinGameSecret, secret);
+ WasJoinGame.store(true);
+ }
+ }
+ else if (strcmp(evtName, "ACTIVITY_SPECTATE") == 0) {
+ auto secret = GetStrMember(data, "secret");
+ if (secret) {
+ StringCopy(SpectateGameSecret, secret);
+ WasSpectateGame.store(true);
+ }
+ }
+ else if (strcmp(evtName, "ACTIVITY_JOIN_REQUEST") == 0) {
+ auto user = GetObjMember(data, "user");
+ auto userId = GetStrMember(user, "id");
+ auto username = GetStrMember(user, "username");
+ auto avatar = GetStrMember(user, "avatar");
+ auto joinReq = JoinAskQueue.GetNextAddMessage();
+ if (userId && username && joinReq) {
+ StringCopy(joinReq->userId, userId);
+ StringCopy(joinReq->username, username);
+ auto discriminator = GetStrMember(user, "discriminator");
+ if (discriminator) {
+ StringCopy(joinReq->discriminator, discriminator);
+ }
+ if (avatar) {
+ StringCopy(joinReq->avatar, avatar);
+ }
+ else {
+ joinReq->avatar[0] = 0;
+ }
+ JoinAskQueue.CommitAdd();
+ }
+ }
+ }
+ }
+
+ // writes
+ if (UpdatePresence.exchange(false) && QueuedPresence.length) {
+ QueuedMessage local;
+ {
+ std::lock_guard guard(PresenceMutex);
+ local.Copy(QueuedPresence);
+ }
+ if (!Connection->Write(local.buffer, local.length)) {
+ // if we fail to send, requeue
+ std::lock_guard guard(PresenceMutex);
+ QueuedPresence.Copy(local);
+ UpdatePresence.exchange(true);
+ }
+ }
+
+ while (SendQueue.HavePendingSends()) {
+ auto qmessage = SendQueue.GetNextSendMessage();
+ Connection->Write(qmessage->buffer, qmessage->length);
+ SendQueue.CommitSend();
+ }
+ }
+}
+
+static void SignalIOActivity()
+{
+ if (IoThread != nullptr) {
+ IoThread->Notify();
+ }
+}
+
+static bool RegisterForEvent(const char* evtName)
+{
+ auto qmessage = SendQueue.GetNextAddMessage();
+ if (qmessage) {
+ qmessage->length =
+ JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
+ SendQueue.CommitAdd();
+ SignalIOActivity();
+ return true;
+ }
+ return false;
+}
+
+static bool DeregisterForEvent(const char* evtName)
+{
+ auto qmessage = SendQueue.GetNextAddMessage();
+ if (qmessage) {
+ qmessage->length =
+ JsonWriteUnsubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
+ SendQueue.CommitAdd();
+ SignalIOActivity();
+ return true;
+ }
+ return false;
+}
+
+extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
+ DiscordEventHandlers* handlers,
+ int autoRegister,
+ const char* optionalSteamId)
+{
+ IoThread = new (std::nothrow) IoThreadHolder();
+ if (IoThread == nullptr) {
+ return;
+ }
+
+ if (autoRegister) {
+ if (optionalSteamId && optionalSteamId[0]) {
+ Discord_RegisterSteamGame(applicationId, optionalSteamId);
+ }
+ else {
+ Discord_Register(applicationId, nullptr);
+ }
+ }
+
+ Pid = GetProcessId();
+
+ {
+ std::lock_guard guard(HandlerMutex);
+
+ if (handlers) {
+ QueuedHandlers = *handlers;
+ }
+ else {
+ QueuedHandlers = {};
+ }
+
+ Handlers = {};
+ }
+
+ if (Connection) {
+ return;
+ }
+
+ Connection = RpcConnection::Create(applicationId);
+ Connection->onConnect = [](JsonDocument& readyMessage) {
+ Discord_UpdateHandlers(&QueuedHandlers);
+ if (QueuedPresence.length > 0) {
+ UpdatePresence.exchange(true);
+ SignalIOActivity();
+ }
+ auto data = GetObjMember(&readyMessage, "data");
+ auto user = GetObjMember(data, "user");
+ auto userId = GetStrMember(user, "id");
+ auto username = GetStrMember(user, "username");
+ auto avatar = GetStrMember(user, "avatar");
+ if (userId && username) {
+ StringCopy(connectedUser.userId, userId);
+ StringCopy(connectedUser.username, username);
+ auto discriminator = GetStrMember(user, "discriminator");
+ if (discriminator) {
+ StringCopy(connectedUser.discriminator, discriminator);
+ }
+ if (avatar) {
+ StringCopy(connectedUser.avatar, avatar);
+ }
+ else {
+ connectedUser.avatar[0] = 0;
+ }
+ }
+ WasJustConnected.exchange(true);
+ ReconnectTimeMs.reset();
+ };
+ Connection->onDisconnect = [](int err, const char* message) {
+ LastDisconnectErrorCode = err;
+ StringCopy(LastDisconnectErrorMessage, message);
+ WasJustDisconnected.exchange(true);
+ UpdateReconnectTime();
+ };
+
+ IoThread->Start();
+}
+
+extern "C" DISCORD_EXPORT void Discord_Shutdown(void)
+{
+ if (!Connection) {
+ return;
+ }
+ Connection->onConnect = nullptr;
+ Connection->onDisconnect = nullptr;
+ Handlers = {};
+ if (IoThread != nullptr) {
+ IoThread->Stop();
+ delete IoThread;
+ IoThread = nullptr;
+ }
+
+ // HACK: We need to send the updated (cleared) presence, but we're shutting down.
+ // Force an update, wait 100ms for it to clear (hopefully this will be long enough), and get any responses.
+ Discord_UpdateConnection();
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ Discord_UpdateConnection();
+
+ QueuedPresence.length = 0;
+ UpdatePresence.exchange(false);
+
+ RpcConnection::Destroy(Connection);
+}
+
+extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence)
+{
+ {
+ std::lock_guard guard(PresenceMutex);
+ QueuedPresence.length = JsonWriteRichPresenceObj(
+ QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
+ UpdatePresence.exchange(true);
+ }
+ SignalIOActivity();
+}
+
+extern "C" DISCORD_EXPORT void Discord_ClearPresence(void)
+{
+ Discord_UpdatePresence(nullptr);
+}
+
+extern "C" DISCORD_EXPORT void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int reply)
+{
+ // if we are not connected, let's not batch up stale messages for later
+ if (!Connection || !Connection->IsOpen()) {
+ return;
+ }
+ auto qmessage = SendQueue.GetNextAddMessage();
+ if (qmessage) {
+ qmessage->length =
+ JsonWriteJoinReply(qmessage->buffer, sizeof(qmessage->buffer), userId, reply, Nonce++);
+ SendQueue.CommitAdd();
+ SignalIOActivity();
+ }
+}
+
+extern "C" DISCORD_EXPORT void Discord_RunCallbacks(void)
+{
+ // Note on some weirdness: internally we might connect, get other signals, disconnect any number
+ // of times inbetween calls here. Externally, we want the sequence to seem sane, so any other
+ // signals are book-ended by calls to ready and disconnect.
+
+ if (!Connection) {
+ return;
+ }
+
+ bool wasDisconnected = WasJustDisconnected.exchange(false);
+ bool isConnected = Connection->IsOpen();
+
+ if (isConnected) {
+ // if we are connected, disconnect cb first
+ std::lock_guard guard(HandlerMutex);
+ if (wasDisconnected && Handlers.disconnected) {
+ Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage);
+ }
+ }
+
+ if (WasJustConnected.exchange(false)) {
+ std::lock_guard guard(HandlerMutex);
+ if (Handlers.ready) {
+ DiscordUser du{connectedUser.userId,
+ connectedUser.username,
+ connectedUser.discriminator,
+ connectedUser.avatar};
+ Handlers.ready(&du);
+ }
+ }
+
+ if (GotErrorMessage.exchange(false)) {
+ std::lock_guard guard(HandlerMutex);
+ if (Handlers.errored) {
+ Handlers.errored(LastErrorCode, LastErrorMessage);
+ }
+ }
+
+ if (WasJoinGame.exchange(false)) {
+ std::lock_guard guard(HandlerMutex);
+ if (Handlers.joinGame) {
+ Handlers.joinGame(JoinGameSecret);
+ }
+ }
+
+ if (WasSpectateGame.exchange(false)) {
+ std::lock_guard guard(HandlerMutex);
+ if (Handlers.spectateGame) {
+ Handlers.spectateGame(SpectateGameSecret);
+ }
+ }
+
+ // Right now this batches up any requests and sends them all in a burst; I could imagine a world
+ // where the implementer would rather sequentially accept/reject each one before the next invite
+ // is sent. I left it this way because I could also imagine wanting to process these all and
+ // maybe show them in one common dialog and/or start fetching the avatars in parallel, and if
+ // not it should be trivial for the implementer to make a queue themselves.
+ while (JoinAskQueue.HavePendingSends()) {
+ auto req = JoinAskQueue.GetNextSendMessage();
+ {
+ std::lock_guard guard(HandlerMutex);
+ if (Handlers.joinRequest) {
+ DiscordUser du{req->userId, req->username, req->discriminator, req->avatar};
+ Handlers.joinRequest(&du);
+ }
+ }
+ JoinAskQueue.CommitSend();
+ }
+
+ if (!isConnected) {
+ // if we are not connected, disconnect message last
+ std::lock_guard guard(HandlerMutex);
+ if (wasDisconnected && Handlers.disconnected) {
+ Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage);
+ }
+ }
+}
+
+extern "C" DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* newHandlers)
+{
+ if (newHandlers) {
+#define HANDLE_EVENT_REGISTRATION(handler_name, event) \
+ if (!Handlers.handler_name && newHandlers->handler_name) { \
+ RegisterForEvent(event); \
+ } \
+ else if (Handlers.handler_name && !newHandlers->handler_name) { \
+ DeregisterForEvent(event); \
+ }
+
+ std::lock_guard guard(HandlerMutex);
+ HANDLE_EVENT_REGISTRATION(joinGame, "ACTIVITY_JOIN")
+ HANDLE_EVENT_REGISTRATION(spectateGame, "ACTIVITY_SPECTATE")
+ HANDLE_EVENT_REGISTRATION(joinRequest, "ACTIVITY_JOIN_REQUEST")
+
+#undef HANDLE_EVENT_REGISTRATION
+
+ Handlers = *newHandlers;
+ }
+ else {
+ std::lock_guard guard(HandlerMutex);
+ Handlers = {};
+ }
+ return;
+}
diff --git a/3rdparty/discord-rpc/src/dllmain.cpp b/3rdparty/discord-rpc/src/dllmain.cpp
new file mode 100644
index 0000000000..fbfc2950d7
--- /dev/null
+++ b/3rdparty/discord-rpc/src/dllmain.cpp
@@ -0,0 +1,8 @@
+#include
+
+// outsmart GCC's missing-declarations warning
+BOOL WINAPI DllMain(HMODULE, DWORD, LPVOID);
+BOOL WINAPI DllMain(HMODULE, DWORD, LPVOID)
+{
+ return TRUE;
+}
diff --git a/3rdparty/discord-rpc/src/msg_queue.h b/3rdparty/discord-rpc/src/msg_queue.h
new file mode 100644
index 0000000000..77f380e705
--- /dev/null
+++ b/3rdparty/discord-rpc/src/msg_queue.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include
+
+// A simple queue. No locks, but only works with a single thread as producer and a single thread as
+// a consumer. Mutex up as needed.
+
+template
+class MsgQueue {
+ ElementType queue_[QueueSize];
+ std::atomic_uint nextAdd_{0};
+ std::atomic_uint nextSend_{0};
+ std::atomic_uint pendingSends_{0};
+
+public:
+ MsgQueue() {}
+
+ ElementType* GetNextAddMessage()
+ {
+ // if we are falling behind, bail
+ if (pendingSends_.load() >= QueueSize) {
+ return nullptr;
+ }
+ auto index = (nextAdd_++) % QueueSize;
+ return &queue_[index];
+ }
+ void CommitAdd() { ++pendingSends_; }
+
+ bool HavePendingSends() const { return pendingSends_.load() != 0; }
+ ElementType* GetNextSendMessage()
+ {
+ auto index = (nextSend_++) % QueueSize;
+ return &queue_[index];
+ }
+ void CommitSend() { --pendingSends_; }
+};
diff --git a/3rdparty/discord-rpc/src/rpc_connection.cpp b/3rdparty/discord-rpc/src/rpc_connection.cpp
new file mode 100644
index 0000000000..0933162169
--- /dev/null
+++ b/3rdparty/discord-rpc/src/rpc_connection.cpp
@@ -0,0 +1,137 @@
+#include "rpc_connection.h"
+#include "serialization.h"
+
+#include
+
+static const int RpcVersion = 1;
+static RpcConnection Instance;
+
+/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
+{
+ Instance.connection = BaseConnection::Create();
+ StringCopy(Instance.appId, applicationId);
+ return &Instance;
+}
+
+/*static*/ void RpcConnection::Destroy(RpcConnection*& c)
+{
+ c->Close();
+ BaseConnection::Destroy(c->connection);
+ c = nullptr;
+}
+
+void RpcConnection::Open()
+{
+ if (state == State::Connected) {
+ return;
+ }
+
+ if (state == State::Disconnected && !connection->Open()) {
+ return;
+ }
+
+ if (state == State::SentHandshake) {
+ JsonDocument message;
+ if (Read(message)) {
+ auto cmd = GetStrMember(&message, "cmd");
+ auto evt = GetStrMember(&message, "evt");
+ if (cmd && evt && !strcmp(cmd, "DISPATCH") && !strcmp(evt, "READY")) {
+ state = State::Connected;
+ if (onConnect) {
+ onConnect(message);
+ }
+ }
+ }
+ }
+ else {
+ sendFrame.opcode = Opcode::Handshake;
+ sendFrame.length = (uint32_t)JsonWriteHandshakeObj(
+ sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
+
+ if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
+ state = State::SentHandshake;
+ }
+ else {
+ Close();
+ }
+ }
+}
+
+void RpcConnection::Close()
+{
+ if (onDisconnect && (state == State::Connected || state == State::SentHandshake)) {
+ onDisconnect(lastErrorCode, lastErrorMessage);
+ }
+ connection->Close();
+ state = State::Disconnected;
+}
+
+bool RpcConnection::Write(const void* data, size_t length)
+{
+ sendFrame.opcode = Opcode::Frame;
+ memcpy(sendFrame.message, data, length);
+ sendFrame.length = (uint32_t)length;
+ if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) {
+ Close();
+ return false;
+ }
+ return true;
+}
+
+bool RpcConnection::Read(JsonDocument& message)
+{
+ if (state != State::Connected && state != State::SentHandshake) {
+ return false;
+ }
+ MessageFrame readFrame;
+ for (;;) {
+ bool didRead = connection->Read(&readFrame, sizeof(MessageFrameHeader));
+ if (!didRead) {
+ if (!connection->isOpen) {
+ lastErrorCode = (int)ErrorCode::PipeClosed;
+ StringCopy(lastErrorMessage, "Pipe closed");
+ Close();
+ }
+ return false;
+ }
+
+ if (readFrame.length > 0) {
+ didRead = connection->Read(readFrame.message, readFrame.length);
+ if (!didRead) {
+ lastErrorCode = (int)ErrorCode::ReadCorrupt;
+ StringCopy(lastErrorMessage, "Partial data in frame");
+ Close();
+ return false;
+ }
+ readFrame.message[readFrame.length] = 0;
+ }
+
+ switch (readFrame.opcode) {
+ case Opcode::Close: {
+ message.ParseInsitu(readFrame.message);
+ lastErrorCode = GetIntMember(&message, "code");
+ StringCopy(lastErrorMessage, GetStrMember(&message, "message", ""));
+ Close();
+ return false;
+ }
+ case Opcode::Frame:
+ message.ParseInsitu(readFrame.message);
+ return true;
+ case Opcode::Ping:
+ readFrame.opcode = Opcode::Pong;
+ if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) {
+ Close();
+ }
+ break;
+ case Opcode::Pong:
+ break;
+ case Opcode::Handshake:
+ default:
+ // something bad happened
+ lastErrorCode = (int)ErrorCode::ReadCorrupt;
+ StringCopy(lastErrorMessage, "Bad ipc frame");
+ Close();
+ return false;
+ }
+ }
+}
diff --git a/3rdparty/discord-rpc/src/rpc_connection.h b/3rdparty/discord-rpc/src/rpc_connection.h
new file mode 100644
index 0000000000..bbdd05c792
--- /dev/null
+++ b/3rdparty/discord-rpc/src/rpc_connection.h
@@ -0,0 +1,59 @@
+#pragma once
+
+#include "connection.h"
+#include "serialization.h"
+
+// I took this from the buffer size libuv uses for named pipes; I suspect ours would usually be much
+// smaller.
+constexpr size_t MaxRpcFrameSize = 64 * 1024;
+
+struct RpcConnection {
+ enum class ErrorCode : int {
+ Success = 0,
+ PipeClosed = 1,
+ ReadCorrupt = 2,
+ };
+
+ enum class Opcode : uint32_t {
+ Handshake = 0,
+ Frame = 1,
+ Close = 2,
+ Ping = 3,
+ Pong = 4,
+ };
+
+ struct MessageFrameHeader {
+ Opcode opcode;
+ uint32_t length;
+ };
+
+ struct MessageFrame : public MessageFrameHeader {
+ char message[MaxRpcFrameSize - sizeof(MessageFrameHeader)];
+ };
+
+ enum class State : uint32_t {
+ Disconnected,
+ SentHandshake,
+ AwaitingResponse,
+ Connected,
+ };
+
+ BaseConnection* connection{nullptr};
+ State state{State::Disconnected};
+ void (*onConnect)(JsonDocument& message){nullptr};
+ void (*onDisconnect)(int errorCode, const char* message){nullptr};
+ char appId[64]{};
+ int lastErrorCode{0};
+ char lastErrorMessage[256]{};
+ RpcConnection::MessageFrame sendFrame;
+
+ static RpcConnection* Create(const char* applicationId);
+ static void Destroy(RpcConnection*&);
+
+ inline bool IsOpen() const { return state == State::Connected; }
+
+ void Open();
+ void Close();
+ bool Write(const void* data, size_t length);
+ bool Read(JsonDocument& message);
+};
diff --git a/3rdparty/discord-rpc/src/serialization.cpp b/3rdparty/discord-rpc/src/serialization.cpp
new file mode 100644
index 0000000000..6cc1e9013d
--- /dev/null
+++ b/3rdparty/discord-rpc/src/serialization.cpp
@@ -0,0 +1,245 @@
+#include "serialization.h"
+#include "connection.h"
+#include "discord_rpc.h"
+
+template
+void NumberToString(char* dest, T number)
+{
+ if (!number) {
+ *dest++ = '0';
+ *dest++ = 0;
+ return;
+ }
+ if (number < 0) {
+ *dest++ = '-';
+ number = -number;
+ }
+ char temp[32];
+ int place = 0;
+ while (number) {
+ auto digit = number % 10;
+ number = number / 10;
+ temp[place++] = '0' + (char)digit;
+ }
+ for (--place; place >= 0; --place) {
+ *dest++ = temp[place];
+ }
+ *dest = 0;
+}
+
+// it's ever so slightly faster to not have to strlen the key
+template
+void WriteKey(JsonWriter& w, T& k)
+{
+ w.Key(k, sizeof(T) - 1);
+}
+
+struct WriteObject {
+ JsonWriter& writer;
+ WriteObject(JsonWriter& w)
+ : writer(w)
+ {
+ writer.StartObject();
+ }
+ template
+ WriteObject(JsonWriter& w, T& name)
+ : writer(w)
+ {
+ WriteKey(writer, name);
+ writer.StartObject();
+ }
+ ~WriteObject() { writer.EndObject(); }
+};
+
+struct WriteArray {
+ JsonWriter& writer;
+ template
+ WriteArray(JsonWriter& w, T& name)
+ : writer(w)
+ {
+ WriteKey(writer, name);
+ writer.StartArray();
+ }
+ ~WriteArray() { writer.EndArray(); }
+};
+
+template
+void WriteOptionalString(JsonWriter& w, T& k, const char* value)
+{
+ if (value && value[0]) {
+ w.Key(k, sizeof(T) - 1);
+ w.String(value);
+ }
+}
+
+static void JsonWriteNonce(JsonWriter& writer, int nonce)
+{
+ WriteKey(writer, "nonce");
+ char nonceBuffer[32];
+ NumberToString(nonceBuffer, nonce);
+ writer.String(nonceBuffer);
+}
+
+size_t JsonWriteRichPresenceObj(char* dest,
+ size_t maxLen,
+ int nonce,
+ int pid,
+ const DiscordRichPresence* presence)
+{
+ JsonWriter writer(dest, maxLen);
+
+ {
+ WriteObject top(writer);
+
+ JsonWriteNonce(writer, nonce);
+
+ WriteKey(writer, "cmd");
+ writer.String("SET_ACTIVITY");
+
+ {
+ WriteObject args(writer, "args");
+
+ WriteKey(writer, "pid");
+ writer.Int(pid);
+
+ if (presence != nullptr) {
+ WriteObject activity(writer, "activity");
+
+ WriteOptionalString(writer, "state", presence->state);
+ WriteOptionalString(writer, "details", presence->details);
+
+ if (presence->startTimestamp || presence->endTimestamp) {
+ WriteObject timestamps(writer, "timestamps");
+
+ if (presence->startTimestamp) {
+ WriteKey(writer, "start");
+ writer.Int64(presence->startTimestamp);
+ }
+
+ if (presence->endTimestamp) {
+ WriteKey(writer, "end");
+ writer.Int64(presence->endTimestamp);
+ }
+ }
+
+ if ((presence->largeImageKey && presence->largeImageKey[0]) ||
+ (presence->largeImageText && presence->largeImageText[0]) ||
+ (presence->smallImageKey && presence->smallImageKey[0]) ||
+ (presence->smallImageText && presence->smallImageText[0])) {
+ WriteObject assets(writer, "assets");
+ WriteOptionalString(writer, "large_image", presence->largeImageKey);
+ WriteOptionalString(writer, "large_text", presence->largeImageText);
+ WriteOptionalString(writer, "small_image", presence->smallImageKey);
+ WriteOptionalString(writer, "small_text", presence->smallImageText);
+ }
+
+ if ((presence->partyId && presence->partyId[0]) || presence->partySize ||
+ presence->partyMax) {
+ WriteObject party(writer, "party");
+ WriteOptionalString(writer, "id", presence->partyId);
+ if (presence->partySize && presence->partyMax) {
+ WriteArray size(writer, "size");
+ writer.Int(presence->partySize);
+ writer.Int(presence->partyMax);
+ }
+ }
+
+ if ((presence->matchSecret && presence->matchSecret[0]) ||
+ (presence->joinSecret && presence->joinSecret[0]) ||
+ (presence->spectateSecret && presence->spectateSecret[0])) {
+ WriteObject secrets(writer, "secrets");
+ WriteOptionalString(writer, "match", presence->matchSecret);
+ WriteOptionalString(writer, "join", presence->joinSecret);
+ WriteOptionalString(writer, "spectate", presence->spectateSecret);
+ }
+
+ writer.Key("instance");
+ writer.Bool(presence->instance != 0);
+ }
+ }
+ }
+
+ return writer.Size();
+}
+
+size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId)
+{
+ JsonWriter writer(dest, maxLen);
+
+ {
+ WriteObject obj(writer);
+ WriteKey(writer, "v");
+ writer.Int(version);
+ WriteKey(writer, "client_id");
+ writer.String(applicationId);
+ }
+
+ return writer.Size();
+}
+
+size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName)
+{
+ JsonWriter writer(dest, maxLen);
+
+ {
+ WriteObject obj(writer);
+
+ JsonWriteNonce(writer, nonce);
+
+ WriteKey(writer, "cmd");
+ writer.String("SUBSCRIBE");
+
+ WriteKey(writer, "evt");
+ writer.String(evtName);
+ }
+
+ return writer.Size();
+}
+
+size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName)
+{
+ JsonWriter writer(dest, maxLen);
+
+ {
+ WriteObject obj(writer);
+
+ JsonWriteNonce(writer, nonce);
+
+ WriteKey(writer, "cmd");
+ writer.String("UNSUBSCRIBE");
+
+ WriteKey(writer, "evt");
+ writer.String(evtName);
+ }
+
+ return writer.Size();
+}
+
+size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce)
+{
+ JsonWriter writer(dest, maxLen);
+
+ {
+ WriteObject obj(writer);
+
+ WriteKey(writer, "cmd");
+ if (reply == DISCORD_REPLY_YES) {
+ writer.String("SEND_ACTIVITY_JOIN_INVITE");
+ }
+ else {
+ writer.String("CLOSE_ACTIVITY_JOIN_REQUEST");
+ }
+
+ WriteKey(writer, "args");
+ {
+ WriteObject args(writer);
+
+ WriteKey(writer, "user_id");
+ writer.String(userId);
+ }
+
+ JsonWriteNonce(writer, nonce);
+ }
+
+ return writer.Size();
+}
diff --git a/3rdparty/discord-rpc/src/serialization.h b/3rdparty/discord-rpc/src/serialization.h
new file mode 100644
index 0000000000..9c462dc283
--- /dev/null
+++ b/3rdparty/discord-rpc/src/serialization.h
@@ -0,0 +1,215 @@
+#pragma once
+
+#include
+
+#ifndef __MINGW32__
+#pragma warning(push)
+
+#pragma warning(disable : 4061) // enum is not explicitly handled by a case label
+#pragma warning(disable : 4365) // signed/unsigned mismatch
+#pragma warning(disable : 4464) // relative include path contains
+#pragma warning(disable : 4668) // is not defined as a preprocessor macro
+#pragma warning(disable : 6313) // Incorrect operator
+#endif // __MINGW32__
+
+#include "rapidjson/document.h"
+#include "rapidjson/stringbuffer.h"
+#include "rapidjson/writer.h"
+
+#ifndef __MINGW32__
+#pragma warning(pop)
+#endif // __MINGW32__
+
+// if only there was a standard library function for this
+template
+inline size_t StringCopy(char (&dest)[Len], const char* src)
+{
+ if (!src || !Len) {
+ return 0;
+ }
+ size_t copied;
+ char* out = dest;
+ for (copied = 1; *src && copied < Len; ++copied) {
+ *out++ = *src++;
+ }
+ *out = 0;
+ return copied - 1;
+}
+
+size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId);
+
+// Commands
+struct DiscordRichPresence;
+size_t JsonWriteRichPresenceObj(char* dest,
+ size_t maxLen,
+ int nonce,
+ int pid,
+ const DiscordRichPresence* presence);
+size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName);
+
+size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName);
+
+size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce);
+
+// I want to use as few allocations as I can get away with, and to do that with RapidJson, you need
+// to supply some of your own allocators for stuff rather than use the defaults
+
+class LinearAllocator {
+public:
+ char* buffer_;
+ char* end_;
+ LinearAllocator()
+ {
+ assert(0); // needed for some default case in rapidjson, should not use
+ }
+ LinearAllocator(char* buffer, size_t size)
+ : buffer_(buffer)
+ , end_(buffer + size)
+ {
+ }
+ static const bool kNeedFree = false;
+ void* Malloc(size_t size)
+ {
+ char* res = buffer_;
+ buffer_ += size;
+ if (buffer_ > end_) {
+ buffer_ = res;
+ return nullptr;
+ }
+ return res;
+ }
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize)
+ {
+ if (newSize == 0) {
+ return nullptr;
+ }
+ // allocate how much you need in the first place
+ assert(!originalPtr && !originalSize);
+ // unused parameter warning
+ (void)(originalPtr);
+ (void)(originalSize);
+ return Malloc(newSize);
+ }
+ static void Free(void* ptr)
+ {
+ /* shrug */
+ (void)ptr;
+ }
+};
+
+template
+class FixedLinearAllocator : public LinearAllocator {
+public:
+ char fixedBuffer_[Size];
+ FixedLinearAllocator()
+ : LinearAllocator(fixedBuffer_, Size)
+ {
+ }
+ static const bool kNeedFree = false;
+};
+
+// wonder why this isn't a thing already, maybe I missed it
+class DirectStringBuffer {
+public:
+ using Ch = char;
+ char* buffer_;
+ char* end_;
+ char* current_;
+
+ DirectStringBuffer(char* buffer, size_t maxLen)
+ : buffer_(buffer)
+ , end_(buffer + maxLen)
+ , current_(buffer)
+ {
+ }
+
+ void Put(char c)
+ {
+ if (current_ < end_) {
+ *current_++ = c;
+ }
+ }
+ void Flush() {}
+ size_t GetSize() const { return (size_t)(current_ - buffer_); }
+};
+
+using MallocAllocator = rapidjson::CrtAllocator;
+using PoolAllocator = rapidjson::MemoryPoolAllocator;
+using UTF8 = rapidjson::UTF8;
+// Writer appears to need about 16 bytes per nested object level (with 64bit size_t)
+using StackAllocator = FixedLinearAllocator<2048>;
+constexpr size_t WriterNestingLevels = 2048 / (2 * sizeof(size_t));
+using JsonWriterBase =
+ rapidjson::Writer;
+class JsonWriter : public JsonWriterBase {
+public:
+ DirectStringBuffer stringBuffer_;
+ StackAllocator stackAlloc_;
+
+ JsonWriter(char* dest, size_t maxLen)
+ : JsonWriterBase(stringBuffer_, &stackAlloc_, WriterNestingLevels)
+ , stringBuffer_(dest, maxLen)
+ , stackAlloc_()
+ {
+ }
+
+ size_t Size() const { return stringBuffer_.GetSize(); }
+};
+
+using JsonDocumentBase = rapidjson::GenericDocument;
+class JsonDocument : public JsonDocumentBase {
+public:
+ static const int kDefaultChunkCapacity = 32 * 1024;
+ // json parser will use this buffer first, then allocate more if needed; I seriously doubt we
+ // send any messages that would use all of this, though.
+ char parseBuffer_[32 * 1024];
+ MallocAllocator mallocAllocator_;
+ PoolAllocator poolAllocator_;
+ StackAllocator stackAllocator_;
+ JsonDocument()
+ : JsonDocumentBase(rapidjson::kObjectType,
+ &poolAllocator_,
+ sizeof(stackAllocator_.fixedBuffer_),
+ &stackAllocator_)
+ , poolAllocator_(parseBuffer_, sizeof(parseBuffer_), kDefaultChunkCapacity, &mallocAllocator_)
+ , stackAllocator_()
+ {
+ }
+};
+
+using JsonValue = rapidjson::GenericValue;
+
+inline JsonValue* GetObjMember(JsonValue* obj, const char* name)
+{
+ if (obj) {
+ auto member = obj->FindMember(name);
+ if (member != obj->MemberEnd() && member->value.IsObject()) {
+ return &member->value;
+ }
+ }
+ return nullptr;
+}
+
+inline int GetIntMember(JsonValue* obj, const char* name, int notFoundDefault = 0)
+{
+ if (obj) {
+ auto member = obj->FindMember(name);
+ if (member != obj->MemberEnd() && member->value.IsInt()) {
+ return member->value.GetInt();
+ }
+ }
+ return notFoundDefault;
+}
+
+inline const char* GetStrMember(JsonValue* obj,
+ const char* name,
+ const char* notFoundDefault = nullptr)
+{
+ if (obj) {
+ auto member = obj->FindMember(name);
+ if (member != obj->MemberEnd() && member->value.IsString()) {
+ return member->value.GetString();
+ }
+ }
+ return notFoundDefault;
+}
diff --git a/3rdparty/rapidjson/CMakeLists.txt b/3rdparty/rapidjson/CMakeLists.txt
new file mode 100644
index 0000000000..d85323b4c9
--- /dev/null
+++ b/3rdparty/rapidjson/CMakeLists.txt
@@ -0,0 +1,2 @@
+add_library(rapidjson INTERFACE)
+target_include_directories(rapidjson INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include/")
diff --git a/3rdparty/rapidjson/include/rapidjson/allocators.h b/3rdparty/rapidjson/include/rapidjson/allocators.h
new file mode 100644
index 0000000000..cc67c89713
--- /dev/null
+++ b/3rdparty/rapidjson/include/rapidjson/allocators.h
@@ -0,0 +1,284 @@
+// Tencent is pleased to support the open source community by making RapidJSON available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file except
+// in compliance with the License. You may obtain a copy of the License at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+#ifndef RAPIDJSON_ALLOCATORS_H_
+#define RAPIDJSON_ALLOCATORS_H_
+
+#include "rapidjson.h"
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+///////////////////////////////////////////////////////////////////////////////
+// Allocator
+
+/*! \class rapidjson::Allocator
+ \brief Concept for allocating, resizing and freeing memory block.
+
+ Note that Malloc() and Realloc() are non-static but Free() is static.
+
+ So if an allocator need to support Free(), it needs to put its pointer in
+ the header of memory block.
+
+\code
+concept Allocator {
+ static const bool kNeedFree; //!< Whether this allocator needs to call Free().
+
+ // Allocate a memory block.
+ // \param size of the memory block in bytes.
+ // \returns pointer to the memory block.
+ void* Malloc(size_t size);
+
+ // Resize a memory block.
+ // \param originalPtr The pointer to current memory block. Null pointer is permitted.
+ // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
+ // \param newSize the new size in bytes.
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
+
+ // Free a memory block.
+ // \param pointer to the memory block. Null pointer is permitted.
+ static void Free(void *ptr);
+};
+\endcode
+*/
+
+
+/*! \def RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
+ \ingroup RAPIDJSON_CONFIG
+ \brief User-defined kDefaultChunkCapacity definition.
+
+ User can define this as any \c size that is a power of 2.
+*/
+
+#ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
+#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
+#endif
+
+
+///////////////////////////////////////////////////////////////////////////////
+// CrtAllocator
+
+//! C-runtime library allocator.
+/*! This class is just wrapper for standard C library memory routines.
+ \note implements Allocator concept
+*/
+class CrtAllocator {
+public:
+ static const bool kNeedFree = true;
+ void* Malloc(size_t size) {
+ if (size) // behavior of malloc(0) is implementation defined.
+ return std::malloc(size);
+ else
+ return NULL; // standardize to returning NULL.
+ }
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
+ (void)originalSize;
+ if (newSize == 0) {
+ std::free(originalPtr);
+ return NULL;
+ }
+ return std::realloc(originalPtr, newSize);
+ }
+ static void Free(void *ptr) { std::free(ptr); }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// MemoryPoolAllocator
+
+//! Default memory allocator used by the parser and DOM.
+/*! This allocator allocate memory blocks from pre-allocated memory chunks.
+
+ It does not free memory blocks. And Realloc() only allocate new memory.
+
+ The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.
+
+ User may also supply a buffer as the first chunk.
+
+ If the user-buffer is full then additional chunks are allocated by BaseAllocator.
+
+ The user-buffer is not deallocated by this allocator.
+
+ \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator.
+ \note implements Allocator concept
+*/
+template
+class MemoryPoolAllocator {
+public:
+ static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
+
+ //! Constructor with chunkSize.
+ /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
+ \param baseAllocator The allocator for allocating memory chunks.
+ */
+ MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
+ chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
+ {
+ }
+
+ //! Constructor with user-supplied buffer.
+ /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.
+
+ The user buffer will not be deallocated when this allocator is destructed.
+
+ \param buffer User supplied buffer.
+ \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
+ \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
+ \param baseAllocator The allocator for allocating memory chunks.
+ */
+ MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
+ chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
+ {
+ RAPIDJSON_ASSERT(buffer != 0);
+ RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
+ chunkHead_ = reinterpret_cast(buffer);
+ chunkHead_->capacity = size - sizeof(ChunkHeader);
+ chunkHead_->size = 0;
+ chunkHead_->next = 0;
+ }
+
+ //! Destructor.
+ /*! This deallocates all memory chunks, excluding the user-supplied buffer.
+ */
+ ~MemoryPoolAllocator() {
+ Clear();
+ RAPIDJSON_DELETE(ownBaseAllocator_);
+ }
+
+ //! Deallocates all memory chunks, excluding the user-supplied buffer.
+ void Clear() {
+ while (chunkHead_ && chunkHead_ != userBuffer_) {
+ ChunkHeader* next = chunkHead_->next;
+ baseAllocator_->Free(chunkHead_);
+ chunkHead_ = next;
+ }
+ if (chunkHead_ && chunkHead_ == userBuffer_)
+ chunkHead_->size = 0; // Clear user buffer
+ }
+
+ //! Computes the total capacity of allocated memory chunks.
+ /*! \return total capacity in bytes.
+ */
+ size_t Capacity() const {
+ size_t capacity = 0;
+ for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
+ capacity += c->capacity;
+ return capacity;
+ }
+
+ //! Computes the memory blocks allocated.
+ /*! \return total used bytes.
+ */
+ size_t Size() const {
+ size_t size = 0;
+ for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
+ size += c->size;
+ return size;
+ }
+
+ //! Allocates a memory block. (concept Allocator)
+ void* Malloc(size_t size) {
+ if (!size)
+ return NULL;
+
+ size = RAPIDJSON_ALIGN(size);
+ if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
+ if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
+ return NULL;
+
+ void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
+ chunkHead_->size += size;
+ return buffer;
+ }
+
+ //! Resizes a memory block (concept Allocator)
+ void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
+ if (originalPtr == 0)
+ return Malloc(newSize);
+
+ if (newSize == 0)
+ return NULL;
+
+ originalSize = RAPIDJSON_ALIGN(originalSize);
+ newSize = RAPIDJSON_ALIGN(newSize);
+
+ // Do not shrink if new size is smaller than original
+ if (originalSize >= newSize)
+ return originalPtr;
+
+ // Simply expand it if it is the last allocation and there is sufficient space
+ if (originalPtr == reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
+ size_t increment = static_cast(newSize - originalSize);
+ if (chunkHead_->size + increment <= chunkHead_->capacity) {
+ chunkHead_->size += increment;
+ return originalPtr;
+ }
+ }
+
+ // Realloc process: allocate and copy memory, do not free original buffer.
+ if (void* newBuffer = Malloc(newSize)) {
+ if (originalSize)
+ std::memcpy(newBuffer, originalPtr, originalSize);
+ return newBuffer;
+ }
+ else
+ return NULL;
+ }
+
+ //! Frees a memory block (concept Allocator)
+ static void Free(void *ptr) { (void)ptr; } // Do nothing
+
+private:
+ //! Copy constructor is not permitted.
+ MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
+ //! Copy assignment operator is not permitted.
+ MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
+
+ //! Creates a new chunk.
+ /*! \param capacity Capacity of the chunk in bytes.
+ \return true if success.
+ */
+ bool AddChunk(size_t capacity) {
+ if (!baseAllocator_)
+ ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
+ if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
+ chunk->capacity = capacity;
+ chunk->size = 0;
+ chunk->next = chunkHead_;
+ chunkHead_ = chunk;
+ return true;
+ }
+ else
+ return false;
+ }
+
+ static const int kDefaultChunkCapacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity.
+
+ //! Chunk header for perpending to each chunk.
+ /*! Chunks are stored as a singly linked list.
+ */
+ struct ChunkHeader {
+ size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
+ size_t size; //!< Current size of allocated memory in bytes.
+ ChunkHeader *next; //!< Next chunk in the linked list.
+ };
+
+ ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
+ size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
+ void *userBuffer_; //!< User supplied buffer.
+ BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
+ BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
+};
+
+RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_ENCODINGS_H_
diff --git a/3rdparty/rapidjson/include/rapidjson/cursorstreamwrapper.h b/3rdparty/rapidjson/include/rapidjson/cursorstreamwrapper.h
new file mode 100644
index 0000000000..52c11a7c01
--- /dev/null
+++ b/3rdparty/rapidjson/include/rapidjson/cursorstreamwrapper.h
@@ -0,0 +1,78 @@
+// Tencent is pleased to support the open source community by making RapidJSON available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file except
+// in compliance with the License. You may obtain a copy of the License at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+#ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_
+#define RAPIDJSON_CURSORSTREAMWRAPPER_H_
+
+#include "stream.h"
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(effc++)
+#endif
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_PUSH
+RAPIDJSON_DIAG_OFF(4702) // unreachable code
+RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
+#endif
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+
+//! Cursor stream wrapper for counting line and column number if error exists.
+/*!
+ \tparam InputStream Any stream that implements Stream Concept
+*/
+template >
+class CursorStreamWrapper : public GenericStreamWrapper {
+public:
+ typedef typename Encoding::Ch Ch;
+
+ CursorStreamWrapper(InputStream& is):
+ GenericStreamWrapper(is), line_(1), col_(0) {}
+
+ // counting line and column number
+ Ch Take() {
+ Ch ch = this->is_.Take();
+ if(ch == '\n') {
+ line_ ++;
+ col_ = 0;
+ } else {
+ col_ ++;
+ }
+ return ch;
+ }
+
+ //! Get the error line number, if error exists.
+ size_t GetLine() const { return line_; }
+ //! Get the error column number, if error exists.
+ size_t GetColumn() const { return col_; }
+
+private:
+ size_t line_; //!< Current Line
+ size_t col_; //!< Current Column
+};
+
+#if defined(_MSC_VER) && _MSC_VER <= 1800
+RAPIDJSON_DIAG_POP
+#endif
+
+#if defined(__GNUC__)
+RAPIDJSON_DIAG_POP
+#endif
+
+RAPIDJSON_NAMESPACE_END
+
+#endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_
diff --git a/3rdparty/rapidjson/include/rapidjson/document.h b/3rdparty/rapidjson/include/rapidjson/document.h
new file mode 100644
index 0000000000..68aaae7e6b
--- /dev/null
+++ b/3rdparty/rapidjson/include/rapidjson/document.h
@@ -0,0 +1,2732 @@
+// Tencent is pleased to support the open source community by making RapidJSON available.
+//
+// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
+//
+// Licensed under the MIT License (the "License"); you may not use this file except
+// in compliance with the License. You may obtain a copy of the License at
+//
+// http://opensource.org/licenses/MIT
+//
+// Unless required by applicable law or agreed to in writing, software distributed
+// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+// CONDITIONS OF ANY KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations under the License.
+
+#ifndef RAPIDJSON_DOCUMENT_H_
+#define RAPIDJSON_DOCUMENT_H_
+
+/*! \file document.h */
+
+#include "reader.h"
+#include "internal/meta.h"
+#include "internal/strfunc.h"
+#include "memorystream.h"
+#include "encodedstream.h"
+#include // placement new
+#include
+#ifdef __cpp_lib_three_way_comparison
+#include
+#endif
+
+RAPIDJSON_DIAG_PUSH
+#ifdef __clang__
+RAPIDJSON_DIAG_OFF(padded)
+RAPIDJSON_DIAG_OFF(switch-enum)
+RAPIDJSON_DIAG_OFF(c++98-compat)
+#elif defined(_MSC_VER)
+RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
+RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
+#endif
+
+#ifdef __GNUC__
+RAPIDJSON_DIAG_OFF(effc++)
+#endif // __GNUC__
+
+#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
+#include // std::random_access_iterator_tag
+#endif
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+#include // std::move
+#endif
+
+RAPIDJSON_NAMESPACE_BEGIN
+
+// Forward declaration.
+template
+class GenericValue;
+
+template
+class GenericDocument;
+
+/*! \def RAPIDJSON_DEFAULT_ALLOCATOR
+ \ingroup RAPIDJSON_CONFIG
+ \brief Allows to choose default allocator.
+
+ User can define this to use CrtAllocator or MemoryPoolAllocator.
+*/
+#ifndef RAPIDJSON_DEFAULT_ALLOCATOR
+#define RAPIDJSON_DEFAULT_ALLOCATOR MemoryPoolAllocator
+#endif
+
+/*! \def RAPIDJSON_DEFAULT_STACK_ALLOCATOR
+ \ingroup RAPIDJSON_CONFIG
+ \brief Allows to choose default stack allocator for Document.
+
+ User can define this to use CrtAllocator or MemoryPoolAllocator.
+*/
+#ifndef RAPIDJSON_DEFAULT_STACK_ALLOCATOR
+#define RAPIDJSON_DEFAULT_STACK_ALLOCATOR CrtAllocator
+#endif
+
+/*! \def RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
+ \ingroup RAPIDJSON_CONFIG
+ \brief User defined kDefaultObjectCapacity value.
+
+ User can define this as any natural number.
+*/
+#ifndef RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY
+// number of objects that rapidjson::Value allocates memory for by default
+#define RAPIDJSON_VALUE_DEFAULT_OBJECT_CAPACITY 16
+#endif
+
+/*! \def RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
+ \ingroup RAPIDJSON_CONFIG
+ \brief User defined kDefaultArrayCapacity value.
+
+ User can define this as any natural number.
+*/
+#ifndef RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY
+// number of array elements that rapidjson::Value allocates memory for by default
+#define RAPIDJSON_VALUE_DEFAULT_ARRAY_CAPACITY 16
+#endif
+
+//! Name-value pair in a JSON object value.
+/*!
+ This class was internal to GenericValue. It used to be a inner struct.
+ But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct.
+ https://code.google.com/p/rapidjson/issues/detail?id=64
+*/
+template
+class GenericMember {
+public:
+ GenericValue name; //!< name of member (must be a string)
+ GenericValue value; //!< value of member.
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move constructor in C++11
+ GenericMember(GenericMember&& rhs) RAPIDJSON_NOEXCEPT
+ : name(std::move(rhs.name)),
+ value(std::move(rhs.value))
+ {
+ }
+
+ //! Move assignment in C++11
+ GenericMember& operator=(GenericMember&& rhs) RAPIDJSON_NOEXCEPT {
+ return *this = static_cast(rhs);
+ }
+#endif
+
+ //! Assignment with move semantics.
+ /*! \param rhs Source of the assignment. Its name and value will become a null value after assignment.
+ */
+ GenericMember& operator=(GenericMember& rhs) RAPIDJSON_NOEXCEPT {
+ if (RAPIDJSON_LIKELY(this != &rhs)) {
+ name = rhs.name;
+ value = rhs.value;
+ }
+ return *this;
+ }
+
+ // swap() for std::sort() and other potential use in STL.
+ friend inline void swap(GenericMember& a, GenericMember& b) RAPIDJSON_NOEXCEPT {
+ a.name.Swap(b.name);
+ a.value.Swap(b.value);
+ }
+
+private:
+ //! Copy constructor is not permitted.
+ GenericMember(const GenericMember& rhs);
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericMemberIterator
+
+#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
+
+//! (Constant) member iterator for a JSON object value
+/*!
+ \tparam Const Is this a constant iterator?
+ \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
+ \tparam Allocator Allocator type for allocating memory of object, array and string.
+
+ This class implements a Random Access Iterator for GenericMember elements
+ of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements].
+
+ \note This iterator implementation is mainly intended to avoid implicit
+ conversions from iterator values to \c NULL,
+ e.g. from GenericValue::FindMember.
+
+ \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a
+ pointer-based implementation, if your platform doesn't provide
+ the C++ header.
+
+ \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator
+ */
+template
+class GenericMemberIterator {
+
+ friend class GenericValue;
+ template friend class GenericMemberIterator;
+
+ typedef GenericMember PlainType;
+ typedef typename internal::MaybeAddConst::Type ValueType;
+
+public:
+ //! Iterator type itself
+ typedef GenericMemberIterator Iterator;
+ //! Constant iterator type
+ typedef GenericMemberIterator ConstIterator;
+ //! Non-constant iterator type
+ typedef GenericMemberIterator NonConstIterator;
+
+ /** \name std::iterator_traits support */
+ //@{
+ typedef ValueType value_type;
+ typedef ValueType * pointer;
+ typedef ValueType & reference;
+ typedef std::ptrdiff_t difference_type;
+ typedef std::random_access_iterator_tag iterator_category;
+ //@}
+
+ //! Pointer to (const) GenericMember
+ typedef pointer Pointer;
+ //! Reference to (const) GenericMember
+ typedef reference Reference;
+ //! Signed integer type (e.g. \c ptrdiff_t)
+ typedef difference_type DifferenceType;
+
+ //! Default constructor (singular value)
+ /*! Creates an iterator pointing to no element.
+ \note All operations, except for comparisons, are undefined on such values.
+ */
+ GenericMemberIterator() : ptr_() {}
+
+ //! Iterator conversions to more const
+ /*!
+ \param it (Non-const) iterator to copy from
+
+ Allows the creation of an iterator from another GenericMemberIterator
+ that is "less const". Especially, creating a non-constant iterator
+ from a constant iterator are disabled:
+ \li const -> non-const (not ok)
+ \li const -> const (ok)
+ \li non-const -> const (ok)
+ \li non-const -> non-const (ok)
+
+ \note If the \c Const template parameter is already \c false, this
+ constructor effectively defines a regular copy-constructor.
+ Otherwise, the copy constructor is implicitly defined.
+ */
+ GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {}
+ Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; }
+
+ //! @name stepping
+ //@{
+ Iterator& operator++(){ ++ptr_; return *this; }
+ Iterator& operator--(){ --ptr_; return *this; }
+ Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; }
+ Iterator operator--(int){ Iterator old(*this); --ptr_; return old; }
+ //@}
+
+ //! @name increment/decrement
+ //@{
+ Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); }
+ Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); }
+
+ Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; }
+ Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; }
+ //@}
+
+ //! @name relations
+ //@{
+ template bool operator==(const GenericMemberIterator& that) const { return ptr_ == that.ptr_; }
+ template bool operator!=(const GenericMemberIterator& that) const { return ptr_ != that.ptr_; }
+ template bool operator<=(const GenericMemberIterator& that) const { return ptr_ <= that.ptr_; }
+ template bool operator>=(const GenericMemberIterator& that) const { return ptr_ >= that.ptr_; }
+ template bool operator< (const GenericMemberIterator& that) const { return ptr_ < that.ptr_; }
+ template bool operator> (const GenericMemberIterator& that) const { return ptr_ > that.ptr_; }
+
+#ifdef __cpp_lib_three_way_comparison
+ template std::strong_ordering operator<=>(const GenericMemberIterator& that) const { return ptr_ <=> that.ptr_; }
+#endif
+ //@}
+
+ //! @name dereference
+ //@{
+ Reference operator*() const { return *ptr_; }
+ Pointer operator->() const { return ptr_; }
+ Reference operator[](DifferenceType n) const { return ptr_[n]; }
+ //@}
+
+ //! Distance
+ DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; }
+
+private:
+ //! Internal constructor from plain pointer
+ explicit GenericMemberIterator(Pointer p) : ptr_(p) {}
+
+ Pointer ptr_; //!< raw pointer
+};
+
+#else // RAPIDJSON_NOMEMBERITERATORCLASS
+
+// class-based member iterator implementation disabled, use plain pointers
+
+template
+class GenericMemberIterator;
+
+//! non-const GenericMemberIterator
+template
+class GenericMemberIterator {
+ //! use plain pointer as iterator type
+ typedef GenericMember* Iterator;
+};
+//! const GenericMemberIterator
+template
+class GenericMemberIterator {
+ //! use plain const pointer as iterator type
+ typedef const GenericMember* Iterator;
+};
+
+#endif // RAPIDJSON_NOMEMBERITERATORCLASS
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericStringRef
+
+//! Reference to a constant string (not taking a copy)
+/*!
+ \tparam CharType character type of the string
+
+ This helper class is used to automatically infer constant string
+ references for string literals, especially from \c const \b (!)
+ character arrays.
+
+ The main use is for creating JSON string values without copying the
+ source string via an \ref Allocator. This requires that the referenced
+ string pointers have a sufficient lifetime, which exceeds the lifetime
+ of the associated GenericValue.
+
+ \b Example
+ \code
+ Value v("foo"); // ok, no need to copy & calculate length
+ const char foo[] = "foo";
+ v.SetString(foo); // ok
+
+ const char* bar = foo;
+ // Value x(bar); // not ok, can't rely on bar's lifetime
+ Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
+ Value y(StringRef(bar, 3)); // ok, explicitly pass length
+ \endcode
+
+ \see StringRef, GenericValue::SetString
+*/
+template
+struct GenericStringRef {
+ typedef CharType Ch; //!< character type of the string
+
+ //! Create string reference from \c const character array
+#ifndef __clang__ // -Wdocumentation
+ /*!
+ This constructor implicitly creates a constant string reference from
+ a \c const character array. It has better performance than
+ \ref StringRef(const CharType*) by inferring the string \ref length
+ from the array length, and also supports strings containing null
+ characters.
+
+ \tparam N length of the string, automatically inferred
+
+ \param str Constant character array, lifetime assumed to be longer
+ than the use of the string in e.g. a GenericValue
+
+ \post \ref s == str
+
+ \note Constant complexity.
+ \note There is a hidden, private overload to disallow references to
+ non-const character arrays to be created via this constructor.
+ By this, e.g. function-scope arrays used to be filled via
+ \c snprintf are excluded from consideration.
+ In such cases, the referenced string should be \b copied to the
+ GenericValue instead.
+ */
+#endif
+ template
+ GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT
+ : s(str), length(N-1) {}
+
+ //! Explicitly create string reference from \c const character pointer
+#ifndef __clang__ // -Wdocumentation
+ /*!
+ This constructor can be used to \b explicitly create a reference to
+ a constant string pointer.
+
+ \see StringRef(const CharType*)
+
+ \param str Constant character pointer, lifetime assumed to be longer
+ than the use of the string in e.g. a GenericValue
+
+ \post \ref s == str
+
+ \note There is a hidden, private overload to disallow references to
+ non-const character arrays to be created via this constructor.
+ By this, e.g. function-scope arrays used to be filled via
+ \c snprintf are excluded from consideration.
+ In such cases, the referenced string should be \b copied to the
+ GenericValue instead.
+ */
+#endif
+ explicit GenericStringRef(const CharType* str)
+ : s(str), length(NotNullStrLen(str)) {}
+
+ //! Create constant string reference from pointer and length
+#ifndef __clang__ // -Wdocumentation
+ /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
+ \param len length of the string, excluding the trailing NULL terminator
+
+ \post \ref s == str && \ref length == len
+ \note Constant complexity.
+ */
+#endif
+ GenericStringRef(const CharType* str, SizeType len)
+ : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) { RAPIDJSON_ASSERT(str != 0 || len == 0u); }
+
+ GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {}
+
+ //! implicit conversion to plain CharType pointer
+ operator const Ch *() const { return s; }
+
+ const Ch* const s; //!< plain CharType pointer
+ const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
+
+private:
+ SizeType NotNullStrLen(const CharType* str) {
+ RAPIDJSON_ASSERT(str != 0);
+ return internal::StrLen(str);
+ }
+
+ /// Empty string - used when passing in a NULL pointer
+ static const Ch emptyString[];
+
+ //! Disallow construction from non-const array
+ template
+ GenericStringRef(CharType (&str)[N]) /* = delete */;
+ //! Copy assignment operator not permitted - immutable type
+ GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */;
+};
+
+template
+const CharType GenericStringRef::emptyString[] = { CharType() };
+
+//! Mark a character pointer as constant string
+/*! Mark a plain character pointer as a "string literal". This function
+ can be used to avoid copying a character string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+ \tparam CharType Character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
+ \return GenericStringRef string reference object
+ \relatesalso GenericStringRef
+
+ \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember
+*/
+template
+inline GenericStringRef StringRef(const CharType* str) {
+ return GenericStringRef(str);
+}
+
+//! Mark a character pointer as constant string
+/*! Mark a plain character pointer as a "string literal". This function
+ can be used to avoid copying a character string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+
+ This version has better performance with supplied length, and also
+ supports string containing null characters.
+
+ \tparam CharType character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
+ \param length The length of source string.
+ \return GenericStringRef string reference object
+ \relatesalso GenericStringRef
+*/
+template
+inline GenericStringRef StringRef(const CharType* str, size_t length) {
+ return GenericStringRef(str, SizeType(length));
+}
+
+#if RAPIDJSON_HAS_STDSTRING
+//! Mark a string object as constant string
+/*! Mark a string object (e.g. \c std::string) as a "string literal".
+ This function can be used to avoid copying a string to be referenced as a
+ value in a JSON GenericValue object, if the string's lifetime is known
+ to be valid long enough.
+
+ \tparam CharType character type of the string
+ \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
+ \return GenericStringRef string reference object
+ \relatesalso GenericStringRef
+ \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
+*/
+template
+inline GenericStringRef StringRef(const std::basic_string& str) {
+ return GenericStringRef(str.data(), SizeType(str.size()));
+}
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericValue type traits
+namespace internal {
+
+template
+struct IsGenericValueImpl : FalseType {};
+
+// select candidates according to nested encoding and allocator types
+template struct IsGenericValueImpl::Type, typename Void::Type>
+ : IsBaseOf, T>::Type {};
+
+// helper to match arbitrary GenericValue instantiations, including derived classes
+template struct IsGenericValue : IsGenericValueImpl::Type {};
+
+} // namespace internal
+
+///////////////////////////////////////////////////////////////////////////////
+// TypeHelper
+
+namespace internal {
+
+template
+struct TypeHelper {};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsBool(); }
+ static bool Get(const ValueType& v) { return v.GetBool(); }
+ static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); }
+ static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsInt(); }
+ static int Get(const ValueType& v) { return v.GetInt(); }
+ static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); }
+ static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsUint(); }
+ static unsigned Get(const ValueType& v) { return v.GetUint(); }
+ static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); }
+ static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); }
+};
+
+#ifdef _MSC_VER
+RAPIDJSON_STATIC_ASSERT(sizeof(long) == sizeof(int));
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsInt(); }
+ static long Get(const ValueType& v) { return v.GetInt(); }
+ static ValueType& Set(ValueType& v, long data) { return v.SetInt(data); }
+ static ValueType& Set(ValueType& v, long data, typename ValueType::AllocatorType&) { return v.SetInt(data); }
+};
+
+RAPIDJSON_STATIC_ASSERT(sizeof(unsigned long) == sizeof(unsigned));
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsUint(); }
+ static unsigned long Get(const ValueType& v) { return v.GetUint(); }
+ static ValueType& Set(ValueType& v, unsigned long data) { return v.SetUint(data); }
+ static ValueType& Set(ValueType& v, unsigned long data, typename ValueType::AllocatorType&) { return v.SetUint(data); }
+};
+#endif
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsInt64(); }
+ static int64_t Get(const ValueType& v) { return v.GetInt64(); }
+ static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); }
+ static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsUint64(); }
+ static uint64_t Get(const ValueType& v) { return v.GetUint64(); }
+ static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); }
+ static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsDouble(); }
+ static double Get(const ValueType& v) { return v.GetDouble(); }
+ static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); }
+ static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); }
+};
+
+template
+struct TypeHelper {
+ static bool Is(const ValueType& v) { return v.IsFloat(); }
+ static float Get(const ValueType& v) { return v.GetFloat(); }
+ static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); }
+ static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); }
+};
+
+template
+struct TypeHelper {
+ typedef const typename ValueType::Ch* StringType;
+ static bool Is(const ValueType& v) { return v.IsString(); }
+ static StringType Get(const ValueType& v) { return v.GetString(); }
+ static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); }
+ static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); }
+};
+
+#if RAPIDJSON_HAS_STDSTRING
+template
+struct TypeHelper > {
+ typedef std::basic_string StringType;
+ static bool Is(const ValueType& v) { return v.IsString(); }
+ static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); }
+ static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); }
+};
+#endif
+
+template
+struct TypeHelper {
+ typedef typename ValueType::Array ArrayType;
+ static bool Is(const ValueType& v) { return v.IsArray(); }
+ static ArrayType Get(ValueType& v) { return v.GetArray(); }
+ static ValueType& Set(ValueType& v, ArrayType data) { return v = data; }
+ static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::ConstArray ArrayType;
+ static bool Is(const ValueType& v) { return v.IsArray(); }
+ static ArrayType Get(const ValueType& v) { return v.GetArray(); }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::Object ObjectType;
+ static bool Is(const ValueType& v) { return v.IsObject(); }
+ static ObjectType Get(ValueType& v) { return v.GetObject(); }
+ static ValueType& Set(ValueType& v, ObjectType data) { return v = data; }
+ static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; }
+};
+
+template
+struct TypeHelper {
+ typedef typename ValueType::ConstObject ObjectType;
+ static bool Is(const ValueType& v) { return v.IsObject(); }
+ static ObjectType Get(const ValueType& v) { return v.GetObject(); }
+};
+
+} // namespace internal
+
+// Forward declarations
+template class GenericArray;
+template class GenericObject;
+
+///////////////////////////////////////////////////////////////////////////////
+// GenericValue
+
+//! Represents a JSON value. Use Value for UTF8 encoding and default allocator.
+/*!
+ A JSON value can be one of 7 types. This class is a variant type supporting
+ these types.
+
+ Use the Value if UTF8 and default allocator
+
+ \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
+ \tparam Allocator Allocator type for allocating memory of object, array and string.
+*/
+template
+class GenericValue {
+public:
+ //! Name-value pair in an object.
+ typedef GenericMember Member;
+ typedef Encoding EncodingType; //!< Encoding type from template parameter.
+ typedef Allocator AllocatorType; //!< Allocator type from template parameter.
+ typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
+ typedef GenericStringRef StringRefType; //!< Reference to a constant string
+ typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object.
+ typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object.
+ typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array.
+ typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
+ typedef GenericValue ValueType; //!< Value type of itself.
+ typedef GenericArray Array;
+ typedef GenericArray ConstArray;
+ typedef GenericObject Object;
+ typedef GenericObject ConstObject;
+
+ //!@name Constructors and destructor.
+ //@{
+
+ //! Default constructor creates a null value.
+ GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move constructor in C++11
+ GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) {
+ rhs.data_.f.flags = kNullFlag; // give up contents
+ }
+#endif
+
+private:
+ //! Copy constructor is not permitted.
+ GenericValue(const GenericValue& rhs);
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Moving from a GenericDocument is not permitted.
+ template
+ GenericValue(GenericDocument&& rhs);
+
+ //! Move assignment from a GenericDocument is not permitted.
+ template
+ GenericValue& operator=(GenericDocument&& rhs);
+#endif
+
+public:
+
+ //! Constructor with JSON value type.
+ /*! This creates a Value of specified type with default content.
+ \param type Type of the value.
+ \note Default content for number is zero.
+ */
+ explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() {
+ static const uint16_t defaultFlags[] = {
+ kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
+ kNumberAnyFlag
+ };
+ RAPIDJSON_NOEXCEPT_ASSERT(type >= kNullType && type <= kNumberType);
+ data_.f.flags = defaultFlags[type];
+
+ // Use ShortString to store empty string.
+ if (type == kStringType)
+ data_.ss.SetLength(0);
+ }
+
+ //! Explicit copy constructor (with allocator)
+ /*! Creates a copy of a Value by using the given Allocator
+ \tparam SourceAllocator allocator of \c rhs
+ \param rhs Value to copy from (read-only)
+ \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
+ \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
+ \see CopyFrom()
+ */
+ template
+ GenericValue(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) {
+ switch (rhs.GetType()) {
+ case kObjectType: {
+ SizeType count = rhs.data_.o.size;
+ Member* lm = reinterpret_cast(allocator.Malloc(count * sizeof(Member)));
+ const typename GenericValue::Member* rm = rhs.GetMembersPointer();
+ for (SizeType i = 0; i < count; i++) {
+ new (&lm[i].name) GenericValue(rm[i].name, allocator, copyConstStrings);
+ new (&lm[i].value) GenericValue(rm[i].value, allocator, copyConstStrings);
+ }
+ data_.f.flags = kObjectFlag;
+ data_.o.size = data_.o.capacity = count;
+ SetMembersPointer(lm);
+ }
+ break;
+ case kArrayType: {
+ SizeType count = rhs.data_.a.size;
+ GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue)));
+ const GenericValue* re = rhs.GetElementsPointer();
+ for (SizeType i = 0; i < count; i++)
+ new (&le[i]) GenericValue(re[i], allocator, copyConstStrings);
+ data_.f.flags = kArrayFlag;
+ data_.a.size = data_.a.capacity = count;
+ SetElementsPointer(le);
+ }
+ break;
+ case kStringType:
+ if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) {
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast(&rhs.data_);
+ }
+ else
+ SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
+ break;
+ default:
+ data_.f.flags = rhs.data_.f.flags;
+ data_ = *reinterpret_cast(&rhs.data_);
+ break;
+ }
+ }
+
+ //! Constructor for boolean value.
+ /*! \param b Boolean value
+ \note This constructor is limited to \em real boolean values and rejects
+ implicitly converted types like arbitrary pointers. Use an explicit cast
+ to \c bool, if you want to construct a boolean JSON value in such cases.
+ */
+#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
+ template
+ explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT // See #472
+#else
+ explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT
+#endif
+ : data_() {
+ // safe-guard against failing SFINAE
+ RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value));
+ data_.f.flags = b ? kTrueFlag : kFalseFlag;
+ }
+
+ //! Constructor for int value.
+ explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.i64 = i;
+ data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag;
+ }
+
+ //! Constructor for unsigned value.
+ explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.u64 = u;
+ data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag);
+ }
+
+ //! Constructor for int64_t value.
+ explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.i64 = i64;
+ data_.f.flags = kNumberInt64Flag;
+ if (i64 >= 0) {
+ data_.f.flags |= kNumberUint64Flag;
+ if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
+ data_.f.flags |= kUintFlag;
+ if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ }
+ else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ }
+
+ //! Constructor for uint64_t value.
+ explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() {
+ data_.n.u64 = u64;
+ data_.f.flags = kNumberUint64Flag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
+ data_.f.flags |= kInt64Flag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
+ data_.f.flags |= kUintFlag;
+ if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
+ data_.f.flags |= kIntFlag;
+ }
+
+ //! Constructor for double value.
+ explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; }
+
+ //! Constructor for float value.
+ explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = static_cast(f); data_.f.flags = kNumberDoubleFlag; }
+
+ //! Constructor for constant string (i.e. do not make a copy of string)
+ GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); }
+
+ //! Constructor for constant string (i.e. do not make a copy of string)
+ explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); }
+
+ //! Constructor for copy-string (i.e. do make a copy of string)
+ GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); }
+
+ //! Constructor for copy-string (i.e. do make a copy of string)
+ GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Constructor for copy-string from a string object (i.e. do make a copy of string)
+ /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
+ */
+ GenericValue(const std::basic_string& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); }
+#endif
+
+ //! Constructor for Array.
+ /*!
+ \param a An array obtained by \c GetArray().
+ \note \c Array is always pass-by-value.
+ \note the source array is moved into this value and the sourec array becomes empty.
+ */
+ GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) {
+ a.value_.data_ = Data();
+ a.value_.data_.f.flags = kArrayFlag;
+ }
+
+ //! Constructor for Object.
+ /*!
+ \param o An object obtained by \c GetObject().
+ \note \c Object is always pass-by-value.
+ \note the source object is moved into this value and the sourec object becomes empty.
+ */
+ GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) {
+ o.value_.data_ = Data();
+ o.value_.data_.f.flags = kObjectFlag;
+ }
+
+ //! Destructor.
+ /*! Need to destruct elements of array, members of object, or copy-string.
+ */
+ ~GenericValue() {
+ if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
+ switch(data_.f.flags) {
+ case kArrayFlag:
+ {
+ GenericValue* e = GetElementsPointer();
+ for (GenericValue* v = e; v != e + data_.a.size; ++v)
+ v->~GenericValue();
+ Allocator::Free(e);
+ }
+ break;
+
+ case kObjectFlag:
+ for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
+ m->~Member();
+ Allocator::Free(GetMembersPointer());
+ break;
+
+ case kCopyStringFlag:
+ Allocator::Free(const_cast(GetStringPointer()));
+ break;
+
+ default:
+ break; // Do nothing for other types.
+ }
+ }
+ }
+
+ //@}
+
+ //!@name Assignment operators
+ //@{
+
+ //! Assignment with move semantics.
+ /*! \param rhs Source of the assignment. It will become a null value after assignment.
+ */
+ GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
+ if (RAPIDJSON_LIKELY(this != &rhs)) {
+ this->~GenericValue();
+ RawAssign(rhs);
+ }
+ return *this;
+ }
+
+#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
+ //! Move assignment in C++11
+ GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT {
+ return *this = rhs.Move();
+ }
+#endif
+
+ //! Assignment of constant string reference (no copy)
+ /*! \param str Constant string reference to be assigned
+ \note This overload is needed to avoid clashes with the generic primitive type assignment overload below.
+ \see GenericStringRef, operator=(T)
+ */
+ GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT {
+ GenericValue s(str);
+ return *this = s;
+ }
+
+ //! Assignment with primitive types.
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
+ \param value The value to be assigned.
+
+ \note The source type \c T explicitly disallows all pointer types,
+ especially (\c const) \ref Ch*. This helps avoiding implicitly
+ referencing character strings with insufficient lifetime, use
+ \ref SetString(const Ch*, Allocator&) (for copying) or
+ \ref StringRef() (to explicitly mark the pointer as constant) instead.
+ All other pointer types would implicitly convert to \c bool,
+ use \ref SetBool() instead.
+ */
+ template
+ RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&))
+ operator=(T value) {
+ GenericValue v(value);
+ return *this = v;
+ }
+
+ //! Deep-copy assignment from Value
+ /*! Assigns a \b copy of the Value to the current Value object
+ \tparam SourceAllocator Allocator type of \c rhs
+ \param rhs Value to copy from (read-only)
+ \param allocator Allocator to use for copying
+ \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
+ */
+ template
+ GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) {
+ RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs));
+ this->~GenericValue();
+ new (this) GenericValue(rhs, allocator, copyConstStrings);
+ return *this;
+ }
+
+ //! Exchange the contents of this value with those of other.
+ /*!
+ \param other Another value.
+ \note Constant complexity.
+ */
+ GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT {
+ GenericValue temp;
+ temp.RawAssign(*this);
+ RawAssign(other);
+ other.RawAssign(temp);
+ return *this;
+ }
+
+ //! free-standing swap function helper
+ /*!
+ Helper function to enable support for common swap implementation pattern based on \c std::swap:
+ \code
+ void swap(MyClass& a, MyClass& b) {
+ using std::swap;
+ swap(a.value, b.value);
+ // ...
+ }
+ \endcode
+ \see Swap()
+ */
+ friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); }
+
+ //! Prepare Value for move semantics
+ /*! \return *this */
+ GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; }
+ //@}
+
+ //!@name Equal-to and not-equal-to operators
+ //@{
+ //! Equal-to operator
+ /*!
+ \note If an object contains duplicated named member, comparing equality with any object is always \c false.
+ \note Complexity is quadratic in Object's member number and linear for the rest (number of all values in the subtree and total lengths of all strings).
+ */
+ template
+ bool operator==(const GenericValue& rhs) const {
+ typedef GenericValue RhsType;
+ if (GetType() != rhs.GetType())
+ return false;
+
+ switch (GetType()) {
+ case kObjectType: // Warning: O(n^2) inner-loop
+ if (data_.o.size != rhs.data_.o.size)
+ return false;
+ for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
+ typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
+ if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
+ return false;
+ }
+ return true;
+
+ case kArrayType:
+ if (data_.a.size != rhs.data_.a.size)
+ return false;
+ for (SizeType i = 0; i < data_.a.size; i++)
+ if ((*this)[i] != rhs[i])
+ return false;
+ return true;
+
+ case kStringType:
+ return StringEqual(rhs);
+
+ case kNumberType:
+ if (IsDouble() || rhs.IsDouble()) {
+ double a = GetDouble(); // May convert from integer to double.
+ double b = rhs.GetDouble(); // Ditto
+ return a >= b && a <= b; // Prevent -Wfloat-equal
+ }
+ else
+ return data_.n.u64 == rhs.data_.n.u64;
+
+ default:
+ return true;
+ }
+ }
+
+ //! Equal-to operator with const C-string pointer
+ bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); }
+
+#if RAPIDJSON_HAS_STDSTRING
+ //! Equal-to operator with string object
+ /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
+ */
+ bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); }
+#endif
+
+ //! Equal-to operator with primitive types
+ /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false
+ */
+ template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); }
+
+ //! Not-equal-to operator
+ /*! \return !(*this == rhs)
+ */
+ template
+ bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); }
+
+ //! Not-equal-to operator with const C-string pointer
+ bool operator!=(const Ch* rhs) const { return !(*this == rhs); }
+
+ //! Not-equal-to operator with arbitrary types
+ /*! \return !(*this == rhs)
+ */
+ template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); }
+
+ //! Equal-to operator with arbitrary types (symmetric version)
+ /*! \return (rhs == lhs)
+ */
+ template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; }
+
+ //! Not-Equal-to operator with arbitrary types (symmetric version)
+ /*! \return !(rhs == lhs)
+ */
+ template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); }
+ //@}
+
+ //!@name Type
+ //@{
+
+ Type GetType() const { return static_cast(data_.f.flags & kTypeMask); }
+ bool IsNull() const { return data_.f.flags == kNullFlag; }
+ bool IsFalse() const { return data_.f.flags == kFalseFlag; }
+ bool IsTrue() const { return data_.f.flags == kTrueFlag; }
+ bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; }
+ bool IsObject() const { return data_.f.flags == kObjectFlag; }
+ bool IsArray() const { return data_.f.flags == kArrayFlag; }
+ bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; }
+ bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; }
+ bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; }
+ bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; }
+ bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; }
+ bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; }
+ bool IsString() const { return (data_.f.flags & kStringFlag) != 0; }
+
+ // Checks whether a number can be losslessly converted to a double.
+ bool IsLosslessDouble() const {
+ if (!IsNumber()) return false;
+ if (IsUint64()) {
+ uint64_t u = GetUint64();
+ volatile double d = static_cast(u);
+ return (d >= 0.0)
+ && (d < static_cast((std::numeric_limits::max)()))
+ && (u == static_cast(d));
+ }
+ if (IsInt64()) {
+ int64_t i = GetInt64();
+ volatile double d = static_cast(i);
+ return (d >= static_cast((std::numeric_limits::min)()))
+ && (d < static_cast((std::numeric_limits