Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:13:02

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 // 
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed 
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
0013 // specific language governing permissions and limitations under the License.
0014 
0015 #ifndef RAPIDJSON_MEMORYBUFFER_H_
0016 #define RAPIDJSON_MEMORYBUFFER_H_
0017 
0018 #include "stream.h"
0019 #include "internal/stack.h"
0020 
0021 RAPIDJSON_NAMESPACE_BEGIN
0022 
0023 //! Represents an in-memory output byte stream.
0024 /*!
0025     This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream.
0026 
0027     It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file.
0028 
0029     Differences between MemoryBuffer and StringBuffer:
0030     1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. 
0031     2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator.
0032 
0033     \tparam Allocator type for allocating memory buffer.
0034     \note implements Stream concept
0035 */
0036 template <typename Allocator = CrtAllocator>
0037 struct GenericMemoryBuffer {
0038     typedef char Ch; // byte
0039 
0040     GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
0041 
0042     void Put(Ch c) { *stack_.template Push<Ch>() = c; }
0043     void Flush() {}
0044 
0045     void Clear() { stack_.Clear(); }
0046     void ShrinkToFit() { stack_.ShrinkToFit(); }
0047     Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
0048     void Pop(size_t count) { stack_.template Pop<Ch>(count); }
0049 
0050     const Ch* GetBuffer() const {
0051         return stack_.template Bottom<Ch>();
0052     }
0053 
0054     size_t GetSize() const { return stack_.GetSize(); }
0055 
0056     static const size_t kDefaultCapacity = 256;
0057     mutable internal::Stack<Allocator> stack_;
0058 };
0059 
0060 typedef GenericMemoryBuffer<> MemoryBuffer;
0061 
0062 //! Implement specialized version of PutN() with memset() for better performance.
0063 template<>
0064 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
0065     std::memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
0066 }
0067 
0068 RAPIDJSON_NAMESPACE_END
0069 
0070 #endif // RAPIDJSON_MEMORYBUFFER_H_