DeepCover Embedded Security in IoT: Public-key Secured Data Paths
Dependencies: MaximInterface
stringbuffer.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_STRINGBUFFER_H_ 00016 #define RAPIDJSON_STRINGBUFFER_H_ 00017 00018 #include "stream.h" 00019 #include "internal/stack.h" 00020 00021 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 00022 #include <utility> // std::move 00023 #endif 00024 00025 #include "internal/stack.h" 00026 00027 #if defined(__clang__) 00028 RAPIDJSON_DIAG_PUSH 00029 RAPIDJSON_DIAG_OFF(c++98-compat) 00030 #endif 00031 00032 RAPIDJSON_NAMESPACE_BEGIN 00033 00034 //! Represents an in-memory output stream. 00035 /*! 00036 \tparam Encoding Encoding of the stream. 00037 \tparam Allocator type for allocating memory buffer. 00038 \note implements Stream concept 00039 */ 00040 template <typename Encoding, typename Allocator = CrtAllocator> 00041 class GenericStringBuffer { 00042 public: 00043 typedef typename Encoding::Ch Ch; 00044 00045 GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} 00046 00047 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 00048 GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} 00049 GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { 00050 if (&rhs != this) 00051 stack_ = std::move(rhs.stack_); 00052 return *this; 00053 } 00054 #endif 00055 00056 void Put(Ch c) { *stack_.template Push<Ch>() = c; } 00057 void PutUnsafe(Ch c) { *stack_.template PushUnsafe<Ch>() = c; } 00058 void Flush() {} 00059 00060 void Clear() { stack_.Clear(); } 00061 void ShrinkToFit() { 00062 // Push and pop a null terminator. This is safe. 00063 *stack_.template Push<Ch>() = '\0'; 00064 stack_.ShrinkToFit(); 00065 stack_.template Pop<Ch>(1); 00066 } 00067 00068 void Reserve(size_t count) { stack_.template Reserve<Ch>(count); } 00069 Ch* Push(size_t count) { return stack_.template Push<Ch>(count); } 00070 Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe<Ch>(count); } 00071 void Pop(size_t count) { stack_.template Pop<Ch>(count); } 00072 00073 const Ch* GetString() const { 00074 // Push and pop a null terminator. This is safe. 00075 *stack_.template Push<Ch>() = '\0'; 00076 stack_.template Pop<Ch>(1); 00077 00078 return stack_.template Bottom<Ch>(); 00079 } 00080 00081 //! Get the size of string in bytes in the string buffer. 00082 size_t GetSize() const { return stack_.GetSize(); } 00083 00084 //! Get the length of string in Ch in the string buffer. 00085 size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); } 00086 00087 static const size_t kDefaultCapacity = 256; 00088 mutable internal::Stack<Allocator> stack_; 00089 00090 private: 00091 // Prohibit copy constructor & assignment operator. 00092 GenericStringBuffer(const GenericStringBuffer&); 00093 GenericStringBuffer& operator=(const GenericStringBuffer&); 00094 }; 00095 00096 //! String buffer with UTF8 encoding 00097 typedef GenericStringBuffer<UTF8<> > StringBuffer; 00098 00099 template<typename Encoding, typename Allocator> 00100 inline void PutReserve(GenericStringBuffer<Encoding, Allocator>& stream, size_t count) { 00101 stream.Reserve(count); 00102 } 00103 00104 template<typename Encoding, typename Allocator> 00105 inline void PutUnsafe(GenericStringBuffer<Encoding, Allocator>& stream, typename Encoding::Ch c) { 00106 stream.PutUnsafe(c); 00107 } 00108 00109 //! Implement specialized version of PutN() with memset() for better performance. 00110 template<> 00111 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) { 00112 std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c)); 00113 } 00114 00115 RAPIDJSON_NAMESPACE_END 00116 00117 #if defined(__clang__) 00118 RAPIDJSON_DIAG_POP 00119 #endif 00120 00121 #endif // RAPIDJSON_STRINGBUFFER_H_
Generated on Tue Jul 12 2022 12:06:49 by 1.7.2