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