All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
memorybuffer.h
1 #ifndef RAPIDJSON_MEMORYBUFFER_H_
2 #define RAPIDJSON_MEMORYBUFFER_H_
3 
4 #include "rapidjson.h"
5 #include "internal/stack.h"
6 
7 namespace rapidjson {
8 
9 //! Represents an in-memory output byte stream.
10 /*!
11  This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream.
12 
13  It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file.
14 
15  Differences between MemoryBuffer and StringBuffer:
16  1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer.
17  2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator.
18 
19  \tparam Allocator type for allocating memory buffer.
20  \note implements Stream concept
21 */
22 template <typename Allocator = CrtAllocator>
24  typedef char Ch; // byte
25 
26  GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
27 
28  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
29  void Flush() {}
30 
31  void Clear() { stack_.Clear(); }
32  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
33  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
34 
35  const Ch* GetBuffer() const {
36  return stack_.template Bottom<Ch>();
37  }
38 
39  size_t GetSize() const { return stack_.GetSize(); }
40 
41  static const size_t kDefaultCapacity = 256;
42  mutable internal::Stack<Allocator> stack_;
43 };
44 
46 
47 //! Implement specialized version of PutN() with memset() for better performance.
48 template<>
49 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
50  memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
51 }
52 
53 } // namespace rapidjson
54 
55 #endif // RAPIDJSON_MEMORYBUFFER_H_