parent
8609c68f31
commit
5e09f1a569
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/* clang-format off */
|
// clang-format off
|
||||||
|
|
||||||
#if defined(DISCORD_DYNAMIC_LIB)
|
#if defined(DISCORD_DYNAMIC_LIB)
|
||||||
# if defined(_WIN32)
|
# if defined(_WIN32)
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
# define DISCORD_EXPORT
|
# define DISCORD_EXPORT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* clang-format on */
|
// clang-format on
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
#ifndef __MINGW32__
|
#ifndef __MINGW32__
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
|
|
||||||
#pragma warning(disable : 4061) /* enum is not explicitly handled by a case label */
|
#pragma warning(disable : 4061) // enum is not explicitly handled by a case label
|
||||||
#pragma warning(disable : 4365) /* signed/unsigned mismatch */
|
#pragma warning(disable : 4365) // signed/unsigned mismatch
|
||||||
#pragma warning(disable : 4464) /* relative include path contains */
|
#pragma warning(disable : 4464) // relative include path contains
|
||||||
#pragma warning(disable : 4668) /* is not defined as a preprocessor macro */
|
#pragma warning(disable : 4668) // is not defined as a preprocessor macro
|
||||||
#pragma warning(disable : 6313) /* Incorrect operator*/
|
#pragma warning(disable : 6313) // Incorrect operator
|
||||||
#endif /* __MINGW32__ */
|
#endif // __MINGW32__
|
||||||
|
|
||||||
#include "rapidjson/document.h"
|
#include "rapidjson/document.h"
|
||||||
#include "rapidjson/stringbuffer.h"
|
#include "rapidjson/stringbuffer.h"
|
||||||
|
@ -18,26 +18,27 @@
|
||||||
|
|
||||||
#ifndef __MINGW32__
|
#ifndef __MINGW32__
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif /* __MINGW32__ */
|
#endif // __MINGW32__
|
||||||
|
|
||||||
/* if only there was a standard library function for this */
|
// if only there was a standard library function for this
|
||||||
template <size_t Len>
|
template <size_t Len>
|
||||||
inline size_t StringCopy(char (&dest)[Len], const char* src)
|
inline size_t StringCopy(char (&dest)[Len], const char* src)
|
||||||
{
|
{
|
||||||
size_t copied;
|
if (!src || !Len) {
|
||||||
char *out;
|
|
||||||
if (!src || !Len)
|
|
||||||
return 0;
|
return 0;
|
||||||
out = dest;
|
}
|
||||||
for (copied = 1; *src && copied < Len; ++copied)
|
size_t copied;
|
||||||
|
char* out = dest;
|
||||||
|
for (copied = 1; *src && copied < Len; ++copied) {
|
||||||
*out++ = *src++;
|
*out++ = *src++;
|
||||||
|
}
|
||||||
*out = 0;
|
*out = 0;
|
||||||
return copied - 1;
|
return copied - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId);
|
size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId);
|
||||||
|
|
||||||
/* Commands */
|
// Commands
|
||||||
struct DiscordRichPresence;
|
struct DiscordRichPresence;
|
||||||
size_t JsonWriteRichPresenceObj(char* dest,
|
size_t JsonWriteRichPresenceObj(char* dest,
|
||||||
size_t maxLen,
|
size_t maxLen,
|
||||||
|
@ -50,9 +51,8 @@ size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const c
|
||||||
|
|
||||||
size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce);
|
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
|
// 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
|
// to supply some of your own allocators for stuff rather than use the defaults
|
||||||
*/
|
|
||||||
|
|
||||||
class LinearAllocator {
|
class LinearAllocator {
|
||||||
public:
|
public:
|
||||||
|
@ -60,7 +60,7 @@ public:
|
||||||
char* end_;
|
char* end_;
|
||||||
LinearAllocator()
|
LinearAllocator()
|
||||||
{
|
{
|
||||||
assert(0); /* needed for some default case in rapidjson, should not use */
|
assert(0); // needed for some default case in rapidjson, should not use
|
||||||
}
|
}
|
||||||
LinearAllocator(char* buffer, size_t size)
|
LinearAllocator(char* buffer, size_t size)
|
||||||
: buffer_(buffer)
|
: buffer_(buffer)
|
||||||
|
@ -80,11 +80,12 @@ public:
|
||||||
}
|
}
|
||||||
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize)
|
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize)
|
||||||
{
|
{
|
||||||
if (newSize == 0)
|
if (newSize == 0) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
/* allocate how much you need in the first place */
|
}
|
||||||
|
// allocate how much you need in the first place
|
||||||
assert(!originalPtr && !originalSize);
|
assert(!originalPtr && !originalSize);
|
||||||
/* unused parameter warning */
|
// unused parameter warning
|
||||||
(void)(originalPtr);
|
(void)(originalPtr);
|
||||||
(void)(originalSize);
|
(void)(originalSize);
|
||||||
return Malloc(newSize);
|
return Malloc(newSize);
|
||||||
|
@ -107,7 +108,7 @@ public:
|
||||||
static const bool kNeedFree = false;
|
static const bool kNeedFree = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* wonder why this isn't a thing already, maybe I missed it */
|
// wonder why this isn't a thing already, maybe I missed it
|
||||||
class DirectStringBuffer {
|
class DirectStringBuffer {
|
||||||
public:
|
public:
|
||||||
using Ch = char;
|
using Ch = char;
|
||||||
|
@ -135,7 +136,7 @@ public:
|
||||||
using MallocAllocator = rapidjson::CrtAllocator;
|
using MallocAllocator = rapidjson::CrtAllocator;
|
||||||
using PoolAllocator = rapidjson::MemoryPoolAllocator<MallocAllocator>;
|
using PoolAllocator = rapidjson::MemoryPoolAllocator<MallocAllocator>;
|
||||||
using UTF8 = rapidjson::UTF8<char>;
|
using UTF8 = rapidjson::UTF8<char>;
|
||||||
/* Writer appears to need about 16 bytes per nested object level (with 64bit size_t) */
|
// Writer appears to need about 16 bytes per nested object level (with 64bit size_t)
|
||||||
using StackAllocator = FixedLinearAllocator<2048>;
|
using StackAllocator = FixedLinearAllocator<2048>;
|
||||||
constexpr size_t WriterNestingLevels = 2048 / (2 * sizeof(size_t));
|
constexpr size_t WriterNestingLevels = 2048 / (2 * sizeof(size_t));
|
||||||
using JsonWriterBase =
|
using JsonWriterBase =
|
||||||
|
@ -159,8 +160,8 @@ using JsonDocumentBase = rapidjson::GenericDocument<UTF8, PoolAllocator, StackAl
|
||||||
class JsonDocument : public JsonDocumentBase {
|
class JsonDocument : public JsonDocumentBase {
|
||||||
public:
|
public:
|
||||||
static const int kDefaultChunkCapacity = 32 * 1024;
|
static const int kDefaultChunkCapacity = 32 * 1024;
|
||||||
/* json parser will use this buffer first, then allocate more if needed; I seriously doubt we
|
// 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. */
|
// send any messages that would use all of this, though.
|
||||||
char parseBuffer_[32 * 1024];
|
char parseBuffer_[32 * 1024];
|
||||||
MallocAllocator mallocAllocator_;
|
MallocAllocator mallocAllocator_;
|
||||||
PoolAllocator poolAllocator_;
|
PoolAllocator poolAllocator_;
|
||||||
|
|
Loading…
Reference in New Issue