15 #ifndef RAPIDJSON_INTERNAL_STACK_H_
16 #define RAPIDJSON_INTERNAL_STACK_H_
18 #include "../rapidjson.h"
21 RAPIDJSON_NAMESPACE_BEGIN
30 template <
typename Allocator>
35 Stack(Allocator* allocator,
size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
39 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
41 : allocator_(rhs.allocator_),
42 ownAllocator_(rhs.ownAllocator_),
44 stackTop_(rhs.stackTop_),
45 stackEnd_(rhs.stackEnd_),
46 initialCapacity_(rhs.initialCapacity_)
49 rhs.ownAllocator_ = 0;
53 rhs.initialCapacity_ = 0;
61 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
62 Stack& operator=(Stack&& rhs) {
67 allocator_ = rhs.allocator_;
68 ownAllocator_ = rhs.ownAllocator_;
70 stackTop_ = rhs.stackTop_;
71 stackEnd_ = rhs.stackEnd_;
72 initialCapacity_ = rhs.initialCapacity_;
75 rhs.ownAllocator_ = 0;
79 rhs.initialCapacity_ = 0;
85 void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT {
86 internal::Swap(allocator_, rhs.allocator_);
87 internal::Swap(ownAllocator_, rhs.ownAllocator_);
88 internal::Swap(stack_, rhs.stack_);
89 internal::Swap(stackTop_, rhs.stackTop_);
90 internal::Swap(stackEnd_, rhs.stackEnd_);
91 internal::Swap(initialCapacity_, rhs.initialCapacity_);
94 void Clear() { stackTop_ = stack_; }
99 Allocator::Free(stack_);
111 RAPIDJSON_FORCEINLINE T* Push(
size_t count = 1) {
113 if (stackTop_ +
sizeof(T) * count >= stackEnd_)
116 T* ret =
reinterpret_cast<T*
>(stackTop_);
117 stackTop_ +=
sizeof(T) * count;
122 T* Pop(
size_t count) {
124 stackTop_ -= count *
sizeof(T);
125 return reinterpret_cast<T*
>(stackTop_);
131 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
135 T* Bottom() {
return (T*)stack_; }
137 Allocator& GetAllocator() {
return *allocator_; }
138 bool Empty()
const {
return stackTop_ == stack_; }
139 size_t GetSize()
const {
return static_cast<size_t>(stackTop_ - stack_); }
140 size_t GetCapacity()
const {
return static_cast<size_t>(stackEnd_ - stack_); }
144 void Expand(
size_t count) {
150 newCapacity = initialCapacity_;
152 newCapacity = GetCapacity();
153 newCapacity += (newCapacity + 1) / 2;
155 size_t newSize = GetSize() +
sizeof(T) * count;
156 if (newCapacity < newSize)
157 newCapacity = newSize;
162 void Resize(
size_t newCapacity) {
163 const size_t size = GetSize();
164 stack_ = (
char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
165 stackTop_ = stack_ + size;
166 stackEnd_ = stack_ + newCapacity;
170 Allocator::Free(stack_);
176 Stack& operator=(
const Stack&);
178 Allocator* allocator_;
179 Allocator* ownAllocator_;
183 size_t initialCapacity_;
187 RAPIDJSON_NAMESPACE_END
189 #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