stringbuffer.h

Engine/source/persistence/rapidjson/stringbuffer.h

More...

Classes:

class

Represents an in-memory output stream.

Public Typedefs

StringBuffer 

String buffer with UTF8 encoding.

Public Functions

PutN(GenericStringBuffer< UTF8<> > & stream, char c, size_t n)

Implement specialized version of PutN() with memset() for better performance.

PutUnsafe(GenericStringBuffer< Encoding, Allocator > & stream, typename Encoding::Ch c)

Detailed Description

Public Typedefs

typedef GenericStringBuffer< UTF8<> > StringBuffer 

String buffer with UTF8 encoding.

Public Functions

PutN(GenericStringBuffer< UTF8<> > & stream, char c, size_t n)

Implement specialized version of PutN() with memset() for better performance.

PutReserve(GenericStringBuffer< Encoding, Allocator > & stream, size_t count)

PutUnsafe(GenericStringBuffer< Encoding, Allocator > & stream, typename Encoding::Ch c)

  1
  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
 33RAPIDJSON_NAMESPACE_BEGIN
 34
 35//! Represents an in-memory output stream.
 36/*!
 37    \tparam Encoding Encoding of the stream.
 38    \tparam Allocator type for allocating memory buffer.
 39    \note implements Stream concept
 40*/
 41template <typename Encoding, typename Allocator = CrtAllocator>
 42class GenericStringBuffer {
 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_)) {}
 50    GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
 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
 82    //! Get the size of string in bytes in the string buffer.
 83    size_t GetSize() const { return stack_.GetSize(); }
 84
 85    //! Get the length of string in Ch in the string buffer.
 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.
 93    GenericStringBuffer(const GenericStringBuffer&);
 94    GenericStringBuffer& operator=(const GenericStringBuffer&);
 95};
 96
 97//! String buffer with UTF8 encoding
 98typedef GenericStringBuffer<UTF8<> > StringBuffer;
 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
110//! Implement specialized version of PutN() with memset() for better performance.
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
116RAPIDJSON_NAMESPACE_END
117
118#if defined(__clang__)
119RAPIDJSON_DIAG_POP
120#endif
121
122#endif // RAPIDJSON_STRINGBUFFER_H_
123