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>
43 : allocator_(allocator),
48 initialCapacity_(stackCapacity)
52 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
54 : allocator_(rhs.allocator_),
55 ownAllocator_(rhs.ownAllocator_),
57 stackTop_(rhs.stackTop_),
58 stackEnd_(rhs.stackEnd_),
59 initialCapacity_(rhs.initialCapacity_)
62 rhs.ownAllocator_ = 0;
66 rhs.initialCapacity_ = 0;
72 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
79 allocator_ = rhs.allocator_;
80 ownAllocator_ = rhs.ownAllocator_;
82 stackTop_ = rhs.stackTop_;
83 stackEnd_ = rhs.stackEnd_;
84 initialCapacity_ = rhs.initialCapacity_;
87 rhs.ownAllocator_ = 0;
91 rhs.initialCapacity_ = 0;
107 void Clear() { stackTop_ = stack_; }
125 template <
typename T>
126 RAPIDJSON_FORCEINLINE
void Reserve(
size_t count = 1)
130 (stackEnd_ - stackTop_)))
134 template <
typename T>
135 RAPIDJSON_FORCEINLINE T*
Push(
size_t count = 1)
138 return PushUnsafe<T>(count);
141 template <
typename T>
145 RAPIDJSON_ASSERT(
static_cast<std::ptrdiff_t
>(
sizeof(T) * count) <= (stackEnd_ - stackTop_));
146 T* ret =
reinterpret_cast<T*
>(stackTop_);
147 stackTop_ +=
sizeof(T) * count;
151 template <
typename T>
155 stackTop_ -= count *
sizeof(T);
156 return reinterpret_cast<T*
>(stackTop_);
159 template <
typename T>
163 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
166 template <
typename T>
170 return reinterpret_cast<T*
>(stackTop_ -
sizeof(T));
173 template <
typename T>
176 return reinterpret_cast<T*
>(stackTop_);
179 template <
typename T>
182 return reinterpret_cast<T*
>(stackTop_);
185 template <
typename T>
188 return reinterpret_cast<T*
>(stack_);
191 template <
typename T>
194 return reinterpret_cast<T*
>(stack_);
205 bool Empty()
const {
return stackTop_ == stack_; }
206 size_t GetSize()
const {
return static_cast<size_t>(stackTop_ - stack_); }
207 size_t GetCapacity()
const {
return static_cast<size_t>(stackEnd_ - stack_); }
210 template <
typename T>
211 void Expand(
size_t count)
220 newCapacity = initialCapacity_;
225 newCapacity += (newCapacity + 1) / 2;
227 size_t newSize =
GetSize() +
sizeof(T) * count;
228 if(newCapacity < newSize)
229 newCapacity = newSize;
234 void Resize(
size_t newCapacity)
237 stack_ =
static_cast<char*
>(allocator_->Realloc(stack_,
GetCapacity(), newCapacity));
238 stackTop_ = stack_ + size;
239 stackEnd_ = stack_ + newCapacity;
257 size_t initialCapacity_;
263 #if defined(__clang__)
void Free(A &a, T *p, size_t n=1)
Definition: allocators.h:485
A type-unsafe stack for storing different types of data.
Definition: stack.h:38
void Clear()
Definition: stack.h:107
T * Bottom()
Definition: stack.h:186
bool Empty() const
Definition: stack.h:205
void ShrinkToFit()
Definition: stack.h:109
T * End()
Definition: stack.h:174
void Swap(Stack &rhs) RAPIDJSON_NOEXCEPT
Definition: stack.h:97
const T * End() const
Definition: stack.h:180
RAPIDJSON_FORCEINLINE T * PushUnsafe(size_t count=1)
Definition: stack.h:142
bool HasAllocator() const
Definition: stack.h:197
size_t GetCapacity() const
Definition: stack.h:207
RAPIDJSON_FORCEINLINE void Reserve(size_t count=1)
Definition: stack.h:126
RAPIDJSON_FORCEINLINE T * Push(size_t count=1)
Definition: stack.h:135
T * Pop(size_t count)
Definition: stack.h:152
Allocator & GetAllocator()
Definition: stack.h:199
T * Top()
Definition: stack.h:160
const T * Top() const
Definition: stack.h:167
size_t GetSize() const
Definition: stack.h:206
Stack(Allocator *allocator, size_t stackCapacity)
Definition: stack.h:42
const T * Bottom() const
Definition: stack.h:192
~Stack()
Definition: stack.h:70
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:531
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Definition: allocators.h:459
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
Definition: swap.h:33
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:746
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:742