SimulationCraft
SimulationCraft is a tool to explore combat mechanics in the popular MMO RPG World of Warcraft (tm).
stringbuffer.h
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.
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 #include <string_view>
28 
29 #if defined(__clang__)
30 RAPIDJSON_DIAG_PUSH
31 RAPIDJSON_DIAG_OFF(c++98-compat)
32 #endif
33 
35 
37 
42 template <typename Encoding, typename Allocator = CrtAllocator>
43 class GenericStringBuffer {
44 public:
45  typedef typename Encoding::Ch Ch;
46 
47  GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
48 
49 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
50  GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {}
51  GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
52  if (&rhs != this)
53  stack_ = std::move(rhs.stack_);
54  return *this;
55  }
56 #endif
57 
58  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
59  void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
60  void Flush() {}
61 
62  void Clear() { stack_.Clear(); }
63  void ShrinkToFit() {
64  // Push and pop a null terminator. This is safe.
65  *stack_.template Push<Ch>() = '\0';
66  stack_.ShrinkToFit();
67  stack_.template Pop<Ch>(1);
68  }
69 
70  void Reserve(size_t count) { stack_.template Reserve<Ch>(count); }
71  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
72  Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); }
73  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
74 
75  const Ch* GetString() const {
76  // Push and pop a null terminator. This is safe.
77  *stack_.template Push<Ch>() = '\0';
78  stack_.template Pop<Ch>(1);
79 
80  return stack_.template Bottom<Ch>();
81  }
82 
83  std::basic_string_view<Ch> GetStringView() const
84  {
85  return std::string_view{ stack_.template Bottom<Ch>(), GetLength() };
86  }
87 
89  size_t GetSize() const { return stack_.GetSize(); }
90 
92  size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); }
93 
94  static const size_t kDefaultCapacity = 256;
95  mutable internal::Stack<Allocator> stack_;
96 
97 private:
98  // Prohibit copy constructor & assignment operator.
100  GenericStringBuffer& operator=(const GenericStringBuffer&);
101 };
102 
105 
106 template<typename Encoding, typename Allocator>
107 inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) {
108  stream.Reserve(count);
109 }
110 
111 template<typename Encoding, typename Allocator>
112 inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) {
113  stream.PutUnsafe(c);
114 }
115 
117 template<>
118 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
119  std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
120 }
121 
123 
124 #if defined(__clang__)
125 RAPIDJSON_DIAG_POP
126 #endif
127 
128 #endif // RAPIDJSON_STRINGBUFFER_H_
Represents an in-memory output stream.
Definition: fwd.h:59
size_t GetSize() const
Get the size of string in bytes in the string buffer.
Definition: stringbuffer.h:89
Definition: span.hpp:609
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
size_t GetLength() const
Get the length of string in Ch in the string buffer.
Definition: stringbuffer.h:92
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
A type-unsafe stack for storing different types of data.
Definition: stack.h:37
UTF-8 encoding.
Definition: encodings.h:96