⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restack.h

📁 vsstylemanager1.0.4希望对大家有用啊,
💻 H
📖 第 1 页 / 共 2 页
字号:
            // allocate a new block and return a ptr to the new memory
            return grow( size );
        }

        return mem;
    }

    byte_t * unwind( byte_t * pb ) // throw()
    {
        // roll back the stack
        m_current = pb;

        // If we've unwound this whole block, then make the
        // previous node the current node
        if( m_current == m_begin )
        {
            // write the cached value of m_current into m_current_node
            m_current_node->m_head.m_current = m_current;
            m_current_node = m_current_node->m_head.m_back;

            // update the cache
            m_begin   = m_current_node->m_mem;
            m_current = m_current_node->m_head.m_current;
            m_end     = m_current_node->m_head.m_end;
        }

        return pb;
    }

    byte_t * unwind( size_t size ) // throw()
    {
        return unwind( m_current - size );
    }

    void long_jump_impl( void * jump_ptr, detail::bool2type<true> ) // throw()
    {
        safe_long_jump( jump_ptr );
    }

    void long_jump_impl( void * jump_ptr, detail::bool2type<false> ) // throw()
    {
        unsafe_long_jump( jump_ptr );
    }

    struct real_unwinder;
    friend struct real_unwinder;
    struct real_unwinder
    {
        real_unwinder( stack_type * pstack, size_t size ) // throw()
            : m_pstack(pstack), m_size(size) {}

        ~real_unwinder() // throw()
        {
            if( m_pstack )
                m_pstack->unwind( m_size );
        }

        void dismiss() // throw()
        {
            m_pstack = 0;
        }

    private:
        real_unwinder( real_unwinder const & );
        real_unwinder & operator=( real_unwinder const & );

        stack_type * m_pstack;
        size_t       m_size;
    };

    struct dummy_unwinder
    {
        dummy_unwinder( stack_type *, size_t ) {} // throw()
        void dismiss() {} // throw()
    };

    // Disallow these for now. Might implement them later.
    hetero_stack( hetero_stack const & );
    hetero_stack & operator=( hetero_stack const & );

