Home | History | Annotate | Download | only in rapidjson
      1 // Tencent is pleased to support the open source community by making RapidJSON available.
      2 //
      3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
      4 //
      5 // Licensed under the MIT License (the "License"); you may not use this file except
      6 // in compliance with the License. You may obtain a copy of the License at
      7 //
      8 // http://opensource.org/licenses/MIT
      9 //
     10 // Unless required by applicable law or agreed to in writing, software distributed
     11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
     13 // specific language governing permissions and limitations under the License.
     14 
     15 #ifndef RAPIDJSON_STRINGBUFFER_H_
     16 #define RAPIDJSON_STRINGBUFFER_H_
     17 
     18 #include "rapidjson.h"
     19 
     20 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
     21 #include <utility> // std::move
     22 #endif
     23 
     24 #include "internal/stack.h"
     25 
     26 RAPIDJSON_NAMESPACE_BEGIN
     27 
     28 //! Represents an in-memory output stream.
     29 /*!
     30     \tparam Encoding Encoding of the stream.
     31     \tparam Allocator type for allocating memory buffer.
     32     \note implements Stream concept
     33 */
     34 template <typename Encoding, typename Allocator = CrtAllocator>
     35 class GenericStringBuffer {
     36 public:
     37     typedef typename Encoding::Ch Ch;
     38 
     39     explicit GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
     40 
     41 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
     42     GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {}
     43     GenericStringBuffer& operator=(GenericStringBuffer&& rhs) {
     44         if (&rhs != this)
     45             stack_ = std::move(rhs.stack_);
     46         return *this;
     47     }
     48 #endif
     49 
     50     void Put(Ch c) { *stack_.template Push<Ch>() = c; }
     51     void Flush() {}
     52 
     53     void Clear() { stack_.Clear(); }
     54     void ShrinkToFit() {
     55         // Push and pop a null terminator. This is safe.
     56         *stack_.template Push<Ch>() = '\0';
     57         stack_.ShrinkToFit();
     58         stack_.template Pop<Ch>(1);
     59     }
     60     Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
     61     void Pop(size_t count) { stack_.template Pop<Ch>(count); }
     62 
     63     const Ch* GetString() const {
     64         // Push and pop a null terminator. This is safe.
     65         *stack_.template Push<Ch>() = '\0';
     66         stack_.template Pop<Ch>(1);
     67 
     68         return stack_.template Bottom<Ch>();
     69     }
     70 
     71     size_t GetSize() const { return stack_.GetSize(); }
     72 
     73     static const size_t kDefaultCapacity = 256;
     74     mutable internal::Stack<Allocator> stack_;
     75 
     76 private:
     77     // Prohibit copy constructor & assignment operator.
     78     GenericStringBuffer(const GenericStringBuffer&);
     79     GenericStringBuffer& operator=(const GenericStringBuffer&);
     80 };
     81 
     82 //! String buffer with UTF8 encoding
     83 typedef GenericStringBuffer<UTF8<> > StringBuffer;
     84 
     85 //! Implement specialized version of PutN() with memset() for better performance.
     86 template<>
     87 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
     88     std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
     89 }
     90 
     91 RAPIDJSON_NAMESPACE_END
     92 
     93 #endif // RAPIDJSON_STRINGBUFFER_H_
     94