String tag mapping housekeeping

This commit is contained in:
elecpower
2021-12-31 10:53:10 +01:00
committed by Raphael Coeffic
parent 19a9259d96
commit 340b779c8b
5 changed files with 48 additions and 229 deletions
+23
View File
@@ -68,3 +68,26 @@ QString DataHelpers::timeToString(const int value, const unsigned int mask)
result.append(QString("%1:%2").arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0'))); result.append(QString("%1:%2").arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));
return result; return result;
} }
int DataHelpers::getStringTagMappingIndex(const StringTagMappingTable& lut, const char * tag)
{
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string DataHelpers::getStringTagMappingTag(const StringTagMappingTable& lut, unsigned int index)
{
if (index < lut.size())
return lut[index].tag;
return std::string();
}
+15 -1
View File
@@ -49,6 +49,19 @@ struct StringTagMapping {
typedef std::vector<StringTagMapping> StringTagMappingTable; typedef std::vector<StringTagMapping> StringTagMappingTable;
#define STRINGTAGMAPPINGFUNCS_HELPER(tbl, name, index, tag) \
inline int name##index (const char * tag) \
{ \
return DataHelpers::getStringTagMappingIndex(tbl, tag); \
} \
\
inline std::string name##tag (unsigned int index) \
{ \
return DataHelpers::getStringTagMappingTag(tbl, index); \
}
#define STRINGTAGMAPPINGFUNCS(tbl, name) STRINGTAGMAPPINGFUNCS_HELPER(tbl, get##name, Index, Tag)
class FieldRange class FieldRange
{ {
public: public:
@@ -92,5 +105,6 @@ namespace DataHelpers
QString boolToString(const bool value, const BoolFormat format); QString boolToString(const bool value, const BoolFormat format);
QString getElementName(const QString & prefix, const unsigned int index, const char * name = 0, const bool padding = false); QString getElementName(const QString & prefix, const unsigned int index, const char * name = 0, const bool padding = false);
QString timeToString(const int value, const unsigned int mask); QString timeToString(const int value, const unsigned int mask);
int getStringTagMappingIndex(const StringTagMappingTable& lut, const char * tag);
std::string getStringTagMappingTag(const StringTagMappingTable& lut, unsigned int index);
} }
@@ -68,7 +68,6 @@ Node convert<YamlCalibData>::encode(const YamlCalibData& rhs)
{ {
Node node; Node node;
int idx = 0; int idx = 0;
// TODO: for efficiency construct once at an outer level possibly add to Firmware?
const auto* calibIdxLut = getCurrentFirmware()->getAnalogIndexNamesLookupTable(); const auto* calibIdxLut = getCurrentFirmware()->getAnalogIndexNamesLookupTable();
for (const auto& kv : *calibIdxLut) { for (const auto& kv : *calibIdxLut) {
node[kv.tag] = rhs.calib[idx++]; node[kv.tag] = rhs.calib[idx++];
-183
View File
@@ -178,186 +178,3 @@ void Firmware::addOptionsGroup(const OptionsGroup & options)
{ {
this->opts.append(options); this->opts.append(options);
} }
int Firmware::getAnalogInputIndex(const char * tag)
{
const StringTagMappingTable& lut = analogInputNamesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getAnalogInputTag(unsigned int index)
{
const StringTagMappingTable& lut = analogInputNamesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getSwitchesIndex(const char * tag)
{
const StringTagMappingTable& lut = switchesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getSwitchesTag(unsigned int index)
{
const StringTagMappingTable& lut = switchesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getTrimSwitchesIndex(const char * tag)
{
const StringTagMappingTable& lut = trimSwitchesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getTrimSwitchesTag(unsigned int index)
{
const StringTagMappingTable& lut = trimSwitchesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getTrimSourcesIndex(const char * tag)
{
const StringTagMappingTable& lut = trimSourcesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getTrimSourcesTag(unsigned int index)
{
const StringTagMappingTable& lut = trimSourcesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getRawSwitchTypesIndex(const char * tag)
{
const StringTagMappingTable& lut = rawSwitchTypesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getRawSwitchTypesTag(unsigned int index)
{
const StringTagMappingTable& lut = rawSwitchTypesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getRawSourceSpecialTypesIndex(const char * tag)
{
const StringTagMappingTable& lut = rawSourceSpecialTypesLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getRawSourceSpecialTypesTag(unsigned int index)
{
const StringTagMappingTable& lut = rawSourceSpecialTypesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getRawSourceCyclicIndex(const char * tag)
{
const StringTagMappingTable& lut = rawSourceCyclicLookupTable;
auto it =
find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) {
if (elmt.tag == tag) return true;
return false;
});
if (it != lut.end()) {
return it - lut.begin();
}
return -1;
}
std::string Firmware::getRawSourceCyclicTag(unsigned int index)
{
const StringTagMappingTable& lut = rawSourceCyclicLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
+10 -44
View File
@@ -411,50 +411,13 @@ class Firmware
return &analogInputNamesLookupTable; return &analogInputNamesLookupTable;
} }
const StringTagMappingTable* getSwitchesLookupTable() STRINGTAGMAPPINGFUNCS(analogInputNamesLookupTable, AnalogInput);
{ STRINGTAGMAPPINGFUNCS(switchesLookupTable, Switches);
return &switchesLookupTable; STRINGTAGMAPPINGFUNCS(trimSwitchesLookupTable, TrimSwitches);
} STRINGTAGMAPPINGFUNCS(trimSourcesLookupTable, TrimSources);
STRINGTAGMAPPINGFUNCS(rawSwitchTypesLookupTable, RawSwitchTypes);
const StringTagMappingTable* getTrimSwitchesLookupTable() STRINGTAGMAPPINGFUNCS(rawSourceSpecialTypesLookupTable, RawSourceSpecialTypes);
{ STRINGTAGMAPPINGFUNCS(rawSourceCyclicLookupTable, RawSourceCyclic);
return &trimSwitchesLookupTable;
}
const StringTagMappingTable* getTrimSourcesLookupTable()
{
return &trimSourcesLookupTable;
}
const StringTagMappingTable* getRawSwitchTypesLookupTable()
{
return &rawSwitchTypesLookupTable;
}
const StringTagMappingTable* getRawSourceSpecialTypesLookupTable()
{
return &rawSourceSpecialTypesLookupTable;
}
const StringTagMappingTable* getRawSourceCyclicLookupTable()
{
return &rawSourceCyclicLookupTable;
}
int getAnalogInputIndex(const char * tag);
std::string getAnalogInputTag(unsigned int index);
int getSwitchesIndex(const char * tag);
std::string getSwitchesTag(unsigned int index);
int getTrimSwitchesIndex(const char * tag);
std::string getTrimSwitchesTag(unsigned int index);
int getTrimSourcesIndex(const char * tag);
std::string getTrimSourcesTag(unsigned int index);
int getRawSwitchTypesIndex(const char * tag);
std::string getRawSwitchTypesTag(unsigned int index);
int getRawSourceSpecialTypesIndex(const char * tag);
std::string getRawSourceSpecialTypesTag(unsigned int index);
int getRawSourceCyclicIndex(const char * tag);
std::string getRawSourceCyclicTag(unsigned int index);
protected: protected:
QString id; QString id;
@@ -463,6 +426,8 @@ class Firmware
unsigned int variantBase; unsigned int variantBase;
Firmware * base; Firmware * base;
EEPROMInterface * eepromInterface; EEPROMInterface * eepromInterface;
// used by YAML encode and decode
const StringTagMappingTable analogInputNamesLookupTable; const StringTagMappingTable analogInputNamesLookupTable;
const StringTagMappingTable switchesLookupTable; const StringTagMappingTable switchesLookupTable;
const StringTagMappingTable trimSwitchesLookupTable; const StringTagMappingTable trimSwitchesLookupTable;
@@ -470,6 +435,7 @@ class Firmware
const StringTagMappingTable rawSwitchTypesLookupTable; const StringTagMappingTable rawSwitchTypesLookupTable;
const StringTagMappingTable rawSourceSpecialTypesLookupTable; const StringTagMappingTable rawSourceSpecialTypesLookupTable;
const StringTagMappingTable rawSourceCyclicLookupTable; const StringTagMappingTable rawSourceCyclicLookupTable;
QList<const char *> languages; QList<const char *> languages;
//QList<const char *> ttslanguages; //QList<const char *> ttslanguages;
OptionsList opts; OptionsList opts;