memorybuffer.h

Engine/source/persistence/rapidjson/memorybuffer.h

More...

Classes:

class

Represents an in-memory output byte stream.

Public Typedefs

MemoryBuffer 

Public Functions

PutN(MemoryBuffer & memoryBuffer, char c, size_t n)

Implement specialized version of PutN() with memset() for better performance.

Detailed Description

Public Typedefs

typedef GenericMemoryBuffer MemoryBuffer 

Public Functions

PutN(MemoryBuffer & memoryBuffer, char c, size_t n)

Implement specialized version of PutN() with memset() for better performance.

 1
 2// Tencent is pleased to support the open source community by making RapidJSON available.
 3// 
 4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
 5//
 6// Licensed under the MIT License (the "License"); you may not use this file except
 7// in compliance with the License. You may obtain a copy of the License at
 8//
 9// http://opensource.org/licenses/MIT
10//
11// Unless required by applicable law or agreed to in writing, software distributed 
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
14// specific language governing permissions and limitations under the License.
15
16#ifndef RAPIDJSON_MEMORYBUFFER_H_
17#define RAPIDJSON_MEMORYBUFFER_H_
18
19#include "stream.h"
20#include "internal/stack.h"
21
22RAPIDJSON_NAMESPACE_BEGIN
23
24//! Represents an in-memory output byte stream.
25/*!
26    This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream.
27
28    It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file.
29
30    Differences between MemoryBuffer and StringBuffer:
31    1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. 
32    2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator.
33
34    \tparam Allocator type for allocating memory buffer.
35    \note implements Stream concept
36*/
37template <typename Allocator = CrtAllocator>
38struct GenericMemoryBuffer {
39    typedef char Ch; // byte
40
41    GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
42
43    void Put(Ch c) { *stack_.template Push<Ch>() = c; }
44    void Flush() {}
45
46    void Clear() { stack_.Clear(); }
47    void ShrinkToFit() { stack_.ShrinkToFit(); }
48    Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
49    void Pop(size_t count) { stack_.template Pop<Ch>(count); }
50
51    const Ch* GetBuffer() const {
52        return stack_.template Bottom<Ch>();
53    }
54
55    size_t GetSize() const { return stack_.GetSize(); }
56
57    static const size_t kDefaultCapacity = 256;
58    mutable internal::Stack<Allocator> stack_;
59};
60
61typedef GenericMemoryBuffer<> MemoryBuffer;
62
63//! Implement specialized version of PutN() with memset() for better performance.
64template<>
65inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
66    std::memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
67}
68
69RAPIDJSON_NAMESPACE_END
70
71#endif // RAPIDJSON_MEMORYBUFFER_H_
72