All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stringbuffer.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_STRINGBUFFER_H_
22 #define RAPIDJSON_STRINGBUFFER_H_
23 
24 #include "rapidjson.h"
25 #include "internal/stack.h"
26 
27 namespace rapidjson {
28 
29 //! Represents an in-memory output stream.
30 /*!
31  \tparam Encoding Encoding of the stream.
32  \tparam Allocator type for allocating memory buffer.
33  \note implements Stream concept
34 */
35 template <typename Encoding, typename Allocator = CrtAllocator>
37  typedef typename Encoding::Ch Ch;
38 
39  GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
40 
41  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
42  void Flush() {}
43 
44  void Clear() { stack_.Clear(); }
45  void ShrinkToFit() {
46  // Push and pop a null terminator. This is safe.
47  *stack_.template Push<Ch>() = '\0';
48  stack_.ShrinkToFit();
49  stack_.template Pop<Ch>(1);
50  }
51  Ch* Push(size_t count) { return stack_.template Push<Ch>(count); }
52  void Pop(size_t count) { stack_.template Pop<Ch>(count); }
53 
54  const Ch* GetString() const {
55  // Push and pop a null terminator. This is safe.
56  *stack_.template Push<Ch>() = '\0';
57  stack_.template Pop<Ch>(1);
58 
59  return stack_.template Bottom<Ch>();
60  }
61 
62  size_t GetSize() const { return stack_.GetSize(); }
63 
64  static const size_t kDefaultCapacity = 256;
65  mutable internal::Stack<Allocator> stack_;
66 };
67 
68 //! String buffer with UTF8 encoding
70 
71 //! Implement specialized version of PutN() with memset() for better performance.
72 template<>
73 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
74  std::memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
75 }
76 
77 } // namespace rapidjson
78 
79 #endif // RAPIDJSON_STRINGBUFFER_H_