DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers memorybuffer.h Source File

memorybuffer.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_MEMORYBUFFER_H_
00016 #define RAPIDJSON_MEMORYBUFFER_H_
00017 
00018 #include "stream.h"
00019 #include "internal/stack.h"
00020 
00021 RAPIDJSON_NAMESPACE_BEGIN
00022 
00023 //! Represents an in-memory output byte stream.
00024 /*!
00025     This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream.
00026 
00027     It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file.
00028 
00029     Differences between MemoryBuffer and StringBuffer:
00030     1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. 
00031     2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator.
00032 
00033     \tparam Allocator type for allocating memory buffer.
00034     \note implements Stream concept
00035 */
00036 template <typename Allocator = CrtAllocator>
00037 struct GenericMemoryBuffer {
00038     typedef char Ch; // byte
00039 
00040     GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
00041 
00042     void Put(Ch c) { *stack_.template Push<Ch>() = c; }
00043     void Flush() {}
00044 
00045     void Clear() { stack_.Clear(); }
00046     void ShrinkToFit() { stack_.ShrinkToFit(); }
00047     Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
00048     void Pop(size_t count) { stack_.template Pop<Ch>(count); }
00049 
00050     const Ch* GetBuffer() const {
00051         return stack_.template Bottom<Ch>();
00052     }
00053 
00054     size_t GetSize() const { return stack_.GetSize(); }
00055 
00056     static const size_t kDefaultCapacity = 256;
00057     mutable internal::Stack<Allocator> stack_;
00058 };
00059 
00060 typedef GenericMemoryBuffer<> MemoryBuffer;
00061 
00062 //! Implement specialized version of PutN() with memset() for better performance.
00063 template<>
00064 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
00065     std::memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
00066 }
00067 
00068 RAPIDJSON_NAMESPACE_END
00069 
00070 #endif // RAPIDJSON_MEMORYBUFFER_H_