15 #ifndef RAPIDJSON_INTERNAL_STACK_H_
16 #define RAPIDJSON_INTERNAL_STACK_H_
18 #include "../allocators.h"
22 #if defined(__clang__)
24 RAPIDJSON_DIAG_OFF(c++98-compat)
36 template <
typename Allocator>
41 Stack(
Allocator* allocator,
size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
44 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
46 : allocator_(rhs.allocator_),
47 ownAllocator_(rhs.ownAllocator_),
49 stackTop_(rhs.stackTop_),
50 stackEnd_(rhs.stackEnd_),
51 initialCapacity_(rhs.initialCapacity_)
54 rhs.ownAllocator_ = 0;
58 rhs.initialCapacity_ = 0;
66 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
72 allocator_ = rhs.allocator_;
73 ownAllocator_ = rhs.ownAllocator_;
75 stackTop_ = rhs.stackTop_;
76 stackEnd_ = rhs.stackEnd_;
77 initialCapacity_ = rhs.initialCapacity_;
80 rhs.ownAllocator_ = 0;
84 rhs.initialCapacity_ = 0;
90 void Swap(
Stack& rhs) RAPIDJSON_NOEXCEPT {
91 internal::Swap(allocator_, rhs.allocator_);
92 internal::Swap(ownAllocator_, rhs.ownAllocator_);
93 internal::Swap(stack_, rhs.stack_);
94 internal::Swap(stackTop_, rhs.stackTop_);
95 internal::Swap(stackEnd_, rhs.stackEnd_);
96 internal::Swap(initialCapacity_, rhs.initialCapacity_);
99 void Clear() { stackTop_ = stack_; }
104 Allocator::Free(stack_);
116 RAPIDJSON_FORCEINLINE
void Reserve(
size_t count = 1) {
121 if (
RAPIDJSON_UNLIKELY(
static_cast<std::ptrdiff_t
>(
sizeof(T) * count) > (stackEnd_ - stackTop_)))
126 RAPIDJSON_FORCEINLINE T* Push(
size_t count = 1) {
128 return PushUnsafe<T>(count);
132 RAPIDJSON_FORCEINLINE T* PushUnsafe(
size_t count = 1) {
133 RAPIDJSON_ASSERT(
static_cast<std::ptrdiff_t
>(
sizeof(T) * count) <= (stackEnd_ - stackTop_));
134 T* ret =
reinterpret_cast<T*
>(stackTop_);
135 stackTop_ +=
sizeof(T) * count;
140 T* Pop(
size_t count) {
142 stackTop_ -= count *
sizeof(T);
143 return reinterpret_cast<T*
>(stackTop_);
149 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
153 const T* Top()
const {
155 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
159 T* End() {
return reinterpret_cast<T*
>(stackTop_); }
162 const T* End()
const {
return reinterpret_cast<T*
>(stackTop_); }
165 T* Bottom() {
return reinterpret_cast<T*
>(stack_); }
168 const T* Bottom()
const {
return reinterpret_cast<T*
>(stack_); }
170 bool HasAllocator()
const {
171 return allocator_ != 0;
179 bool Empty()
const {
return stackTop_ == stack_; }
180 size_t GetSize()
const {
return static_cast<size_t>(stackTop_ - stack_); }
181 size_t GetCapacity()
const {
return static_cast<size_t>(stackEnd_ - stack_); }
185 void Expand(
size_t count) {
191 newCapacity = initialCapacity_;
193 newCapacity = GetCapacity();
194 newCapacity += (newCapacity + 1) / 2;
196 size_t newSize = GetSize() +
sizeof(T) * count;
197 if (newCapacity < newSize)
198 newCapacity = newSize;
203 void Resize(
size_t newCapacity) {
204 const size_t size = GetSize();
205 stack_ =
static_cast<char*
>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
206 stackTop_ = stack_ + size;
207 stackEnd_ = stack_ + newCapacity;
211 Allocator::Free(stack_);
224 size_t initialCapacity_;
230 #if defined(__clang__)
A type-unsafe stack for storing different types of data.
Definition: stack.h:37
Concept for allocating, resizing and freeing memory block.
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:468
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:590
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:586