42 Stack(
Allocator* allocator,
size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
45#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
47 : allocator_(rhs.allocator_),
48 ownAllocator_(rhs.ownAllocator_),
50 stackTop_(rhs.stackTop_),
51 stackEnd_(rhs.stackEnd_),
52 initialCapacity_(rhs.initialCapacity_)
55 rhs.ownAllocator_ = 0;
59 rhs.initialCapacity_ = 0;
67#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
73 allocator_ = rhs.allocator_;
74 ownAllocator_ = rhs.ownAllocator_;
76 stackTop_ = rhs.stackTop_;
77 stackEnd_ = rhs.stackEnd_;
78 initialCapacity_ = rhs.initialCapacity_;
81 rhs.ownAllocator_ = 0;
85 rhs.initialCapacity_ = 0;
91 void Swap(
Stack& rhs) RAPIDJSON_NOEXCEPT {
92 internal::Swap(allocator_, rhs.allocator_);
93 internal::Swap(ownAllocator_, rhs.ownAllocator_);
94 internal::Swap(stack_, rhs.stack_);
95 internal::Swap(stackTop_, rhs.stackTop_);
96 internal::Swap(stackEnd_, rhs.stackEnd_);
97 internal::Swap(initialCapacity_, rhs.initialCapacity_);
100 void Clear() { stackTop_ = stack_; }
105 Allocator::Free(stack_);
117 RAPIDJSON_FORCEINLINE
void Reserve(
size_t count = 1) {
119 if (
RAPIDJSON_UNLIKELY(
static_cast<std::ptrdiff_t
>(
sizeof(T) * count) > (stackEnd_ - stackTop_)))
124 RAPIDJSON_FORCEINLINE T* Push(
size_t count = 1) {
126 return PushUnsafe<T>(count);
130 RAPIDJSON_FORCEINLINE T* PushUnsafe(
size_t count = 1) {
132 RAPIDJSON_ASSERT(
static_cast<std::ptrdiff_t
>(
sizeof(T) * count) <= (stackEnd_ - stackTop_));
133 T* ret =
reinterpret_cast<T*
>(stackTop_);
134 stackTop_ +=
sizeof(T) * count;
139 T* Pop(
size_t count) {
141 stackTop_ -= count *
sizeof(T);
142 return reinterpret_cast<T*
>(stackTop_);
148 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
152 const T* Top()
const {
154 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
158 T* End() {
return reinterpret_cast<T*
>(stackTop_); }
161 const T* End()
const {
return reinterpret_cast<T*
>(stackTop_); }
164 T* Bottom() {
return reinterpret_cast<T*
>(stack_); }
167 const T* Bottom()
const {
return reinterpret_cast<T*
>(stack_); }
169 bool HasAllocator()
const {
170 return allocator_ != 0;
178 bool Empty()
const {
return stackTop_ == stack_; }
179 size_t GetSize()
const {
return static_cast<size_t>(stackTop_ - stack_); }
180 size_t GetCapacity()
const {
return static_cast<size_t>(stackEnd_ - stack_); }
184 void Expand(
size_t count) {
190 newCapacity = initialCapacity_;
192 newCapacity = GetCapacity();
193 newCapacity += (newCapacity + 1) / 2;
195 size_t newSize = GetSize() +
sizeof(T) * count;
196 if (newCapacity < newSize)
197 newCapacity = newSize;
202 void Resize(
size_t newCapacity) {
203 const size_t size = GetSize();
204 stack_ =
static_cast<char*
>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
205 stackTop_ = stack_ + size;
206 stackEnd_ = stack_ + newCapacity;
210 Allocator::Free(stack_);
223 size_t initialCapacity_;