From 75958ec52a17e91e51cebaadd327fc9ada39d6fe Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Tue, 5 Nov 2019 17:43:28 +0100 Subject: [PATCH] get rid of dynamic cast, because we have no rtti on arduino --- src/knx/memory.cpp | 61 ++++++++++++++++++++++------------------ src/knx/memory.h | 9 ++++-- src/knx/table_object.cpp | 1 + src/knx/table_object.h | 2 +- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/knx/memory.cpp b/src/knx/memory.cpp index fdb1663..58e846a 100644 --- a/src/knx/memory.cpp +++ b/src/knx/memory.cpp @@ -1,7 +1,6 @@ #include "memory.h" #include #include "bits.h" -#include "table_object.h" Memory::Memory(Platform& platform, DeviceObject& deviceObject) : _platform(platform), _deviceObject(deviceObject) @@ -37,19 +36,19 @@ void Memory::readMemory() return; } - int size = _saveCount; - for (int i = 0; i < size; i++) + for (int i = 0; i < _saveCount; i++) { buffer = _saveRestores[i]->restore(buffer); - TableObject* tableObject = dynamic_cast(_saveRestores[i]); - if (tableObject) - { - uint16_t memorySize = 0; - buffer = popWord(memorySize, buffer); + } - // this works because TableObject saves a relative addr and restores it itself - addNewUsedBlock(tableObject->_data, memorySize); - } + for (int i = 0; i < _tableObjCount; i++) + { + buffer = _tableObjects[i]->restore(buffer); + uint16_t memorySize = 0; + buffer = popWord(memorySize, buffer); + + // this works because TableObject saves a relative addr and restores it itself + addNewUsedBlock(_tableObjects[i]->_data, memorySize); } } @@ -60,24 +59,26 @@ void Memory::writeMemory() buffer = pushByteArray(_deviceObject.hardwareType(), LEN_HARDWARE_TYPE, buffer); buffer = pushWord(_deviceObject.version(), buffer); - int size = _saveCount; - for (int i = 0; i < size; i++) + for (int i = 0; i < _saveCount; i++) { buffer = _saveRestores[i]->save(buffer); + } + + for (int i = 0; i < _tableObjCount; i++) + { + buffer = _tableObjects[i]->save(buffer); //save to size of the memoryblock for tableobject too, so that we can rebuild the usedList and freeList - TableObject* tableObject = dynamic_cast(_saveRestores[i]); - if (tableObject) + + MemoryBlock* block = findBlockInList(_usedList, _tableObjects[i]->_data); + if (block == nullptr) { - MemoryBlock* block = findBlockInList(_usedList, tableObject->_data); - if (block == nullptr) - { - println("_data of TableObject not in errorlist"); - _platform.fatalError(); - } - buffer = pushWord(block->size, buffer); + println("_data of TableObject not in errorlist"); + _platform.fatalError(); } + buffer = pushWord(block->size, buffer); } + _platform.commitToEeprom(); } @@ -89,14 +90,18 @@ void Memory::addSaveRestore(SaveRestore* obj) _saveRestores[_saveCount] = obj; _saveCount += 1; _metadataSize += obj->saveSize(); - - TableObject* tableObject = dynamic_cast(obj); - if (tableObject) - { - _metadataSize += 2; // - } } +void Memory::addSaveRestore(TableObject* obj) +{ + if (_tableObjCount >= MAXTABLEOBJ - 1) + return; + + _tableObjects[_tableObjCount] = obj; + _tableObjCount += 1; + _metadataSize += obj->saveSize(); + _metadataSize += 2; // for size +} uint8_t* Memory::allocMemory(size_t size) { diff --git a/src/knx/memory.h b/src/knx/memory.h index 5097729..b0794b5 100644 --- a/src/knx/memory.h +++ b/src/knx/memory.h @@ -4,8 +4,10 @@ #include "save_restore.h" #include "platform.h" #include "device_object.h" +#include "table_object.h" -#define MAXSAVE 10 +#define MAXSAVE 5 +#define MAXTABLEOBJ 4 class MemoryBlock { @@ -25,6 +27,7 @@ public: void readMemory(); void writeMemory(); void addSaveRestore(SaveRestore* obj); + void addSaveRestore(TableObject* obj); uint8_t* allocMemory(size_t size); void freeMemory(uint8_t* ptr); @@ -45,7 +48,9 @@ public: Platform& _platform; DeviceObject& _deviceObject; SaveRestore* _saveRestores[MAXSAVE] = {0}; - int _saveCount = 0; + TableObject* _tableObjects[MAXTABLEOBJ] = {0}; + uint8_t _saveCount = 0; + uint8_t _tableObjCount = 0; uint8_t* _data = nullptr; MemoryBlock* _freeList = nullptr; MemoryBlock* _usedList = nullptr; diff --git a/src/knx/table_object.cpp b/src/knx/table_object.cpp index 64f816a..16cc7f0 100644 --- a/src/knx/table_object.cpp +++ b/src/knx/table_object.cpp @@ -2,6 +2,7 @@ #include "table_object.h" #include "bits.h" +#include "memory.h" TableObject::TableObject(Memory& memory): _memory(memory) { diff --git a/src/knx/table_object.h b/src/knx/table_object.h index 25d4636..2bdcd08 100644 --- a/src/knx/table_object.h +++ b/src/knx/table_object.h @@ -1,8 +1,8 @@ #pragma once #include "interface_object.h" -#include "memory.h" +class Memory; /** * This class provides common functionality for interface objects that are configured by ETS with MemorWrite. */