stringbuffer.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef RAPIDJSON_STRINGBUFFER_H_
27 #define RAPIDJSON_STRINGBUFFER_H_
28 
29 #include "rapidjson.h"
30 #include "internal/stack.h"
31 
32 namespace rapidjson {
33 
35 
40 template <typename Encoding, typename Allocator = CrtAllocator>
42  typedef typename Encoding::Ch Ch;
43 
44  GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {}
45 
46  void Put(Ch c) { *stack_.template Push<Ch>() = c; }
47 
48  void Clear() { stack_.Clear(); }
49 
50  const char* GetString() const {
51  // Push and pop a null terminator. This is safe.
52  *stack_.template Push<Ch>() = '\0';
53  stack_.template Pop<Ch>(1);
54 
55  return stack_.template Bottom<Ch>();
56  }
57 
58  size_t Size() const { return stack_.GetSize(); }
59 
60  static const size_t kDefaultCapacity = 256;
62 };
63 
65 
67 template<>
68 inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {
69  memset(stream.stack_.Push<char>(n), c, n * sizeof(c));
70 }
71 
72 } // namespace rapidjson
73 
74 #endif // RAPIDJSON_STRINGBUFFER_H_
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
Concept for allocating, resizing and freeing memory block.
A type-unsafe stack for storing different types of data.
Definition: stack.h:39
Definition: document.h:38
GenericStringBuffer< UTF8<> > StringBuffer
Definition: stringbuffer.h:64
void PutN(GenericWriteStream &stream, char c, size_t n)
Definition: genericstream.h:113
Represents an in-memory output stream.
Definition: stringbuffer.h:41
internal::Stack< Allocator > stack_
Definition: stringbuffer.h:61
size_t Size() const
Definition: stringbuffer.h:58
const char * GetString() const
Definition: stringbuffer.h:50
Encoding::Ch Ch
Definition: stringbuffer.h:42
void Put(Ch c)
Definition: stringbuffer.h:46
static const size_t kDefaultCapacity
Definition: stringbuffer.h:60
void Clear()
Definition: stringbuffer.h:48
GenericStringBuffer(Allocator *allocator=0, size_t capacity=kDefaultCapacity)
Definition: stringbuffer.h:44
UTF-8 encoding.
Definition: rapidjson.h:360