| 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_STRINGBUFFER_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
16 |
#define CEREAL_RAPIDJSON_STRINGBUFFER_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 |
#if CEREAL_RAPIDJSON_HAS_CXX11_RVALUE_REFS |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
22 |
#include <utility> // std::move |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
23 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
24 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
25 |
#include "internal/stack.h" |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
26 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
27 |
#if defined(__clang__) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
28 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
29 |
CEREAL_RAPIDJSON_DIAG_OFF(c++98-compat) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
30 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
31 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
32 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
33 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
34 |
//! Represents an in-memory output stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
35 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
36 |
\tparam Encoding Encoding of the stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
37 |
\tparam Allocator type for allocating memory buffer. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
38 |
\note implements Stream concept |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
39 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
40 |
template <typename Encoding, typename Allocator = CrtAllocator> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
41 |
class GenericStringBuffer { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
42 |
public: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
43 |
typedef typename Encoding::Ch Ch; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
44 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
45 |
GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
46 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
47 |
#if CEREAL_RAPIDJSON_HAS_CXX11_RVALUE_REFS |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
48 |
GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
49 |
GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
50 |
if (&rhs != this) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
51 |
stack_ = std::move(rhs.stack_); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
52 |
return *this; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
53 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
54 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
55 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
56 |
void Put(Ch c) { *stack_.template Push<Ch>() = c; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
57 |
void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
58 |
void Flush() {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
59 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
60 |
void Clear() { stack_.Clear(); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
61 |
void ShrinkToFit() { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
62 |
// Push and pop a null terminator. This is safe. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
63 |
*stack_.template Push<Ch>() = '\0'; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
64 |
stack_.ShrinkToFit(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
65 |
stack_.template Pop<Ch>(1); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
66 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
67 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
68 |
void Reserve(size_t count) { stack_.template Reserve<Ch>(count); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
69 |
Ch* Push(size_t count) { return stack_.template Push<Ch>(count); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
70 |
Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
71 |
void Pop(size_t count) { stack_.template Pop<Ch>(count); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
72 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
73 |
const Ch* GetString() const { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
74 |
// Push and pop a null terminator. This is safe. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
75 |
*stack_.template Push<Ch>() = '\0'; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
76 |
stack_.template Pop<Ch>(1); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
77 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
78 |
return stack_.template Bottom<Ch>(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
79 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
80 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
81 |
size_t GetSize() const { return stack_.GetSize(); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
82 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
83 |
static const size_t kDefaultCapacity = 256; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
84 |
mutable internal::Stack<Allocator> stack_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
85 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
86 |
private: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
87 |
// Prohibit copy constructor & assignment operator. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
88 |
GenericStringBuffer(const GenericStringBuffer&); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
89 |
GenericStringBuffer& operator=(const GenericStringBuffer&); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
90 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
91 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
92 |
//! String buffer with UTF8 encoding |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
93 |
typedef GenericStringBuffer<UTF8<> > StringBuffer; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
94 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
95 |
template<typename Encoding, typename Allocator> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
96 |
inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
97 |
stream.Reserve(count); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
98 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
99 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
100 |
template<typename Encoding, typename Allocator> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
101 |
inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
102 |
stream.PutUnsafe(c); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
103 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
104 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
105 |
//! Implement specialized version of PutN() with memset() for better performance. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
106 |
template<> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
107 |
inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
108 |
std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c)); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
109 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
110 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
111 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
112 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
113 |
#if defined(__clang__) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
114 |
CEREAL_RAPIDJSON_DIAG_POP |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
115 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
116 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
117 |
#endif // CEREAL_RAPIDJSON_STRINGBUFFER_H_ |
|
|
1970-01-01 00:00 |
118 |
|