📄 vmqueue.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implemenation for the queue class for VMVariant classes
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 "VMQueue.h"
/*****************************************************************************/
/*
FUNCTION NAME: DeepCopy
DESCRIPTION: performs an elemental copy of another stack
INPUT: roOther - the stack to copy
OUTPUT: none
RETURNS: none
*/
void VMDataQueue::DeepCopy( VMDataQueue& roOther )
{
if ( roOther.GetCount() == 0 )
{
m_pxHead = NULL;
m_pxTail = NULL;
m_xCount = 0;
return;
}
m_pxHead = (PNODE)new NODE;
if ( NULL == m_pxHead )
{
VMException oError( __FILE__,
__LINE__,
ALLOCATION_ERROR,
VMException::fatal );
throw oError;
}
m_pxTail = m_pxHead;
PNODE pxSource = roOther.m_pxHead;
while ( true )
{
m_pxTail->m_oData.CloneValue( pxSource->m_oData );
++m_xCount;
if ( NULL != pxSource->m_pxNext )
{
m_pxTail->m_pxNext = (PNODE) new NODE;
if ( NULL == m_pxTail->m_pxNext )
{
VMException oError( __FILE__,
__LINE__,
ALLOCATION_ERROR,
VMException::fatal );
throw oError;
}
m_pxTail = m_pxTail->m_pxNext;
pxSource = pxSource->m_pxNext;
}
else
{
m_pxTail->m_pxNext = NULL;
break;
}
}
}
/* End of function "VMDataQueue::DeepCopy"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Clear
DESCRIPTION: Pops all elements off the queue
INPUT: void
OUTPUT: none
RETURNS: none
*/
void VMDataQueue::Clear( void )
{
VMVariant xIgnored;
while ( m_xCount )
{
Dequeue( xIgnored );
}
}
/* End of function "VMDataQueue::Clear"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMDataQueue
DESCRIPTION: ctor.
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMDataQueue::VMDataQueue( void )
{
m_xCount = 0;
m_pxHead = NULL;
m_pxTail = NULL;
}
/* End of function "VMDataQueue::VMDataQueue"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMDataQueue
DESCRIPTION: Copy ctor
INPUT: roOther - the stack to copy
OUTPUT: none
RETURNS: none
*/
VMDataQueue::VMDataQueue( VMDataQueue& roOther )
{
m_xCount = 0;
DeepCopy( roOther );
}
/* End of function "VMDataQueue::VMDataQueue"
/*****************************************************************************/
/*****************************************************************************/
/*
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 VMDataQueue::operator =( VMDataQueue& roOther )
{
Clear();
DeepCopy( roOther );
}
/* End of function "="
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: ~VMDataQueue
DESCRIPTION: clears this
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMDataQueue::~VMDataQueue( void )
{
Clear();
}
/* End of function "VMDataQueue::~VMDataQueue"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Enqueue
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 VMDataQueue::Enqueue( VMVariant& roItem )
{
PNODE pNewNode = new NODE;
if ( NULL == pNewNode )
{
VMException oError( __FILE__,
__LINE__,
ALLOCATION_ERROR,
VMException::fatal );
throw oError;
}
pNewNode->m_pxNext = NULL;
pNewNode->m_oData.CloneValue( roItem );
if ( NULL != m_pxTail )
{
m_pxTail->m_pxNext = pNewNode;
}
else
{
m_pxHead = pNewNode;
}
m_pxTail = pNewNode;
++m_xCount;
}
/* End of function "VMDataQueue::Enqueue"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: Dequeue
DESCRIPTION: returns the item that is at the top of the stack
INPUT: void
OUTPUT: none
RETURNS: true if an item returned, false if not
*/
bool VMDataQueue::Dequeue( 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;
if ( 0 == m_xCount )
{
m_pxTail = NULL;
}
return( true );
}
/* End of function "VMDataQueue::Dequeue"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GetCount
DESCRIPTION: returns the number of items stored in this
INPUT: void
OUTPUT: none
RETURNS: the number of items
*/
size_t VMDataQueue::GetCount( void )
{
return( m_xCount );
}
/* End of function "VMDataQueue::GetCount"
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -