All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stringbuffer.h
1 #ifndef RAPIDJSON_STRINGBUFFER_H_
2 #define RAPIDJSON_STRINGBUFFER_H_
3 
4 #include "rapidjson.h"
5 #include "internal/stack.h"
6 
7 namespace rapidjson {
8 
9 //! Represents an in-memory output stream.
10 /*!
11  \tparam Encoding Encoding of the stream.
12  \tparam Allocator type for allocating memory buffer.
13  \note implements Stream concept
14 */
15 template <typename Encoding, typename Allocator = CrtAllocator>
17  typedef typename Encoding::Ch Ch;
18 
19  GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
20 
21  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
22  void Flush() {}
23 
24  void Clear() { stack_.Clear(); }
25  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
26  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
27 
28  const Ch* GetString() const {
29  // Push and pop a null terminator. This is safe.
30  *stack_.template Push<Ch>() = '\0';
31  stack_.template Pop<Ch>(1);
32 
33  return stack_.template Bottom<Ch>();
34  }
35 
36  size_t GetSize() const { return stack_.GetSize(); }
37 
38  static const size_t kDefaultCapacity = 256;
39  mutable internal::Stack<Allocator> stack_;
40 };
41 
42 //! String buffer with UTF8 encoding
44 
45 //! Implement specialized version of PutN() with memset() for better performance.
46 template<>
47 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
48  memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
49 }
50 
51 } // namespace rapidjson
52 
53 #endif // RAPIDJSON_STRINGBUFFER_H_