memorybuffer.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_MEMORYBUFFER_H_
16 #define RAPIDJSON_MEMORYBUFFER_H_
17 
18 #include "stream.h"
19 #include "internal/stack.h"
20 
22 
24 
37 template <typename Allocator = CrtAllocator>
39 {
40  typedef char Ch; // byte
41 
42  GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity)
43  {
44  }
45 
46  void Put(Ch c)
47  {
48  *stack_.template Push<Ch>() = c;
49  }
50  void Flush()
51  {
52  }
53 
54  void Clear()
55  {
56  stack_.Clear();
57  }
58  void ShrinkToFit()
59  {
60  stack_.ShrinkToFit();
61  }
62  Ch* Push(size_t count)
63  {
64  return stack_.template Push<Ch>(count);
65  }
66  void Pop(size_t count)
67  {
68  stack_.template Pop<Ch>(count);
69  }
70 
71  const Ch* GetBuffer() const
72  {
73  return stack_.template Bottom<Ch>();
74  }
75 
76  size_t GetSize() const
77  {
78  return stack_.GetSize();
79  }
80 
81  static const size_t kDefaultCapacity = 256;
83 };
84 
86 
88 template <>
89 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n)
90 {
91  std::memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
92 }
93 
95 
96 #endif // RAPIDJSON_MEMORYBUFFER_H_
static const size_t kDefaultCapacity
Definition: memorybuffer.h:81
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
Represents an in-memory output byte stream.
Definition: fwd.h:83
A type-unsafe stack for storing different types of data.
Definition: stack.h:37
size_t GetSize() const
Definition: memorybuffer.h:76
GenericMemoryBuffer(Allocator *allocator=0, size_t capacity=kDefaultCapacity)
Definition: memorybuffer.h:42
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
void Pop(size_t count)
Definition: memorybuffer.h:66
Ch * Push(size_t count)
Definition: memorybuffer.h:62
const Ch * GetBuffer() const
Definition: memorybuffer.h:71
internal::Stack< Allocator > stack_
Definition: memorybuffer.h:82
void PutN(MemoryBuffer &memoryBuffer, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: memorybuffer.h:89
GenericMemoryBuffer MemoryBuffer
Definition: memorybuffer.h:85


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