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