15 #ifndef RAPIDJSON_INTERNAL_STACK_H_ 16 #define RAPIDJSON_INTERNAL_STACK_H_ 18 #include "../rapidjson.h" 20 RAPIDJSON_NAMESPACE_BEGIN
29 template <
typename Allocator>
34 Stack(Allocator* allocator,
size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
38 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 40 : allocator_(rhs.allocator_),
41 ownAllocator_(rhs.ownAllocator_),
43 stackTop_(rhs.stackTop_),
44 stackEnd_(rhs.stackEnd_),
45 initialCapacity_(rhs.initialCapacity_)
48 rhs.ownAllocator_ = 0;
52 rhs.initialCapacity_ = 0;
60 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS 61 Stack& operator=(Stack&& rhs) {
66 allocator_ = rhs.allocator_;
67 ownAllocator_ = rhs.ownAllocator_;
69 stackTop_ = rhs.stackTop_;
70 stackEnd_ = rhs.stackEnd_;
71 initialCapacity_ = rhs.initialCapacity_;
74 rhs.ownAllocator_ = 0;
78 rhs.initialCapacity_ = 0;
84 void Clear() { stackTop_ = stack_; }
89 Allocator::Free(stack_);
101 RAPIDJSON_FORCEINLINE T* Push(
size_t count = 1) {
103 if (stackTop_ +
sizeof(T) * count >= stackEnd_)
106 T* ret =
reinterpret_cast<T*
>(stackTop_);
107 stackTop_ +=
sizeof(T) * count;
112 T* Pop(
size_t count) {
114 stackTop_ -= count *
sizeof(T);
115 return reinterpret_cast<T*
>(stackTop_);
121 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
125 T* Bottom() {
return (T*)stack_; }
127 Allocator& GetAllocator() {
return *allocator_; }
128 bool Empty()
const {
return stackTop_ == stack_; }
129 size_t GetSize()
const {
return static_cast<size_t>(stackTop_ - stack_); }
130 size_t GetCapacity()
const {
return static_cast<size_t>(stackEnd_ - stack_); }
134 void Expand(
size_t count) {
140 newCapacity = initialCapacity_;
142 newCapacity = GetCapacity();
143 newCapacity += (newCapacity + 1) / 2;
145 size_t newSize = GetSize() +
sizeof(T) * count;
146 if (newCapacity < newSize)
147 newCapacity = newSize;
152 void Resize(
size_t newCapacity) {
153 const size_t size = GetSize();
154 stack_ = (
char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
155 stackTop_ = stack_ + size;
156 stackEnd_ = stack_ + newCapacity;
160 Allocator::Free(stack_);
166 Stack& operator=(
const Stack&);
168 Allocator* allocator_;
169 Allocator* ownAllocator_;
173 size_t initialCapacity_;
177 RAPIDJSON_NAMESPACE_END
179 #endif // RAPIDJSON_STACK_H_ #define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344