Public Member Functions | Private Member Functions | Private Attributes | List of all members
internal::Stack< Allocator > Class Template Reference

A type-unsafe stack for storing different types of data. More...

#include <stack.h>

Public Member Functions

 Stack (Allocator *allocator, size_t stackCapacity)
 
 ~Stack ()
 
void Swap (Stack &rhs) RAPIDJSON_NOEXCEPT
 
void Clear ()
 
void ShrinkToFit ()
 
template<typename T >
RAPIDJSON_FORCEINLINE void Reserve (size_t count=1)
 
template<typename T >
RAPIDJSON_FORCEINLINE T * Push (size_t count=1)
 
template<typename T >
RAPIDJSON_FORCEINLINE T * PushUnsafe (size_t count=1)
 
template<typename T >
T * Pop (size_t count)
 
template<typename T >
T * Top ()
 
template<typename T >
const T * Top () const
 
template<typename T >
T * End ()
 
template<typename T >
const T * End () const
 
template<typename T >
T * Bottom ()
 
template<typename T >
const T * Bottom () const
 
bool HasAllocator () const
 
Allocator & GetAllocator ()
 
bool Empty () const
 
size_t GetSize () const
 
size_t GetCapacity () const
 

Private Member Functions

template<typename T >
void Expand (size_t count)
 
void Resize (size_t newCapacity)
 
void Destroy ()
 
 Stack (const Stack &)
 
Stackoperator= (const Stack &)
 

Private Attributes

Allocator * allocator_
 
Allocator * ownAllocator_
 
char * stack_
 
char * stackTop_
 
char * stackEnd_
 
size_t initialCapacity_
 

Detailed Description

template<typename Allocator>
class internal::Stack< Allocator >

A type-unsafe stack for storing different types of data.

Template Parameters
AllocatorAllocator for allocating stack memory.

Definition at line 36 of file stack.h.

Constructor & Destructor Documentation

template<typename Allocator>
internal::Stack< Allocator >::Stack ( Allocator *  allocator,
size_t  stackCapacity 
)
inline

Definition at line 40 of file stack.h.

40  : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
41  }
char * stack_
Definition: stack.h:218
Allocator * ownAllocator_
Definition: stack.h:217
char * stackEnd_
Definition: stack.h:220
size_t initialCapacity_
Definition: stack.h:221
Allocator * allocator_
Definition: stack.h:216
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
internal::Stack< Allocator >::~Stack ( )
inline

Definition at line 61 of file stack.h.

61  {
62  Destroy();
63  }
void Destroy()
Definition: stack.h:207
template<typename Allocator>
internal::Stack< Allocator >::Stack ( const Stack< Allocator > &  )
private

Member Function Documentation

template<typename Allocator>
template<typename T >
T* internal::Stack< Allocator >::Bottom ( )
inline

Definition at line 162 of file stack.h.

162 { return reinterpret_cast<T*>(stack_); }
char * stack_
Definition: stack.h:218
template<typename Allocator>
template<typename T >
const T* internal::Stack< Allocator >::Bottom ( ) const
inline

Definition at line 165 of file stack.h.

165 { return reinterpret_cast<T*>(stack_); }
char * stack_
Definition: stack.h:218
template<typename Allocator>
void internal::Stack< Allocator >::Clear ( )
inline

Definition at line 98 of file stack.h.

98 { stackTop_ = stack_; }
char * stack_
Definition: stack.h:218
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
void internal::Stack< Allocator >::Destroy ( )
inlineprivate

Definition at line 207 of file stack.h.

207  {
208  Allocator::Free(stack_);
209  RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
210  }
char * stack_
Definition: stack.h:218
Allocator * ownAllocator_
Definition: stack.h:217
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:605
template<typename Allocator>
bool internal::Stack< Allocator >::Empty ( ) const
inline

Definition at line 176 of file stack.h.

176 { return stackTop_ == stack_; }
char * stack_
Definition: stack.h:218
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
T* internal::Stack< Allocator >::End ( )
inline

Definition at line 156 of file stack.h.

156 { return reinterpret_cast<T*>(stackTop_); }
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
const T* internal::Stack< Allocator >::End ( ) const
inline

Definition at line 159 of file stack.h.

159 { return reinterpret_cast<T*>(stackTop_); }
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
void internal::Stack< Allocator >::Expand ( size_t  count)
inlineprivate

Definition at line 182 of file stack.h.

182  {
183  // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
184  size_t newCapacity;
185  if (stack_ == 0) {
186  if (!allocator_)
187  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
188  newCapacity = initialCapacity_;
189  } else {
190  newCapacity = GetCapacity();
191  newCapacity += (newCapacity + 1) / 2;
192  }
193  size_t newSize = GetSize() + sizeof(T) * count;
194  if (newCapacity < newSize)
195  newCapacity = newSize;
196 
197  Resize(newCapacity);
198  }
char * stack_
Definition: stack.h:218
Allocator * ownAllocator_
Definition: stack.h:217
void Resize(size_t newCapacity)
Definition: stack.h:200
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:601
size_t GetSize() const
Definition: stack.h:177
size_t initialCapacity_
Definition: stack.h:221
Allocator * allocator_
Definition: stack.h:216
size_t GetCapacity() const
Definition: stack.h:178
template<typename Allocator>
Allocator& internal::Stack< Allocator >::GetAllocator ( )
inline

Definition at line 171 of file stack.h.

