TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 Clear ()
 
void ShrinkToFit ()
 
template<typename T >
RAPIDJSON_FORCEINLINE T * Push (size_t count=1)
 
template<typename T >
T * Pop (size_t count)
 
template<typename T >
T * Top ()
 
template<typename T >
T * Bottom ()
 
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.

Constructor & Destructor Documentation

template<typename Allocator>
internal::Stack< Allocator >::Stack ( Allocator *  allocator,
size_t  stackCapacity 
)
inline
34  : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
35  RAPIDJSON_ASSERT(stackCapacity > 0);
36  }
char * stack_
Definition: stack.h:170
Allocator * ownAllocator_
Definition: stack.h:169
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
char * stackEnd_
Definition: stack.h:172
size_t initialCapacity_
Definition: stack.h:173
Allocator * allocator_
Definition: stack.h:168
char * stackTop_
Definition: stack.h:171
template<typename Allocator>
internal::Stack< Allocator >::~Stack ( )
inline
56  {
57  Destroy();
58  }
void Destroy()
Definition: stack.h:159
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
125 { return (T*)stack_; }
char * stack_
Definition: stack.h:170
template<typename Allocator>
void internal::Stack< Allocator >::Clear ( )
inline
84 { stackTop_ = stack_; }
char * stack_
Definition: stack.h:170
char * stackTop_
Definition: stack.h:171

+ Here is the caller graph for this function:

template<typename Allocator>
void internal::Stack< Allocator >::Destroy ( )
inlineprivate
159  {
160  Allocator::Free(stack_);
161  RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
162  }
char * stack_
Definition: stack.h:170
Allocator * ownAllocator_
Definition: stack.h:169
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484

+ Here is the caller graph for this function:

template<typename Allocator>
bool internal::Stack< Allocator >::Empty ( ) const
inline
128 { return stackTop_ == stack_; }
char * stack_
Definition: stack.h:170
char * stackTop_
Definition: stack.h:171

+ Here is the caller graph for this function:

template<typename Allocator>
template<typename T >
void internal::Stack< Allocator >::Expand ( size_t  count)
inlineprivate
134  {
135  // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
136  size_t newCapacity;
137  if (stack_ == 0) {
138  if (!allocator_)
139  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
140  newCapacity = initialCapacity_;
141  } else {
142  newCapacity = GetCapacity();
143  newCapacity += (newCapacity + 1) / 2;
144  }
145  size_t newSize = GetSize() + sizeof(T) * count;
146  if (newCapacity < newSize)
147  newCapacity = newSize;
148 
149  Resize(newCapacity);
150  }
char * stack_
Definition: stack.h:170
Allocator * ownAllocator_
Definition: stack.h:169
void Resize(size_t newCapacity)
Definition: stack.h:152
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
size_t GetSize() const
Definition: stack.h:129
size_t initialCapacity_
Definition: stack.h:173
Allocator * allocator_
Definition: stack.h:168
size_t GetCapacity() const
Definition: stack.h:130
template<typename Allocator>
Allocator& internal::Stack< Allocator >::GetAllocator ( )
inline
127 { return *allocator_; }
Allocator * allocator_
Definition: stack.h:168

+ Here is the caller graph for this function:

template<typename Allocator>
size_t internal::Stack< Allocator >::GetCapacity ( ) const
inline
130 { return static_cast<size_t>(stackEnd_ - stack_); }
char * stack_
Definition: stack.h:170
char * stackEnd_
Definition: stack.h:172

+ Here is the caller graph for this function:

template<typename Allocator>
size_t internal::Stack< Allocator >::GetSize ( ) const
inline
129 { return static_cast<size_t>(stackTop_ - stack_); }
char * stack_
Definition: stack.h:170
char * stackTop_
Definition: stack.h:171

+ Here is the caller graph for this function:

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
112  {
113  RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
114  stackTop_ -= count * sizeof(T);
115  return reinterpret_cast<T*>(stackTop_);
116  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
size_t GetSize() const
Definition: stack.h:129
char * stackTop_
Definition: stack.h:171
template<typename Allocator>
template<typename T >
RAPIDJSON_FORCEINLINE T* internal::Stack< Allocator >::Push ( size_t  count = 1)
inline
101  {
102  // Expand the stack if needed
103  if (stackTop_ + sizeof(T) * count >= stackEnd_)
104  Expand<T>(count);
105 
106  T* ret = reinterpret_cast<T*>(stackTop_);
107  stackTop_ += sizeof(T) * count;
108  return ret;
109  }
char * stackEnd_
Definition: stack.h:172
char * stackTop_
Definition: stack.h:171
template<typename Allocator>
void internal::Stack< Allocator >::Resize ( size_t  newCapacity)
inlineprivate
152  {
153  const size_t size = GetSize(); // Backup the current size
154  stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
155  stackTop_ = stack_ + size;
156  stackEnd_ = stack_ + newCapacity;
157  }
char * stack_
Definition: stack.h:170
char * stackEnd_
Definition: stack.h:172
size_t GetSize() const
Definition: stack.h:129
Allocator * allocator_
Definition: stack.h:168
char * stackTop_
Definition: stack.h:171
size_t GetCapacity() const
Definition: stack.h:130

+ Here is the caller graph for this function:

template<typename Allocator>
void internal::Stack< Allocator >::ShrinkToFit ( )
inline
86  {
87  if (Empty()) {
88  // If the stack is empty, completely deallocate the memory.
89  Allocator::Free(stack_);
90  stack_ = 0;
91  stackTop_ = 0;
92  stackEnd_ = 0;
93  }
94  else
95  Resize(GetSize());
96  }
char * stack_
Definition: stack.h:170
void Resize(size_t newCapacity)
Definition: stack.h:152
bool Empty() const
Definition: stack.h:128
char * stackEnd_
Definition: stack.h:172
size_t GetSize() const
Definition: stack.h:129
char * stackTop_
Definition: stack.h:171

+ Here is the caller graph for this function:

template<typename Allocator>
template<typename T >
T* internal::Stack< Allocator >::Top ( )
inline
119  {
120  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
121  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
122  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
size_t GetSize() const
Definition: stack.h:129
char * stackTop_
Definition: stack.h:171

Member Data Documentation

template<typename Allocator>
Allocator* internal::Stack< Allocator >::allocator_
private
template<typename Allocator>
size_t internal::Stack< Allocator >::initialCapacity_
private
template<typename Allocator>
Allocator* internal::Stack< Allocator >::ownAllocator_
private
template<typename Allocator>
char* internal::Stack< Allocator >::stack_
private
template<typename Allocator>
char* internal::Stack< Allocator >::stackEnd_
private
template<typename Allocator>
char* internal::Stack< Allocator >::stackTop_
private

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