webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
memorybuffer.h
Go to the documentation of this file.
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 
28 
30 
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;
64 };
65 
67 
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 
75 
76 #endif // RAPIDJSON_MEMORYBUFFER_H_
GLint GLsizei count
Definition: gl2.h:421
size_t GetSize() const
Definition: memorybuffer.h:60
Ch * Push(size_t count)
Definition: memorybuffer.h:53
int c
Definition: cpp_unittests.cpp:275
char Ch
Definition: memorybuffer.h:44
void Clear()
Definition: memorybuffer.h:51
void PutN(MemoryBuffer &memoryBuffer, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: memorybuffer.h:70
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
void Flush()
Definition: memorybuffer.h:49
EGLStreamKHR EGLint n
Definition: eglext.h:984
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
Represents an in-memory output byte stream.
Definition: memorybuffer.h:43
A type-unsafe stack for storing different types of data.
Definition: stack.h:34
const Ch * GetBuffer() const
Definition: memorybuffer.h:56
internal::Stack< Allocator > stack_
Definition: memorybuffer.h:63
GenericMemoryBuffer MemoryBuffer
Definition: memorybuffer.h:66
common definitions and configuration
static const size_t kDefaultCapacity
Definition: memorybuffer.h:62
GenericMemoryBuffer(Allocator *allocator=0, size_t capacity=kDefaultCapacity)
Definition: memorybuffer.h:46
void Pop(size_t count)
Definition: memorybuffer.h:54
void Put(Ch c)
Definition: memorybuffer.h:48
void ShrinkToFit()
Definition: memorybuffer.h:52