public:

    class type_error : public std::logic_error
    {
        std::type_info const * m_prequested_type;
        std::type_info const * m_pactual_type;
    public:
        type_error
        (
            std::type_info const & requested_type,
            std::type_info const & actual_type,
            std::string const & s = "type error in hetero_stack"
        ) // throw()
            : std::logic_error( s + " (requested type: " + requested_type.name()
                + ", actual type: " + actual_type.name() + ")" )
            , m_prequested_type( &requested_type )
            , m_pactual_type( &actual_type )
        {
        }
        std::type_info const & requested_type() const // throw()
        {
            return *m_prequested_type;
        }
        std::type_info const & actual_type() const // throw()
        {
            return *m_pactual_type;
        }
    };

    hetero_stack() // throw()
        : m_current_node( &m_first_node.m_node )
    {
        m_first_node.m_node.m_head.m_back    = & m_first_node.m_node;
        m_first_node.m_node.m_head.m_next    = 0;
        m_begin = m_current = m_first_node.m_node.m_head.m_current = m_first_node.m_node.m_mem;
        m_end = m_first_node.m_node.m_head.m_end = m_first_node.m_buf + sizeof( m_first_node );
    }

    ~hetero_stack() // throw()
    {
        // AlignmentT must be a power of two
        detail::static_assert< detail::is_power_of_two<AlignmentT>::value > const align_test;

        // Call any destructors for objects still on the stack
        if( RuntimeTypeCheckT && ! AssumePodT )
        {
            long_jump( m_first_node.m_node.m_mem );
        }

        // delete all the memory blocks
        m_current_node = m_first_node.m_node.m_head.m_next;
        for( stack_node * next_node; m_current_node; m_current_node = next_node )
        {
            next_node = m_current_node->m_head.m_next;
            ::operator delete( static_cast<void*>( m_current_node ) );
        }
    }

    template< typename T >
    inline void push( T const & t ) // throw(std::bad_alloc,...)
    {
        // Make sure that the alignment for type T is not worse
        // than our declared alignment.
        detail::static_assert<( AlignmentT >= detail::alignof<T>::value )> const align_test;
        static_cast<void>(align_test);

        // If T won't throw in copy c'tor then we don't need to use an unwinder object.
        typedef typename detail::select< AssumePodT || detail::has_trivial_copy<T>::value,
            dummy_unwinder, real_unwinder >::type unwinder;

        // If this throws, it doesn't change state,
        // so there is nothing to roll back.
        byte_t * pb = allocate( aligned_sizeof<T>::with_rtti );

        // Rolls back the allocate if later steps throw
        // BUGBUG we can do the alloc, but not update m_current until after
        // the copy c'tor to avoid the need for an unwinder object
        unwinder guard( this, aligned_sizeof<T>::with_rtti );

        new ( pb ) T( t ); // Could throw if ! has_trivial_copy<T>::value

        // If we are debugging the stack, then push a pointer to the type_info
        // for this type T. It will be checked in pop().
        if( RuntimeTypeCheckT )
        {
            detail::to_type<vtable_ptr>( pb + aligned_sizeof<T>::no_rtti ) = & detail::type_info_ex<T,AlignmentT>::vtable;
        }

        // ok, everything succeeded -- dismiss the guard
        guard.dismiss();
    }

    template< typename T >
    inline void pop( T & t ) // throw(...)
    {
        detail::static_assert<( AlignmentT >= detail::alignof<T>::value )> const align_test;
        static_cast<void>(align_test);

        // If we are debugging the stack, then in push() we pushed a pointer
        // to the type_info struct for this type T.  Check it now.
        if( RuntimeTypeCheckT )
        {
            byte_t * pti = m_current - aligned_sizeof<vtable_ptr>::no_rtti;
            if( ! detail::type_info_ex<T,AlignmentT>::equals( detail::to_type<vtable_ptr>( pti ) ) )
                throw type_error( typeid( T ), *detail::to_type<vtable_ptr>( pti )->typeinfo_ptr );
        }

        // Don't change state yet because assignment op could throw!
        byte_t * pT = m_current - aligned_sizeof<T>::with_rtti;
        t = detail::to_type<T const>( pT ); // could throw
        T const & ref = detail::to_type<T const>( pT );
        regex::detail::destroy( &ref );
        unwind( pT );
    }

    // Call this version of pop when you don't need the popped value
    template< typename T >
    inline void pop( REGEX_VC6(detail::type2type<T> COMMA int) ) // throw(type_error,...)
    {
        detail::static_assert<( AlignmentT >= detail::alignof<T>::value )> const align_test;
        static_cast<void>(align_test);

        // If we are debugging the stack, then in push() we pushed a pointer
        // to the type_info struct for this type T.  Check it now.
        if( RuntimeTypeCheckT )
        {
            byte_t * pti = m_current - aligned_sizeof<vtable_ptr>::no_rtti;
            if( ! detail::type_info_ex<T,AlignmentT>::equals( detail::to_type<vtable_ptr>( pti ) ) )
                throw type_error( typeid( T ), *detail::to_type<vtable_ptr>( pti )->typeinfo_ptr );
        }

        byte_t * pv = unwind( aligned_sizeof<T>::with_rtti );
        T const & ref = detail::to_type<T const>( pv );
        regex::detail::destroy( &ref );
    }

    // Call this version of pop when you don't need the popped value and
    // throwing an exception isn't an option
    template< typename T >
    inline bool pop( std::nothrow_t const & ) // throw()
    {
        detail::static_assert<( AlignmentT >= detail::alignof<T>::value )> const align_test;
        static_cast<void>(align_test);

        // If we are debugging the stack, then in push() we pushed a pointer
        // to the type_info struct for this type T.  Check it now.
        if( RuntimeTypeCheckT )
        {
            byte_t * pti = m_current - aligned_sizeof<vtable_ptr>::no_rtti;
            if( ! detail::type_info_ex<T,AlignmentT>::equals( detail::to_type<vtable_ptr>( pti ) ) )
                return false; // type error, can't throw so bail.
        }

        byte_t * pv = unwind( aligned_sizeof<T>::with_rtti );
        T const & ref = detail::to_type<T const>( pv );
        regex::detail::destroy( &ref );
        return true;
    }

    template< typename T >
    inline T & top( REGEX_VC6(detail::type2type<T>) ) const // throw(type_error,...)
    {
        detail::static_assert<( AlignmentT >= detail::alignof<T>::value )> const align_test;
        static_cast<void>(align_test);

        if( RuntimeTypeCheckT )
        {
            // If we are debugging the stack, then the top of the stack is a
            // pointer to a type_info struct. Assert that we have the correct type.
            byte_t * pti = m_current - aligned_sizeof<vtable_ptr>::no_rtti;
            if( ! detail::type_info_ex<T,AlignmentT>::equals( detail::to_type<vtable_ptr>( pti ) ) )
                throw type_error( typeid( T ), *detail::to_type<vtable_ptr>( pti )->typeinfo_ptr );
        }

        byte_t * pT = m_current - aligned_sizeof<T>::with_rtti;
        return detail::to_type<T>( pT );
    }

    // Fetch the type_info for the element at the top of the stack
    std::type_info const & top_type() const // throw()
    {
        detail::static_assert< RuntimeTypeCheckT > const type_check;
        static_cast<void>(type_check);

        byte_t * pti = m_current - aligned_sizeof<vtable_ptr>::no_rtti;
        return *detail::to_type<vtable_ptr>( pti )->typeinfo_ptr;
    }

    // Get a pointer to the top of the stack
    void * set_jump() const // throw()
    {
        return m_current;
    }

    // Quick and dirty stack unwind. Does not call destructors.
    void unsafe_long_jump( void *const jump_ptr ) // throw()
    {
        for( ;; )
        {
            if( std::less<void*>()( jump_ptr, m_current_node->m_mem ) ||
                std::less<void*>()( m_current_node->m_head.m_end, jump_ptr ) )
            {
                m_current_node->m_head.m_current = m_current_node->m_mem;
                m_current_node = m_current_node->m_head.m_back;
            }
            else
            {
                m_begin   = m_current_node->m_mem;
                m_current = m_current_node->m_head.m_current = static_cast<byte_t*>( jump_ptr );
                m_end     = m_current_node->m_head.m_end;
                return;
            }
        }
    }

    // Safe long jump; does call destructors if RuntimeTypeCheckT is true.
    void safe_long_jump( void *const jump_ptr ) // throw()
    {
        detail::static_assert< RuntimeTypeCheckT > const type_check;
        static_cast<void>(type_check);

        while( m_current != jump_ptr )
        {
            // The top of the stack is a pointer to a type_vtable struct.
            m_current -= aligned_sizeof<vtable_ptr>::no_rtti;
            vtable_ptr pvtable = detail::to_type<vtable_ptr>( m_current );

            // find the start of the object
            m_current -= pvtable->aligned_size;

            // call the destructor for T
            if( pvtable->destroy )
            {
                pvtable->destroy( m_current );
            }

            // move to the previous buffer if necessary
            if( m_current == m_begin && m_current != jump_ptr )
            {
                m_current_node->m_head.m_current = m_current;
                m_current_node = m_current_node->m_head.m_back;
                m_begin   = m_current_node->m_mem;
                m_current = m_current_node->m_head.m_current;
                m_end     = m_current_node->m_head.m_end;
            }
        }
    }

    // Stack unwind. If RuntimeTypeCheckT && !AssumePodT, then destructors
    // are called.  Otherwise they are not.
    void long_jump( void * jump_ptr ) // throw()
    {
        long_jump_impl( jump_ptr, detail::bool2type<RuntimeTypeCheckT && !AssumePodT>() );
    }

    struct stack_guard
    {
        stack_type * m_ps;
        void       * m_jump_ptr;

        explicit stack_guard( stack_type * ps )
            : m_ps( ps )
            , m_jump_ptr( ps->set_jump() )
        {
        }
        ~stack_guard()
        {
            m_ps->long_jump( m_jump_ptr );
        }
    };

    bool empty() const // throw()
    {
        return m_current == m_first_node.m_node.m_mem;
    }

    // Use scoped_push for automatically pushing/popping
    // things to and from the stack. This is especially useful
    // if you want to push a bunch of things "atomically".  For
    // instance:
    //
    // typedef hetero_stack<>::scoped_pop scoped_pop;
    // scoped_pop p1 = stack.scoped_push( int(1) ); // could throw
    // scoped_pop p2 = stack.scoped_push( std::string("foo") ); // could throw
    // stack.push( float(3.14159) ); // could throw
    // p2.dismiss(); // ok, nothing threw, so ...
    // p1.dismiss(); //  ... dismiss the scoped_pops
    //
    // If p2 and p1 are not dismissed, as in the case when an
    // exception gets thrown, then they automatically pop their
    // arguments from the stack.

    class scoped_pop_base
    {
        scoped_pop_base & operator=( scoped_pop_base const & ); // disallow assignment
    protected:
        mutable stack_type * m_pstack;

        explicit scoped_pop_base( stack_type * pstack ) // throw(std::bad_alloc,...)
          : m_pstack( pstack )
        {
        }
        scoped_pop_base( scoped_pop_base const & right ) // throw() // destructive copy
          : m_pstack( right.m_pstack )
        {
            right.dismiss();
        }
    public:
        void dismiss() const // throw()
        {
            m_pstack = 0;
        }
    };

    template< typename T >
    class scoped_pop_t : public scoped_pop_base
    {
        scoped_pop_t & operator=( scoped_pop_t const & ); // disallow assignment
    public:
        scoped_pop_t( stack_type * pstack, T const & t ) // throw(std::bad_alloc,...)
          : scoped_pop_base( pstack )
        {
            // Note that if this throws an exception the destructor
            // will not get called, which is what we want.
            m_pstack->push( t );
        }
        ~scoped_pop_t() // throw()
        {
            // If we own this stack space, pop it.
            if( m_pstack )
                m_pstack->template pop<T>( std::nothrow );
        }
    };

    template< typename T >
    scoped_pop_t<T> scoped_push( T const & t ) // throw(...)
    {
        return scoped_pop_t<T>( this, t );
    }

    typedef scoped_pop_base const & scoped_pop;
};

#ifdef _MSC_VER
#pragma warning( pop )
#endif

} // namespace regex

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -