#ifndef RAPIDJSON_INTERNAL_STACK_H_#define RAPIDJSON_INTERNAL_STACK_H_namespacerapidjson{namespaceinternal{///////////////////////////////////////////////////////////////////////////////// Stack//! A type-unsafe stack for storing different types of data./*! \tparam Allocator Allocator for allocating stack memory.*/template<typenameAllocator>classStack{public:Stack(Allocator*allocator,size_tstack_capacity):allocator_(allocator),own_allocator_(0),stack_(0),stack_top_(0),stack_end_(0),stack_capacity_(stack_capacity){RAPIDJSON_ASSERT(stack_capacity_>0);if(!allocator_)own_allocator_=allocator_=newAllocator();stack_top_=stack_=(char*)allocator_->Malloc(stack_capacity_);stack_end_=stack_+stack_capacity_;}~Stack(){Allocator::Free(stack_);deleteown_allocator_;// Only delete if it is owned by the stack}voidClear(){/*stack_top_ = 0;*/stack_top_=stack_;}template<typenameT>T*Push(size_tcount=1){// Expand the stack if neededif(stack_top_+sizeof(T)*count>=stack_end_){size_tnew_capacity=stack_capacity_*2;size_tsize=GetSize();size_tnew_size=GetSize()+sizeof(T)*count;if(new_capacity<new_size)new_capacity=new_size;stack_=(char*)allocator_->Realloc(stack_,stack_capacity_,new_capacity);stack_capacity_=new_capacity;stack_top_=stack_+size;stack_end_=stack_+stack_capacity_;}T*ret=(T*)stack_top_;stack_top_+=sizeof(T)*count;returnret;}template<typenameT>T*Pop(size_tcount){RAPIDJSON_ASSERT(GetSize()>=count*sizeof(T));stack_top_-=count*sizeof(T);return(T*)stack_top_;}template<typenameT>T*Top(){RAPIDJSON_ASSERT(GetSize()>=sizeof(T));return(T*)(stack_top_-sizeof(T));}template<typenameT>T*Bottom(){return(T*)stack_;}Allocator&GetAllocator(){return*allocator_;}size_tGetSize()const{returnstack_top_-stack_;}size_tGetCapacity()const{returnstack_capacity_;}private:Allocator*allocator_;Allocator*own_allocator_;char*stack_;char*stack_top_;char*stack_end_;size_tstack_capacity_;};}// namespace internal}// namespace rapidjson#endif // RAPIDJSON_STACK_H_