Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:55:07

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_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 //! Represents an in-memory output stream.
0035 /*!
0036     \tparam Encoding Encoding of the stream.
0037     \tparam Allocator type for allocating memory buffer.
0038     \note implements Stream concept
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         // Push and pop a null terminator. This is safe.
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         // Push and pop a null terminator. This is safe.
0075         *stack_.template Push<Ch>() = '\0';
0076         stack_.template Pop<Ch>(1);
0077 
0078         return stack_.template Bottom<Ch>();
0079     }
0080 
0081     //! Get the size of string in bytes in the string buffer.
0082     size_t GetSize() const { return stack_.GetSize(); }
0083 
0084     //! Get the length of string in Ch in the string buffer.
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     // Prohibit copy constructor & assignment operator.
0092     GenericStringBuffer(const GenericStringBuffer&);
0093     GenericStringBuffer& operator=(const GenericStringBuffer&);
0094 };
0095 
0096 //! String buffer with UTF8 encoding
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 //! Implement specialized version of PutN() with memset() for better performance.
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 // RAPIDJSON_STRINGBUFFER_H_