You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
5.4 KiB
292 lines
5.4 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2001
|
|
// Last modification: 27/10/2003
|
|
// PROJECT: Laserguard libraries
|
|
// SUBSYSTEM: LgLib
|
|
// FILE: Templ.h
|
|
// AUTHOR: Chizhevskiy Sergey
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Header file of for usefull templates
|
|
//----------------------------------------------------------------------------
|
|
|
|
#pragma warning( disable : 4786 )
|
|
|
|
#ifndef __TEMPLATES__H__INCLUDED__
|
|
#define __TEMPLATES__H__INCLUDED__
|
|
|
|
#include "LgLock.h"
|
|
#include "LgArray.h"
|
|
|
|
template <class TYPE, class ARGTYPE = TYPE&>
|
|
class CLgArrayP :
|
|
public CLgArray<TYPE,ARGTYPE>,
|
|
public CLgCriticalSection
|
|
{
|
|
};
|
|
|
|
template <class TYPE, class ARGTYPE = TYPE&>
|
|
class CLgListP :
|
|
public CLgList<TYPE,ARGTYPE>,
|
|
public CLgCriticalSection
|
|
{
|
|
};
|
|
|
|
template <class TYPE, const int MAXSIZE>
|
|
class CLgQueueP :
|
|
public CLgQueue<TYPE,MAXSIZE>,
|
|
public CLgCriticalSection,
|
|
public CLgEvent
|
|
{
|
|
public:
|
|
virtual BOOL Push( TYPE data )
|
|
{
|
|
CLgLocker Lock( this );
|
|
if( !CLgQueue<TYPE,MAXSIZE>::Push( data ) )
|
|
return FALSE;
|
|
|
|
return CLgEvent::Set();
|
|
}
|
|
virtual BOOL Pop( TYPE& data )
|
|
{
|
|
CLgLocker Lock( this );
|
|
|
|
return CLgQueue<TYPE,MAXSIZE>::Pop( data );
|
|
}
|
|
virtual BOOL Clear()
|
|
{
|
|
CLgLocker Lock( this );
|
|
return CLgQueue<TYPE,MAXSIZE>::Clear();
|
|
}
|
|
};
|
|
|
|
template <class TYPE, class ARGTYPE = TYPE&>
|
|
class CLgStackP :
|
|
public CLgStack<TYPE,ARGTYPE>,
|
|
public CLgCriticalSection,
|
|
public CLgEvent
|
|
{
|
|
public:
|
|
virtual BOOL Push( TYPE data )
|
|
{
|
|
CLgLocker Lock( this );
|
|
if( !CLgStack<TYPE,ARGTYPE>::Push( data ) )
|
|
return FALSE;
|
|
|
|
return CLgEvent::Set();
|
|
}
|
|
virtual BOOL Pop( TYPE& data )
|
|
{
|
|
CLgLocker Lock( this );
|
|
return CLgStack<TYPE,ARGTYPE>::Pop( data );
|
|
}
|
|
virtual BOOL Peek( ARGTYPE data )
|
|
{
|
|
CLgLocker Lock( this );
|
|
return CLgStack<TYPE,ARGTYPE>::Peek( data );
|
|
}
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// fast stream without overflow
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
template< class TYPE, const INT MAX_CAPACITY >
|
|
class CLgFastStream
|
|
{
|
|
public:
|
|
CLgFastStream()
|
|
{
|
|
m_nCount = 0;
|
|
m_nHead = m_nTail = 0;
|
|
m_pBuffer = NULL;
|
|
}
|
|
~CLgFastStream()
|
|
{
|
|
Destroy();
|
|
}
|
|
|
|
public:
|
|
BOOL Create()
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
|
|
Destroy();
|
|
|
|
m_pBuffer = (TYPE*)malloc( cMaxCapacity * sizeof(TYPE));
|
|
if( !m_pBuffer )
|
|
return FALSE;
|
|
|
|
m_nCount = 0;
|
|
m_nHead = m_nTail = 0;
|
|
|
|
return TRUE;
|
|
}
|
|
void Destroy()
|
|
{
|
|
m_nCount = 0;
|
|
m_nHead = m_nTail = 0;
|
|
|
|
if( m_pBuffer )
|
|
{
|
|
free( m_pBuffer );
|
|
m_pBuffer = NULL;
|
|
}
|
|
}
|
|
|
|
public:
|
|
BOOL Put( TYPE byte )
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const INT cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( !m_pBuffer )
|
|
return FALSE;
|
|
|
|
if( m_nCount >= cMaxCapacity )
|
|
return FALSE;
|
|
|
|
m_pBuffer[ m_nHead ] = byte;
|
|
|
|
m_nHead = (m_nHead + 1) & cCapacityMask;
|
|
|
|
m_nCount ++;
|
|
|
|
return TRUE;
|
|
}
|
|
BOOL Get( TYPE *pByte )
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const INT cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( m_nCount <= 0 )
|
|
return FALSE;
|
|
|
|
*pByte = m_pBuffer[ m_nTail ];
|
|
|
|
m_nTail = (m_nTail + 1) & cCapacityMask;
|
|
|
|
m_nCount --;
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
INT Put( TYPE *pByte, INT nCount )
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const INT cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( !m_pBuffer )
|
|
return 0;
|
|
|
|
for( INT i = 0; (i < nCount) && (m_nCount < cMaxCapacity); i++ )
|
|
{
|
|
m_pBuffer[ m_nHead ] = *pByte++;
|
|
m_nHead = (m_nHead + 1) & cCapacityMask;
|
|
m_nCount ++;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
INT Get( TYPE *pByte, INT nCount )
|
|
{
|
|
return Skip( Peek( pByte, nCount ) );
|
|
}
|
|
INT Peek( TYPE *pByte, INT nCount )
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const INT cCapacityMask = cMaxCapacity-1;
|
|
|
|
INT nTempTail = m_nTail;
|
|
INT nTempCount = m_nCount;
|
|
for( INT i = 0; (i < nCount) && nTempCount; i++ )
|
|
{
|
|
*pByte++ = m_pBuffer[ nTempTail ];
|
|
nTempTail = (nTempTail + 1) & cCapacityMask;
|
|
nTempCount --;
|
|
}
|
|
return i;
|
|
}
|
|
INT Skip( INT nCount )
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const INT cCapacityMask = cMaxCapacity-1;
|
|
|
|
// validate count
|
|
if( nCount > m_nCount )
|
|
nCount = m_nCount;
|
|
|
|
// update pointers
|
|
m_nTail = (m_nTail + nCount) & cCapacityMask;
|
|
m_nCount -= nCount;
|
|
|
|
return nCount;
|
|
}
|
|
|
|
INT GetCount()
|
|
{
|
|
return m_nCount;
|
|
}
|
|
|
|
INT GetFree()
|
|
{
|
|
const INT cMaxCapacity = (1<<MAX_CAPACITY);
|
|
|
|
if( !m_pBuffer )
|
|
return FALSE;
|
|
|
|
return cMaxCapacity - m_nCount;
|
|
}
|
|
|
|
void Clear()
|
|
{
|
|
m_nCount = 0;
|
|
m_nHead = m_nTail = 0;
|
|
}
|
|
|
|
private:
|
|
TYPE* m_pBuffer;
|
|
INT m_nCount;
|
|
INT m_nHead;
|
|
INT m_nTail;
|
|
};
|
|
|
|
|
|
template< class TYPE, const INT MAX_CAPACITY >
|
|
class CLgProtectedFastStream :
|
|
public CLgFastStream<TYPE,MAX_CAPACITY>,
|
|
public CLgCriticalSection
|
|
{
|
|
};
|
|
|
|
template< class TYPE, class ARG_TYPE = TYPE& >
|
|
class CLgValueP : public CLgCriticalSection
|
|
{
|
|
public:
|
|
operator TYPE()
|
|
{
|
|
return m_Value;
|
|
}
|
|
TYPE& operator ->()
|
|
{
|
|
return m_Value;
|
|
}
|
|
CLgValueP& operator =( ARG_TYPE value )
|
|
{
|
|
m_Value = value;
|
|
return *this;
|
|
}
|
|
TYPE& operator *()
|
|
{
|
|
return m_Value;
|
|
}
|
|
|
|
private:
|
|
TYPE m_Value;
|
|
};
|
|
|
|
|
|
#endif
|
|
|
|
#pragma warning( default : 4786 )
|