All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
53  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
54 
55  const Ch* GetBuffer() const {
56  return stack_.template Bottom<Ch>();
57  }
58 
59  size_t GetSize() const { return stack_.GetSize(); }
60 
61  static const size_t kDefaultCapacity = 256;
62  mutable internal::Stack<Allocator> stack_;
63 };
64 
66 
67 //! Implement specialized version of PutN() with memset() for better performance.
68 template<>
69 inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) {
70  memset(memoryBuffer.stack_.Push<char>(n), c, n * sizeof(c));
71 }
72 
73 } // namespace rapidjson
74 
75 #endif // RAPIDJSON_MEMORYBUFFER_H_
Represents an in-memory output byte stream.
Definition: memorybuffer.h:43
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:91
common definitions and configuration
Concept for allocating, resizing and freeing memory block.