memorybuffer.h
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_MEMORYBUFFER_H_
22 #define RAPIDJSON_MEMORYBUFFER_H_
23 
24 #include "rapidjson.h"
25 #include "internal/stack.h"
26 
27 namespace rapidjson {
28 
29 //! Represents an in-memory output byte stream.
30 /*!
31  This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream.
32 
33  It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file.
34 
35  Differences between MemoryBuffer and StringBuffer:
36  1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer.
37  2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator.
38 
39  \tparam Allocator type for allocating memory buffer.
40  \note implements Stream concept
41 */
42 template <typename Allocator = CrtAllocator>
44  typedef char Ch; // byte
45 
46  GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
47 
48  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
49  void Flush() {}
50 
51  void Clear() { stack_.Clear(); }
52  void ShrinkToFit() { stack_.ShrinkToFit(); }
53  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
54  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
55 
56  const Ch* GetBuffer() const {
57  return stack_.template Bottom<Ch>();
58  }
59 
60  size_t GetSize() const { return stack_.GetSize(); }
61 
62  static const size_t kDefaultCapacity = 256;
63  mutable internal::Stack<Allocator> stack_;
64 };
65 
67 
68 //! Implement specialized version of PutN() with memset() for better performance.
69 template<>
70 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
71  std::memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
72 }
73 
74 } // namespace rapidjson
75 
76 #endif // RAPIDJSON_MEMORYBUFFER_H_
Represents an in-memory output byte stream.
Definition: memorybuffer.h:43
main RapidJSON namespace
Definition: rapidjson.h:241
common definitions and configuration
Concept for allocating, resizing and freeing memory block.
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:484