All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
rapidjson::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 ()
 
AllocatorGetAllocator ()
 
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

Allocatorallocator_
 
AllocatorownAllocator_
 
char * stack_
 
char * stackTop_
 
char * stackEnd_
 
size_t initialCapacity_
 

Detailed Description

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

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

Template Parameters
AllocatorAllocator for allocating stack memory.

Definition at line 34 of file stack.h.

Constructor & Destructor Documentation

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

Definition at line 38 of file stack.h.

38  : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
39  RAPIDJSON_ASSERT(stackCapacity > 0);
40  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Allocator * ownAllocator_
Definition: stack.h:173
Allocator * allocator_
Definition: stack.h:172
template<typename Allocator>
rapidjson::internal::Stack< Allocator >::~Stack ( )
inline

Definition at line 60 of file stack.h.

60  {
61  Destroy();
62  }
template<typename Allocator>
rapidjson::internal::Stack< Allocator >::Stack ( const Stack< Allocator > &  )
private

Member Function Documentation

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

Definition at line 129 of file stack.h.

template<typename Allocator>
void rapidjson::internal::Stack< Allocator >::Clear ( )
inline

Definition at line 88 of file stack.h.

template<typename Allocator>
void rapidjson::internal::Stack< Allocator >::Destroy ( )
inlineprivate

Definition at line 163 of file stack.h.

163  {
164  Allocator::Free(stack_);
165  RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
166  }
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:412
Allocator * ownAllocator_
Definition: stack.h:173
template<typename Allocator>
bool rapidjson::internal::Stack< Allocator >::Empty ( ) const
inline

Definition at line 132 of file stack.h.

132 { return stackTop_ == stack_; }
template<typename Allocator>
template<typename T >
void rapidjson::internal::Stack< Allocator >::Expand ( size_t  count)
inlineprivate

Definition at line 138 of file stack.h.

138  {
139  // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
140  size_t newCapacity;
141  if (stack_ == 0) {
142  if (!allocator_)
143  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
144  newCapacity = initialCapacity_;
145  } else {
146  newCapacity = GetCapacity();
147  newCapacity += (newCapacity + 1) / 2;
148  }
149  size_t newSize = GetSize() + sizeof(T) * count;
150  if (newCapacity < newSize)
151  newCapacity = newSize;
152 
153  Resize(newCapacity);
154  }
void Resize(size_t newCapacity)
Definition: stack.h:156
size_t GetCapacity() const
Definition: stack.h:134
size_t GetSize() const
Definition: stack.h:133
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:408
Allocator * ownAllocator_
Definition: stack.h:173
Allocator * allocator_
Definition: stack.h:172
template<typename Allocator>
Allocator& rapidjson::internal::Stack< Allocator >::GetAllocator ( )
inline

Definition at line 131 of file stack.h.

131 { return *allocator_; }
Allocator * allocator_
Definition: stack.h:172
template<typename Allocator>
size_t rapidjson::internal::Stack< Allocator >::GetCapacity ( ) const
inline

Definition at line 134 of file stack.h.

134 { return static_cast<size_t>(stackEnd_ - stack_); }
template<typename Allocator>
size_t rapidjson::internal::Stack< Allocator >::GetSize ( ) const
inline

Definition at line 133 of file stack.h.

133 { return static_cast<size_t>(stackTop_ - stack_); }
template<typename Allocator>
Stack& rapidjson::internal::Stack< Allocator >::operator= ( const Stack< Allocator > &  )
private
template<typename Allocator>
template<typename T >
T* rapidjson::internal::Stack< Allocator >::Pop ( size_t  count)
inline

Definition at line 116 of file stack.h.

116  {
117  RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
118  stackTop_ -= count * sizeof(T);
119  return reinterpret_cast<T*>(stackTop_);
120  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
size_t GetSize() const
Definition: stack.h:133
template<typename Allocator>
template<typename T >
RAPIDJSON_FORCEINLINE T* rapidjson::internal::Stack< Allocator >::Push ( size_t  count = 1)
inline

Definition at line 105 of file stack.h.

105  {
106  // Expand the stack if needed
107  if (stackTop_ + sizeof(T) * count >= stackEnd_)
108  Expand<T>(count);
109 
110  T* ret = reinterpret_cast<T*>(stackTop_);
111  stackTop_ += sizeof(T) * count;
112  return ret;
113  }
template<typename Allocator>
void rapidjson::internal::Stack< Allocator >::Resize ( size_t  newCapacity)
inlineprivate

Definition at line 156 of file stack.h.

156  {
157  const size_t size = GetSize(); // Backup the current size
158  stack_ = (char*)allocator_->Realloc(stack_, GetCapacity(), newCapacity);
159  stackTop_ = stack_ + size;
160  stackEnd_ = stack_ + newCapacity;
161  }
size_t GetCapacity() const
Definition: stack.h:134
size_t GetSize() const
Definition: stack.h:133
Allocator * allocator_
Definition: stack.h:172
template<typename Allocator>
void rapidjson::internal::Stack< Allocator >::ShrinkToFit ( )
inline

Definition at line 90 of file stack.h.

90  {
91  if (Empty()) {
92  // If the stack is empty, completely deallocate the memory.
93  Allocator::Free(stack_);
94  stack_ = 0;
95  stackTop_ = 0;
96  stackEnd_ = 0;
97  }
98  else
99  Resize(GetSize());
100  }
void Resize(size_t newCapacity)
Definition: stack.h:156
size_t GetSize() const
Definition: stack.h:133
bool Empty() const
Definition: stack.h:132
template<typename Allocator>
template<typename T >
T* rapidjson::internal::Stack< Allocator >::Top ( )
inline

Definition at line 123 of file stack.h.

123  {
124  RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
125  return reinterpret_cast<T*>(stackTop_ - sizeof(T));
126  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
size_t GetSize() const
Definition: stack.h:133

Member Data Documentation

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

Definition at line 172 of file stack.h.

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

Definition at line 177 of file stack.h.

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

Definition at line 173 of file stack.h.

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

Definition at line 174 of file stack.h.

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

Definition at line 176 of file stack.h.

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

Definition at line 175 of file stack.h.


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