rapidjson
A fast JSON parser/generator for C++ with both SAX/DOM style API
 All Classes Functions Variables Typedefs 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  \implements Stream
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 
23  void Clear() { stack_.Clear(); }
24 
25  const char* GetString() const {
26  // Push and pop a null terminator. This is safe.
27  *stack_.template Push<Ch>() = '\0';
28  stack_.template Pop<Ch>(1);
29 
30  return stack_.template Bottom<Ch>();
31  }
32 
33  size_t Size() const { return stack_.GetSize(); }
34 
35  static const size_t kDefaultCapacity = 256;
36  mutable internal::Stack<Allocator> stack_;
37 };
38 
40 
41 //! Implement specialized version of PutN() with memset() for better performance.
42 template<>
43 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
44  memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
45 }
46 
47 } // namespace rapidjson
48 
49 #endif // RAPIDJSON_STRINGBUFFER_H_