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

📄 vmstack.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Implemenation for the class that can stack VMVariant

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision:   $ : $Date:   $";
/*****************************************************************************/


#include "../../../stdafx.h"
#include "../VMException.h"
#include "../VMCoreGlobal.h"
#include "VMStack.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  DeepCopy

       DESCRIPTION:  performs an elemental copy of another stack

             INPUT:  roOther - the stack to copy
            OUTPUT:  none

           RETURNS:  none
*/
void VMDataStack::DeepCopy( VMDataStack& roOther )
{
  if ( roOther.GetCount() == 0 )
  {
    m_pxHead = NULL;
    m_xCount = 0;
    return;
  }

  m_pxHead = (PNODE) new NODE;
   
  if ( NULL == m_pxHead )
  {
    VMException  oError( __FILE__, 
                         __LINE__, 
                         ALLOCATION_ERROR, 
                         VMException::fatal );
    throw oError;
  }

  PNODE pxSource = roOther.m_pxHead;
  PNODE pxDest   = m_pxHead;

  while ( true )
  {
    pxDest->m_oData.CloneValue( pxSource->m_oData );
    ++m_xCount;

    if ( NULL != pxSource->m_pxNext )
    {
      pxDest->m_pxNext = (PNODE) new NODE;
         
      if ( NULL == pxDest->m_pxNext )
      {
        VMException  oError( __FILE__, 
                             __LINE__, 
                             ALLOCATION_ERROR, 
                             VMException::fatal );
        throw oError;
      }

      pxDest = pxDest->m_pxNext;
      pxSource = pxSource->m_pxNext;
    }
    else
    {
      pxDest->m_pxNext = NULL;
      break;
    }
  }
}
/* End of function "VMDataStack::DeepCopy"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  Clear

       DESCRIPTION:  Pops all stack elements

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
void VMDataStack::Clear( void )
{
  VMVariant xIgnored;
  while ( m_xCount )
  {
    Pop( xIgnored );
  }
}
/* End of function "VMDataStack::Clear"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMDataStack

       DESCRIPTION:  ctor.

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMDataStack::VMDataStack( void )
{
  m_xCount = 0;
  m_pxHead = NULL;
}
/* End of function "VMDataStack::VMDataStack"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMDataStack

       DESCRIPTION:  Copy ctor

             INPUT:  roOther - the stack to copy
            OUTPUT:  none

           RETURNS:  none
*/
VMDataStack::VMDataStack( VMDataStack& roOther )
{
  m_xCount = 0;
  DeepCopy( roOther );
}
/* End of function "VMDataStack::VMDataStack"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  operator =

       DESCRIPTION:  clears this then a deep copy (see above) of another stack

             INPUT:  roOther - the stack to copy
            OUTPUT:  none

           RETURNS:  none
*/
void VMDataStack::operator =( VMDataStack& roOther )
{
  Clear();
  DeepCopy( roOther );
}
/* End of function "="
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  ~VMDataStack

       DESCRIPTION:  clears this 

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMDataStack::~VMDataStack( void )
{
  Clear();
}
/* End of function "VMDataStack::~VMDataStack"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  Push

       DESCRIPTION:  does the work required to add a new element to this 
                     by allocating a new node, copying the item into the node
                     data element and then linking the head/next pointers
                     appropriately.

                     NOTE: The item to be pushed MUST IMPLEMENT a copy constructor
             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
void VMDataStack::Push( VMVariant& roItem )
{
  PNODE pNewNode = (PNODE) new NODE;
 
  if ( NULL == pNewNode )
  {
    VMException  oError( __FILE__, 
                         __LINE__, 
                         ALLOCATION_ERROR, 
                         VMException::fatal );
    throw oError;
  }

  pNewNode->m_pxNext = m_pxHead;
  pNewNode->m_oData.CloneValue( roItem );

  m_pxHead = pNewNode;
  
  ++m_xCount;
}
/* End of function "VMDataStack::Push"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  PeekTop

       DESCRIPTION:  returns the item that is at the top of the stack without
                     popping the item. ie is a 'non-destructive' read

             INPUT:  void
            OUTPUT:  none

           RETURNS:  true if value returned, false if not
*/
bool VMDataStack::PeekTop( VMVariant& roOutput )
{
  if ( NULL == m_pxHead )
  {
   return( false );
  }
  roOutput.CloneValue( m_pxHead->m_oData );
  return( true );
}
/* End of function "VMDataStack::PeekTop"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  Pop

       DESCRIPTION:  returns the item that is at the top of the stack

             INPUT:  void
            OUTPUT:  none

           RETURNS:  true if item returned, false if not
*/
bool VMDataStack::Pop( VMVariant& roOutput )
{
  if ( NULL == m_pxHead )
  {
    return( false );
  }

  roOutput.CloneValue( m_pxHead->m_oData );

  PNODE pNewHead = m_pxHead->m_pxNext;

  delete m_pxHead;

  m_pxHead = pNewHead;

  --m_xCount;

  return( true );
}
/* End of function "VMDataStack::Pop"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  GetCount

       DESCRIPTION:  returns the number of items stored in this 

             INPUT:  void
            OUTPUT:  none

           RETURNS:  the number of items
*/
size_t VMDataStack::GetCount( void )
{
  return( m_xCount );
}
/* End of function "VMDataStack::GetCount"
/*****************************************************************************/




/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/

⌨️ 快捷键说明

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