171  {
173  return *allocator_;
174  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
Allocator * allocator_
Definition: stack.h:216
template<typename Allocator>
size_t internal::Stack< Allocator >::GetCapacity ( ) const
inline

Definition at line 178 of file stack.h.

178 { return static_cast<size_t>(stackEnd_ - stack_); }
char * stack_
Definition: stack.h:218
char * stackEnd_
Definition: stack.h:220
template<typename Allocator>
size_t internal::Stack< Allocator >::GetSize ( ) const
inline

Definition at line 177 of file stack.h.

177 { return static_cast<size_t>(stackTop_ - stack_); }
char * stack_
Definition: stack.h:218
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
bool internal::Stack< Allocator >::HasAllocator ( ) const
inline

Definition at line 167 of file stack.h.

167  {
168  return allocator_ != 0;
169  }
Allocator * allocator_
Definition: stack.h:216
template<typename Allocator>
Stack& internal::Stack< Allocator >::operator= ( const Stack< Allocator > &  )
private
template<typename Allocator>
template<typename T >
T* internal::Stack< Allocator >::Pop ( size_t  count)
inline

Definition at line 137 of file stack.h.

137  {
138  RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
139  stackTop_ -= count * sizeof(T);
140  return reinterpret_cast<T*>(stackTop_);
141  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
size_t GetSize() const
Definition: stack.h:177
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
RAPIDJSON_FORCEINLINE T* internal::Stack< Allocator >::Push ( size_t  count = 1)
inline

Definition at line 122 of file stack.h.

122  {
123  Reserve<T>(count);
124  return PushUnsafe<T>(count);
125  }
template<typename Allocator>
template<typename T >
RAPIDJSON_FORCEINLINE T* internal::Stack< Allocator >::PushUnsafe ( size_t  count = 1)
inline

Definition at line 128 of file stack.h.

128  {
130  RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_);
131  T* ret = reinterpret_cast<T*>(stackTop_);
132  stackTop_ += sizeof(T) * count;
133  return ret;
134  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
char * stackEnd_
Definition: stack.h:220
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
RAPIDJSON_FORCEINLINE void internal::Stack< Allocator >::Reserve ( size_t  count = 1)
inline

Definition at line 115 of file stack.h.

115  {
116  // Expand the stack if needed
117  if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_))
118  Expand<T>(count);
119  }
char * stackEnd_
Definition: stack.h:220
char * stackTop_
Definition: stack.h:219
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:476
template<typename Allocator>
void internal::Stack< Allocator >::Resize ( size_t  newCapacity)
inlineprivate

Definition at line 200 of file stack.h.

200  {
201  const size_t size = GetSize(); // Backup the current size
202  stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
203  stackTop_ = stack_ + size;
204  stackEnd_ = stack_ + newCapacity;
205  }
char * stack_
Definition: stack.h:218
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
char * stackEnd_
Definition: stack.h:220
size_t GetSize() const
Definition: stack.h:177
Allocator * allocator_
Definition: stack.h:216
char * stackTop_
Definition: stack.h:219
size_t GetCapacity() const
Definition: stack.h:178
template<typename Allocator>
void internal::Stack< Allocator >::ShrinkToFit ( )
inline

Definition at line 100 of file stack.h.

100  {
101  if (Empty()) {
102  // If the stack is empty, completely deallocate the memory.
103  Allocator::Free(stack_); // NOLINT (+clang-analyzer-unix.Malloc)
104  stack_ = 0;
105  stackTop_ = 0;
106  stackEnd_ = 0;
107  }
108  else
109  Resize(GetSize());
110  }
char * stack_
Definition: stack.h:218
void Resize(size_t newCapacity)
Definition: stack.h:200
bool Empty() const
Definition: stack.h:176
char * stackEnd_
Definition: stack.h:220
size_t GetSize() const
Definition: stack.h:177
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
void internal::Stack< Allocator >::Swap ( Stack< Allocator > &  rhs)
inline

Definition at line 89 of file stack.h.

89  {
90  internal::Swap(allocator_, rhs.allocator_);
91  internal::Swap(ownAllocator_, rhs.ownAllocator_);
92  internal::Swap(stack_, rhs.stack_);
93  internal::Swap(stackTop_, rhs.stackTop_);
94  internal::Swap(stackEnd_, rhs.stackEnd_);
95  internal::Swap(initialCapacity_, rhs.initialCapacity_);
96  }
char * stack_
Definition: stack.h:218
Allocator * ownAllocator_
Definition: stack.h:217
char * stackEnd_
Definition: stack.h:220
size_t initialCapacity_
Definition: stack.h:221
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
Definition: swap.h:33
Allocator * allocator_
Definition: stack.h:216
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
T* internal::Stack< Allocator >::Top ( )
inline

Definition at line 144 of file stack.h.

144  {
145  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
146  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
147  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
size_t GetSize() const
Definition: stack.h:177
char * stackTop_
Definition: stack.h:219
template<typename Allocator>
template<typename T >
const T* internal::Stack< Allocator >::Top ( ) const
inline

Definition at line 150 of file stack.h.

150  {
151  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
152  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
153  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
size_t GetSize() const
Definition: stack.h:177
char * stackTop_
Definition: stack.h:219

Member Data Documentation

template<typename Allocator>
Allocator* internal::Stack< Allocator >::allocator_
private

Definition at line 216 of file stack.h.

template<typename Allocator>
size_t internal::Stack< Allocator >::initialCapacity_
private

Definition at line 221 of file stack.h.

template<typename Allocator>
Allocator* internal::Stack< Allocator >::ownAllocator_
private

Definition at line 217 of file stack.h.

template<typename Allocator>
char* internal::Stack< Allocator >::stack_
private

Definition at line 218 of file stack.h.

template<typename Allocator>
char* internal::Stack< Allocator >::stackEnd_
private

Definition at line 220 of file stack.h.

template<typename Allocator>
char* internal::Stack< Allocator >::stackTop_
private

Definition at line 219 of file stack.h.


The documentation for this class was generated from the following file: