From ae842147119360391eb4961a7795a9a3b4142b23 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 26 Jun 2016 12:27:29 +0200 Subject: [PATCH] fixed cleanup --- src/lib/ebus/datatype.cpp | 16 ++++++---------- src/lib/ebus/datatype.h | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index 0ecacf46..dc21932c 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -567,6 +567,7 @@ result_t NumberDataType::derive(int divisor, unsigned char bitCount, NumberDataT derived = new NumberDataType(m_id, bitCount, m_flags, m_replacement, m_minValue, m_maxValue, divisor); derived->m_baseType = m_baseType ? m_baseType : this; + DataTypeList::getInstance()->addCleanup(derived); return RESULT_OK; } @@ -930,17 +931,10 @@ DataTypeList* DataTypeList::getInstance() void DataTypeList::clear() { - for (map::iterator it = m_typesByIdLength.begin(); it != m_typesByIdLength.end(); it++) { - if (m_typesById[it->second->getId()] == it->second) { - m_typesById.erase(it->second->getId()); - } - delete it->second; - it->second = NULL; - } - for (map::iterator it = m_typesById.begin(); it != m_typesById.end(); it++) { - delete it->second; - it->second = NULL; + for (list::iterator it = m_cleanupTypes.begin(); it != m_cleanupTypes.end(); it++) { + delete *it; } + m_cleanupTypes.clear(); m_typesByIdLength.clear(); m_typesById.clear(); } @@ -957,12 +951,14 @@ result_t DataTypeList::add(DataType* dataType) } m_typesByIdLength[str.str()] = dataType; if (m_typesById.find(dataType->getId()) != m_typesById.end()) { + m_cleanupTypes.push_back(dataType); return RESULT_OK; // only store first one as default } } else if (m_typesById.find(dataType->getId()) != m_typesById.end()) { return RESULT_ERR_DUPLICATE_NAME; // duplicate key } m_typesById[dataType->getId()] = dataType; + m_cleanupTypes.push_back(dataType); return RESULT_OK; } diff --git a/src/lib/ebus/datatype.h b/src/lib/ebus/datatype.h index a6d63868..0acbcbd3 100644 --- a/src/lib/ebus/datatype.h +++ b/src/lib/ebus/datatype.h @@ -27,6 +27,7 @@ #include #include #include +#include #include /** @file datatype.h @@ -151,13 +152,13 @@ public: * @param flags the combination of flags (like #BCD). * @param replacement the replacement value (fill-up value for @a StringDataType, no replacement if equal to @a NumberDataType#minValue). */ - DataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement) + DataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement) : m_id(id), m_bitCount(bitCount), m_flags(flags), m_replacement(replacement) {} /** * Destructor. */ - virtual ~DataType() {} + virtual ~DataType() { } /** * @return the type identifier. @@ -241,7 +242,7 @@ public: protected: /** the type identifier. */ - const char* m_id; + const string m_id; /** the number of bits (maximum length if #ADJ flag is set, must be multiple of 8 with flag #BCD). */ const unsigned char m_bitCount; @@ -270,7 +271,7 @@ public: * @param replacement the replacement value (fill-up value). * @param isHex true for hex digits instead of characters. */ - StringDataType(const char* id, const unsigned char bitCount, const unsigned short flags, + StringDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, bool isHex=false) : DataType(id, bitCount, flags, replacement), m_isHex(isHex) {} @@ -318,7 +319,7 @@ public: * @param isDate true for date, false for time. * @param resolution the the resolution in minutes for time types, or 1. */ - DateTimeDataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, + DateTimeDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, const bool isDate, const short resolution) : DataType(id, bitCount, flags, replacement), m_isDate(isDate), m_resolution(resolution) {} @@ -380,7 +381,7 @@ public: * @param maxValue the maximum raw value. * @param divisor the divisor (negative for reciprocal). */ - NumberDataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, + NumberDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, const unsigned int minValue, const unsigned int maxValue, const int divisor) : DataType(id, bitCount, flags, replacement), m_minValue(minValue), m_maxValue(maxValue), m_divisor(divisor), m_precision(calcPrecision(divisor)), m_firstBit(0), m_baseType(NULL) {} @@ -393,7 +394,7 @@ public: * @param firstBit the offset to the first bit. * @param divisor the divisor (negative for reciprocal). */ - NumberDataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, + NumberDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, const short firstBit, const int divisor) : DataType(id, bitCount, flags, replacement), m_minValue(0), m_maxValue((1<::iterator& begin, const vector::iterator end, vector< vector >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix, @@ -563,6 +570,9 @@ private: * Note: adjustable length types are stored by ID only. */ map m_typesByIdLength; + /** the @a DataType instances to cleanup. */ + list m_cleanupTypes; + /** the singleton instance. */ static DataTypeList s_instance;