15#ifndef RAPIDJSON_STRINGBUFFER_H_
16#define RAPIDJSON_STRINGBUFFER_H_
19#include "internal/stack.h"
21#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
25#include "internal/stack.h"
29RAPIDJSON_DIAG_OFF(c++98-compat)
32RAPIDJSON_NAMESPACE_BEGIN
40template <
typename Encoding,
typename Allocator = CrtAllocator>
41class GenericStringBuffer {
43 typedef typename Encoding::Ch Ch;
45 GenericStringBuffer(
Allocator* allocator = 0,
size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
47#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
48 GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {}
49 GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
51 stack_ = std::move(rhs.stack_);
56 void Put(Ch c) { *stack_.template Push<Ch>() = c; }
57 void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
60 void Clear() { stack_.Clear(); }
63 *stack_.template Push<Ch>() =
'\0';
65 stack_.template Pop<Ch>(1);
68 void Reserve(
size_t count) { stack_.template Reserve<Ch>(count); }
69 Ch* Push(
size_t count) {
return stack_.template Push<Ch>(count); }
70 Ch* PushUnsafe(
size_t count) {
return stack_.template PushUnsafe<Ch>(count); }
71 void Pop(
size_t count) { stack_.template Pop<Ch>(count); }
73 const Ch* GetString()
const {
75 *stack_.template Push<Ch>() =
'\0';
76 stack_.template Pop<Ch>(1);
78 return stack_.template Bottom<Ch>();
82 size_t GetSize()
const {
return stack_.GetSize(); }
85 size_t GetLength()
const {
return stack_.GetSize() /
sizeof(Ch); }
87 static const size_t kDefaultCapacity = 256;
88 mutable internal::Stack<Allocator> stack_;
97typedef GenericStringBuffer<UTF8<> > StringBuffer;
99template<
typename Encoding,
typename Allocator>
100inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream,
size_t count) {
101 stream.Reserve(count);
104template<
typename Encoding,
typename Allocator>
105inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream,
typename Encoding::Ch c) {
112 std::memset(stream.stack_.Push<
char>(n), c, n *
sizeof(c));
115RAPIDJSON_NAMESPACE_END
117#if defined(__clang__)
Concept for allocating, resizing and freeing memory block.
Represents an in-memory output stream.
Definition stringbuffer.h:41
size_t GetLength() const
Get the length of string in Ch in the string buffer.
Definition stringbuffer.h:85
size_t GetSize() const
Get the size of string in bytes in the string buffer.
Definition stringbuffer.h:82
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition stream.h:91
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition filewritestream.h:94
UTF-8 encoding.
Definition fwd.h:24