stringbuffer.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_STRINGBUFFER_H_
16 #define RAPIDJSON_STRINGBUFFER_H_
17 
18 #include "stream.h"
19 #include "internal/stack.h"
20 
21 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
22 #include <utility> // std::move
23 #endif
24 
25 #include "internal/stack.h"
26 
27 #if defined(__clang__)
28 RAPIDJSON_DIAG_PUSH
29 RAPIDJSON_DIAG_OFF(c++ 98 - compat)
30 #endif
31 
33 
35 
40 template <typename Encoding, typename Allocator = CrtAllocator>
42 {
43 public:
44  typedef typename Encoding::Ch Ch;
45 
46  GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity)
47  {
48  }
49 
50 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
51  GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_))
52  {
53  }
55  {
56  if (&rhs != this)
57  stack_ = std::move(rhs.stack_);
58  return *this;
59  }
60 #endif
61 
62  void Put(Ch c)
63  {
64  *stack_.template Push<Ch>() = c;
65  }
66  void PutUnsafe(Ch c)
67  {
68  *stack_.template PushUnsafe<Ch>() = c;
69  }
70  void Flush()
71  {
72  }
73 
74  void Clear()
75  {
76  stack_.Clear();
77  }
78  void ShrinkToFit()
79  {
80  // Push and pop a null terminator. This is safe.
81  *stack_.template Push<Ch>() = '\0';
82  stack_.ShrinkToFit();
83  stack_.template Pop<Ch>(1);
84  }
85 
86  void Reserve(size_t count)
87  {
88  stack_.template Reserve<Ch>(count);
89  }
90  Ch* Push(size_t count)
91  {
92  return stack_.template Push<Ch>(count);
93  }
94  Ch* PushUnsafe(size_t count)
95  {
96  return stack_.template PushUnsafe<Ch>(count);
97  }
98  void Pop(size_t count)
99  {
100  stack_.template Pop<Ch>(count);
101  }
102 
103  const Ch* GetString() const
104  {
105  // Push and pop a null terminator. This is safe.
106  *stack_.template Push<Ch>() = '\0';
107  stack_.template Pop<Ch>(1);
108 
109  return stack_.template Bottom<Ch>();
110  }
111 
113  size_t GetSize() const
114  {
115  return stack_.GetSize();
116  }
117 
119  size_t GetLength() const
120  {
121  return stack_.GetSize() / sizeof(Ch);
122  }
123 
124  static const size_t kDefaultCapacity = 256;
126 
127 private:
128  // Prohibit copy constructor & assignment operator.
131 };
132 
135 
136 template <typename Encoding, typename Allocator>
138 {
139  stream.Reserve(count);
140 }
141 
142 template <typename Encoding, typename Allocator>
143 inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c)
144 {
145  stream.PutUnsafe(c);
146 }
147 
149 template <>
150 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n)
151 {
152  std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
153 }
154 
156 
157 #if defined(__clang__)
158 RAPIDJSON_DIAG_POP
159 #endif
160 
161 #endif // RAPIDJSON_STRINGBUFFER_H_
void PutReserve(GenericStringBuffer< Encoding, Allocator > &stream, size_t count)
Definition: stringbuffer.h:137
size_t GetSize() const
Get the size of string in bytes in the string buffer.
Definition: stringbuffer.h:113
GenericStringBuffer & operator=(const GenericStringBuffer &)
const Ch * GetString() const
Definition: stringbuffer.h:103
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
A type-unsafe stack for storing different types of data.
Definition: stack.h:37
void PutN(GenericStringBuffer< UTF8<> > &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: stringbuffer.h:150
static const size_t kDefaultCapacity
Definition: stringbuffer.h:124
GenericStringBuffer< UTF8<> > StringBuffer
String buffer with UTF8 encoding.
Definition: stringbuffer.h:134
Encoding::Ch Ch
Definition: stringbuffer.h:44
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
UTF-8 encoding.
Definition: encodings.h:96
Ch * Push(size_t count)
Definition: stringbuffer.h:90
void Reserve(size_t count)
Definition: stringbuffer.h:86
Ch * PushUnsafe(size_t count)
Definition: stringbuffer.h:94
Represents an in-memory output stream.
Definition: fwd.h:68
GenericStringBuffer(Allocator *allocator=0, size_t capacity=kDefaultCapacity)
Definition: stringbuffer.h:46
size_t GetLength() const
Get the length of string in Ch in the string buffer.
Definition: stringbuffer.h:119
internal::Stack< Allocator > stack_
Definition: stringbuffer.h:125
void Pop(size_t count)
Definition: stringbuffer.h:98
void PutUnsafe(Ch c)
Definition: stringbuffer.h:66


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:54