File indexing completed on 2024-11-15 09:55:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef RAPIDJSON_STRINGBUFFER_H_
0016 #define RAPIDJSON_STRINGBUFFER_H_
0017
0018 #include "stream.h"
0019 #include "internal/stack.h"
0020
0021 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
0022 #include <utility> // std::move
0023 #endif
0024
0025 #include "internal/stack.h"
0026
0027 #if defined(__clang__)
0028 RAPIDJSON_DIAG_PUSH
0029 RAPIDJSON_DIAG_OFF(c++98-compat)
0030 #endif
0031
0032 RAPIDJSON_NAMESPACE_BEGIN
0033
0034
0035
0036
0037
0038
0039
0040 template <typename Encoding, typename Allocator = CrtAllocator>
0041 class GenericStringBuffer {
0042 public:
0043 typedef typename Encoding::Ch Ch;
0044
0045 GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
0046
0047 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
0048 GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {}
0049 GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
0050 if (&rhs != this)
0051 stack_ = std::move(rhs.stack_);
0052 return *this;
0053 }
0054 #endif
0055
0056 void Put(Ch c) { *stack_.template Push<Ch>() = c; }
0057 void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; }
0058 void Flush() {}
0059
0060 void Clear() { stack_.Clear(); }
0061 void ShrinkToFit() {
0062
0063 *stack_.template Push<Ch>() = '\0';
0064 stack_.ShrinkToFit();
0065 stack_.template Pop<Ch>(1);
0066 }
0067
0068 void Reserve(size_t count) { stack_.template Reserve<Ch>(count); }
0069 Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
0070 Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); }
0071 void Pop(size_t count) { stack_.template Pop<Ch>(count); }
0072
0073 const Ch* GetString() const {
0074
0075 *stack_.template Push<Ch>() = '\0';
0076 stack_.template Pop<Ch>(1);
0077
0078 return stack_.template Bottom<Ch>();
0079 }
0080
0081
0082 size_t GetSize() const { return stack_.GetSize(); }
0083
0084
0085 size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); }
0086
0087 static const size_t kDefaultCapacity = 256;
0088 mutable internal::Stack<Allocator> stack_;
0089
0090 private:
0091
0092 GenericStringBuffer(const GenericStringBuffer&);
0093 GenericStringBuffer& operator=(const GenericStringBuffer&);
0094 };
0095
0096
0097 typedef GenericStringBuffer<UTF8<> > StringBuffer;
0098
0099 template<typename Encoding, typename Allocator>
0100 inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) {
0101 stream.Reserve(count);
0102 }
0103
0104 template<typename Encoding, typename Allocator>
0105 inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) {
0106 stream.PutUnsafe(c);
0107 }
0108
0109
0110 template<>
0111 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
0112 std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
0113 }
0114
0115 RAPIDJSON_NAMESPACE_END
0116
0117 #if defined(__clang__)
0118 RAPIDJSON_DIAG_POP
0119 #endif
0120
0121 #endif