From f1be4e95407d91edcc0b50c290afa47273f6a016 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Thu, 13 Dec 2018 12:41:37 +0200 Subject: [PATCH] Important Update: - Switch from malloc/free -> new/delete for structures - Bug fix: Move Parameter groups to Device (not class) - Update Parameter groups: - Update Read/Write indicators as params are added --- DeviceCore.cpp | 51 +++++++++++++++++++++++++++++++++++------------- DeviceCore.h | 27 ++++++++++++++----------- FunctionCore.cpp | 15 ++++++-------- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/DeviceCore.cpp b/DeviceCore.cpp index 53c9d51..de813fa 100644 --- a/DeviceCore.cpp +++ b/DeviceCore.cpp @@ -259,6 +259,24 @@ bool CDeviceCore::InitParamGroups( TDevice * Device, CDataMember * DeviceConfig CDataMember * GroupData; CDataMember * ItemData; TDeviceParamGroup * Group; + TDeviceParamGroup * GroupTemplate; + TDeviceParamItem * ItemTemplate; + + // Copy Template + GroupTemplate = (Device->Template)? Device->Template->FirstParamGroup : NULL; + while (GroupTemplate) + { + if ((Group = AddParamGroup( Device, GroupTemplate->Name ))) + { + ItemTemplate = GroupTemplate->FirstParam; + while (ItemTemplate) + { + AddParamItem( Group, ItemTemplate->Param->Name ); + ItemTemplate = ItemTemplate->NextItem; + } + } + GroupTemplate = GroupTemplate->NextGroup; + } // Add Parameters GroupData = DeviceConfig->GetChFirstChild( "ParamGroups", true ); @@ -374,7 +392,7 @@ TDevice * CDeviceCore::AddDeviceType( const char * DeviceTypeName ) ProcessName, Name, DeviceTypeName ); } else { - *DeviceType = (TDevice*)calloc( 1, sizeof(TDevice) ); + *DeviceType = new TDevice; (*DeviceType)->Type = (char *)malloc( strlen( DeviceTypeName )+1 ); strcpy( (*DeviceType)->Type, DeviceTypeName ); @@ -400,8 +418,7 @@ TDevice * CDeviceCore::AddDevice( const char * DeviceName, const char * Type ) DeviceName, (((*Device)->Type)? (*Device)->Type : "no type") ); } else { - *Device = (TDevice*)malloc( sizeof(TDevice) ); - memset( *Device, 0x0, sizeof(TDevice) ); + *Device = new TDevice; (*Device)->Name = (char *)malloc( strlen( DeviceName )+1 ); strcpy( (*Device)->Name, DeviceName ); @@ -437,13 +454,16 @@ bool CDeviceCore::DestroyDevice( TDevice ** Device ) if ((*Device)->Type) free( (*Device)->Type ); - // Destroy paramters + // Destroy parameters & groups while ((*Device)->FirstParam) { DestroyDeviceParam( &((*Device)->FirstParam) ); } + while ((*Device)->FirstParamGroup) { + DestroyParamGroup( &((*Device)->FirstParamGroup) ); + } // Destroy Device - free( *Device ); + delete *Device; // Remove from list *Device = NextDevice; @@ -471,8 +491,7 @@ TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * Param } else { // Create register - *Param = (TDeviceParam*)malloc( sizeof(TDeviceParam) ); - memset( *Param, 0x0, sizeof(TDeviceParam) ); + *Param = new TDeviceParam; // Set Name (*Param)->Name = (char *)malloc( strlen( ParamName )+1 ); @@ -587,7 +606,7 @@ bool CDeviceCore::DestroyDeviceParam( TDeviceParam ** Param ) free( (*Param)->SetValue ); // Destroy Param - free( *Param ); + delete *Param; // Remove from list *Param = NextParam; @@ -599,7 +618,7 @@ bool CDeviceCore::DestroyDeviceParam( TDeviceParam ** Param ) TDeviceParamGroup * CDeviceCore::AddParamGroup( TDevice * Device, const char * GroupName ) { - TDeviceParamGroup ** ParamGroup = GetParamGroupPtr( GroupName ); + TDeviceParamGroup ** ParamGroup = GetParamGroupPtr( Device, GroupName ); if (!ParamGroup) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Could not add param group '%s'", ProcessName, Name, @@ -612,7 +631,7 @@ TDeviceParamGroup * CDeviceCore::AddParamGroup( TDevice * Device, const char * G ProcessName, Name, ((Device->Name)? Device->Name : Device->Type), GroupName ); } else { - *ParamGroup = (TDeviceParamGroup*)calloc( 1, sizeof(TDeviceParamGroup) ); + *ParamGroup = new TDeviceParamGroup; (*ParamGroup)->Name = (char *)malloc( strlen( GroupName )+1 ); strcpy( (*ParamGroup)->Name, GroupName ); @@ -647,7 +666,7 @@ bool CDeviceCore::DestroyParamGroup( TDeviceParamGroup ** ParamGroup ) } // Destroy ParamGroup - free( *ParamGroup ); + delete *ParamGroup; // Remove from list *ParamGroup = NextGroup; @@ -679,11 +698,15 @@ TDeviceParamItem * CDeviceCore::AddParamItem( TDeviceParamGroup * Group, const c ((Group->Device->Name)? Group->Device->Name : Group->Device->Type), ParamName, Group->Name ); } else { - *Item = (TDeviceParamItem*)malloc( sizeof(TDeviceParamItem) ); - memset( *Item, 0x0, sizeof(TDeviceParamItem) ); + *Item = new TDeviceParamItem; (*Item)->Param = Param; + if (Param->Read) + Group->Read = true; + if (Param->Write) + Group->Write = true; + if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Added param group item '%s/%s/%s'", ProcessName, Name, ((Group->Device->Name)? Group->Device->Name : Group->Device->Type), Group->Name, ParamName ); } @@ -699,7 +722,7 @@ bool CDeviceCore::DestroyParamItem( TDeviceParamItem ** Item ) return false; NextParam = (*Item)->NextItem; - free( *Item ); + delete *Item; *Item = NextParam; return true; diff --git a/DeviceCore.h b/DeviceCore.h index 5a00b33..9d05aae 100644 --- a/DeviceCore.h +++ b/DeviceCore.h @@ -32,6 +32,8 @@ typedef struct SDeviceParam TDeviceParam; typedef struct SDeviceParamGroup TDeviceParamGroup; typedef struct SDeviceParamItem TDeviceParamItem; +//--------------------------------------------------------------------------- + // Devices with are polled struct SDevice { // Device definition @@ -43,7 +45,8 @@ struct SDevice { bool Online = false; // Device parameters - TDeviceParam * FirstParam = NULL; + TDeviceParam * FirstParam = NULL; + TDeviceParamGroup * FirstParamGroup = NULL; // Device peer list TDevice * Next = NULL; @@ -82,6 +85,10 @@ struct SDeviceParamGroup { char * Name = NULL; TDevice * Device = NULL; TDeviceParamItem * FirstParam = NULL; + + bool Read = false; + bool Write = false; + TDeviceParamGroup * NextGroup = NULL; }; @@ -112,8 +119,6 @@ protected: TDevice * FirstDevice = NULL; TDevice * ActiveDevice = NULL; - TDeviceParamGroup * FirstParamGroup = NULL; - // Standard channels TChannel * DeviceChannel = NULL; TChannel * CmdChannel = NULL; @@ -215,13 +220,13 @@ protected: bool DestroyParamItem( TDeviceParamItem ** Item ); // Find Param Groups - inline TDeviceParamGroup * GetParamGroup( const char * GroupName ) { - TDeviceParamGroup ** Group = GetParamGroupPtr( GroupName ); + inline TDeviceParamGroup * GetParamGroup( TDevice * Device, const char * GroupName ) { + TDeviceParamGroup ** Group = GetParamGroupPtr( Device, GroupName ); return (Group)? *Group : NULL; } - inline TDeviceParamGroup ** GetParamGroupPtr( const char * GroupName ) { - if (!GroupName || !*GroupName) return NULL; - TDeviceParamGroup ** Group = &FirstParamGroup; + inline TDeviceParamGroup ** GetParamGroupPtr( TDevice * Device, const char * GroupName ) { + if (!Device || !GroupName || !*GroupName) return NULL; + TDeviceParamGroup ** Group = &(Device->FirstParamGroup); while (*Group && strcasecmp( (*Group)->Name, GroupName )) Group = &((*Group)->NextGroup); return Group; @@ -302,12 +307,12 @@ public: // Manage Param Groups TDeviceParamGroup * AddParamGroup( TDevice * Device, const char * GroupName ); - inline bool DestroyParamGroup( const char * GroupName ) { - TDeviceParamGroup ** Group = GetParamGroupPtr( GroupName ); + inline bool DestroyParamGroup( TDevice * Device, const char * GroupName ) { + TDeviceParamGroup ** Group = GetParamGroupPtr( Device, GroupName ); return (Group)? DestroyParamGroup( Group ) : false; }; TDeviceParamItem * AddParamItem( TDeviceParamGroup * Group, const char * ParamName ); - inline bool DestroyParamItem( TDeviceParamGroup * Group, const char * ParamName ) { + inline bool DestroyParamItem( TDevice * Device, TDeviceParamGroup * Group, const char * ParamName ) { TDeviceParamItem ** Item = GetParamItemPtr( Group, ParamName ); return (Item)? DestroyParamItem( Item ) : false; }; diff --git a/FunctionCore.cpp b/FunctionCore.cpp index 7b71799..055fd40 100644 --- a/FunctionCore.cpp +++ b/FunctionCore.cpp @@ -61,7 +61,7 @@ CFunctionCore::~CFunctionCore() free( FirstChannel->FirstInput->Name ); } NextLinkedChannel = FirstChannel->FirstInput->Next; - free( FirstChannel->FirstInput ); + delete FirstChannel->FirstInput; FirstChannel->FirstInput = NextLinkedChannel; } @@ -71,13 +71,13 @@ CFunctionCore::~CFunctionCore() free( FirstChannel->FirstOutput->Name ); } NextLinkedChannel = FirstChannel->FirstOutput->Next; - free( FirstChannel->FirstOutput ); + delete FirstChannel->FirstOutput; FirstChannel->FirstOutput = NextLinkedChannel; } // Destroy Channel NextChannel = FirstChannel->Next; - free( FirstChannel ); + delete FirstChannel; FirstChannel = NextChannel; } @@ -192,8 +192,7 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const bool pInpu // Create if not exist if (!*Channel) { // Create - *Channel = (TChannel*)malloc( sizeof(TChannel) ); - memset( *Channel, 0, sizeof(TChannel) ); + *Channel = new TChannel; // Set Name (*Channel)->Name = (char*)malloc( strlen(ChannelName)+1 ); @@ -234,8 +233,7 @@ bool CFunctionCore::LinkInputChannel( const char * ChannelName, const char * Out if (!*LinkedChannel) { // Create - *LinkedChannel = (TChannelLink*)malloc( sizeof(TChannelLink) ); - memset( *LinkedChannel, 0, sizeof( TChannelLink )); + *LinkedChannel = new TChannelLink; // Set Parameters (*LinkedChannel)->Function = OutFunction; @@ -280,8 +278,7 @@ bool CFunctionCore::LinkOutputChannel( const char * ChannelName, const char * In if (!*LinkedChannel) { // Create - *LinkedChannel = (TChannelLink*)malloc( sizeof(TChannelLink) ); - memset( *LinkedChannel, 0, sizeof( TChannelLink )); + *LinkedChannel = new TChannelLink; // Set Parameters (*LinkedChannel)->Function = InFunction;