Open BitProgress opened 3 years ago
The best way to figure out the fun templates is to read through them! :)
If we look at the Document
type, we'll find it's a typedef.
typedef GenericDocument<UTF8<>> Document;
If we dig deeper into what that GenericDocument
is, we find
template <typename Encoding, typename Allocator = RAPIDJSON_DEFAULT_ALLOCATOR, typename StackAllocator = RAPIDJSON_DEFAULT_STACK_ALLOCATOR >
class GenericDocument : public GenericValue<Encoding, Allocator>;
This now gets us a bit closer to understanding what template arguments are needed for that allocator you've mentioned. If we look at Allocator
, we can see that we can either redefine the RAPIDJSON_DEFAULT_ALLOCATOR
before including rapidjson headers or we can do a full definition of the GenericDocument
as our own version of document. Something like
using MyDocument = GenericDocument<UTF8<>, MemoryAllocator>;
This assumes you don't also want to override the stack allocator. Overriding the stack allocator would just require an additional template argument or defining RAPIDJSON_DEFAULT_STACK_ALLOCATOR
. The only thing to keep in mind is how the allocator type is involved in the Value
type that is exposed and extended by the document. Keep that in mind if you are creating values that you then add to the JSON document.
Half the fun of writing C++ is learning the embedded language known as templates :).
I'll also reference #1959 . Over there was a discussion of using a different memory allocator for usage in a FreeRTOS system. You might find that discussion useful.
Sorry to create this issue, but I am having a hard time to create a document using own Allocator. I have created a class called MemoryAllocator, which has the required methods.
As I understand, "Document d;" create an UTF8 document with default allocators etc. My knowledge about templates is very little. How would it look, if I need to create a Document with my MemoryAllocator. I thought I would be able to write "Document d(new MemoryAllocator);". But it does not compile correctly :-)
I would appreciate a little help. Thanks.