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


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46