- ItemBufferCore:
- New Class for storing items in a FIFO style buffer
- FunctionCore:
- Implemented pulled input:
- Function creates static output: StoredOutput & StoredOutputLen
- Other Function pulls static output from input linked functions
- FunctionCore-ChannelBuffer:
- Tried to create a processing buffer
- Abandoned for now
189 lines
4.1 KiB
C++
189 lines
4.1 KiB
C++
/*
|
|
* BufferCore.h
|
|
*
|
|
* Created on: 22 July 2017
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
// Standard C/C++ Libraries
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// redA Libraries
|
|
#include "ItemBufferCore.h"
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//***** Function Prototypes *****//
|
|
|
|
CItemBufferCore::CItemBufferCore( bool pCopyEntries )
|
|
{
|
|
// Parameters
|
|
CopyEntries = pCopyEntries;
|
|
|
|
// Clear Event list
|
|
FirstItem = NULL;
|
|
LastItem = &FirstItem;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
CItemBufferCore::~ CItemBufferCore()
|
|
{
|
|
// Free Event List
|
|
DeleteAll();
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
TBufferItem * CItemBufferCore::CreateItem( void * Entry, int Size )
|
|
{
|
|
TBufferItem * BufferItem;
|
|
|
|
// Create structure
|
|
if (!(BufferItem = (TBufferItem *)calloc( sizeof(TBufferItem), 1 )))
|
|
return NULL;
|
|
|
|
// Create Entry
|
|
BufferItem->Size = Size;
|
|
if (CopyEntries)
|
|
{
|
|
// Create copy
|
|
if ((BufferItem->Entry = (void *)malloc( sizeof(char)*Size ))) {
|
|
// Copy Value
|
|
memcpy( BufferItem->Entry, Entry, Size );
|
|
}
|
|
else {
|
|
// Fail
|
|
free( BufferItem );
|
|
return NULL;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Use pointer as is
|
|
BufferItem->Entry = Entry;
|
|
}
|
|
|
|
// Return empty structure
|
|
return BufferItem;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CItemBufferCore::DestroyItem( TBufferItem ** BufferItem )
|
|
{
|
|
// Check if valid pointer
|
|
if (!BufferItem || !*BufferItem)
|
|
return false;
|
|
|
|
// Destroy pointers
|
|
if (CopyEntries && (*BufferItem)->Entry)
|
|
DestroyEntry( &((*BufferItem)->Entry) );
|
|
|
|
// Destroy structure
|
|
free( *BufferItem );
|
|
*BufferItem = NULL;
|
|
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CItemBufferCore::DestroyEntry( void ** Entry )
|
|
{
|
|
// Verify pointer
|
|
if (!Entry || !*Entry)
|
|
return false;
|
|
|
|
// free & clear memory
|
|
free( *Entry );
|
|
*Entry = NULL;
|
|
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CItemBufferCore::Push( void * Entry, int Size )
|
|
{
|
|
// Verify pointer
|
|
if (!Entry || !Size)
|
|
return false;
|
|
|
|
// Try to create an add item to list
|
|
if (!(*LastItem = CreateItem( Entry, Size )))
|
|
return false;
|
|
|
|
// Move Next entry reference
|
|
LastItem = &((*LastItem)->NextItem);
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void * CItemBufferCore::Pop( int * Size )
|
|
{
|
|
void * Entry;
|
|
|
|
// Return entry
|
|
if ((Entry = Peek( Size ))) {
|
|
Delete();
|
|
}
|
|
|
|
// return event
|
|
return Entry;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void * CItemBufferCore::Peek( int * Size )
|
|
{
|
|
void * Entry;
|
|
|
|
// Check if any events available
|
|
if (!FirstItem) {
|
|
if (Size) *Size = 0;
|
|
return NULL;
|
|
}
|
|
|
|
// Get entry and size
|
|
Entry = FirstItem->Entry;
|
|
if (Size) *Size = FirstItem->Size;
|
|
|
|
// return event
|
|
return Entry;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CItemBufferCore::Delete()
|
|
{
|
|
TBufferItem * BufferItem;
|
|
|
|
// check if buffer empty
|
|
if (!FirstItem)
|
|
return false;
|
|
|
|
// Delete event from list
|
|
BufferItem = FirstItem;
|
|
FirstItem = FirstItem->NextItem;
|
|
|
|
// Check if last event pointer still correct
|
|
if (!FirstItem)
|
|
LastItem = &FirstItem;
|
|
|
|
// Destroy entry and holder
|
|
if (CopyEntries)
|
|
DestroyEntry( &(BufferItem->Entry) );
|
|
free( BufferItem );
|
|
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void CItemBufferCore::DeleteAll()
|
|
{
|
|
TBufferItem * NextItem;
|
|
|
|
while (FirstItem)
|
|
{
|
|
NextItem = FirstItem->NextItem;
|
|
DestroyItem( &FirstItem );
|
|
FirstItem = NextItem;
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|