cleanup: make device code more consistent with core

We keep track of device, i.e. distinct dive computers with id in the core.
The corresponding code stuck out like a sore thumb. Firstly, because it
is C++. But more importantly, because it used inconsistent nameing conventions.

Notably it defined a "DiveComputerNode" when this is something very different
from "struct dive_computer", the latter being the dive-computer related
data of a single dive.

Since the whole thing is defined in "device.h" and the function to create
such an entry is called "create_device_node", call the structure "device".
Use snake_case for consistency with the other core structures.

Moreover, call the collection of devices "device_table" in analogy
with "dive_table", etc.

Overall, this should make the core code more consistent style-wise.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-10-03 11:18:42 +02:00 committed by Dirk Hohndel
parent 90ca635316
commit 5bc6f5d36c
4 changed files with 36 additions and 36 deletions

View file

@ -192,9 +192,9 @@ extern "C" void fake_dc(struct divecomputer *dc)
/* Even that didn't work? Give up, there's something wrong */ /* Even that didn't work? Give up, there's something wrong */
} }
DiveComputerList dcList; struct device_table device_table;
bool DiveComputerNode::operator==(const DiveComputerNode &a) const bool device::operator==(const device &a) const
{ {
return model == a.model && return model == a.model &&
deviceId == a.deviceId && deviceId == a.deviceId &&
@ -203,25 +203,25 @@ bool DiveComputerNode::operator==(const DiveComputerNode &a) const
nickName == a.nickName; nickName == a.nickName;
} }
bool DiveComputerNode::operator!=(const DiveComputerNode &a) const bool device::operator!=(const device &a) const
{ {
return !(*this == a); return !(*this == a);
} }
bool DiveComputerNode::operator<(const DiveComputerNode &a) const bool device::operator<(const device &a) const
{ {
return std::tie(model, deviceId) < std::tie(a.model, a.deviceId); return std::tie(model, deviceId) < std::tie(a.model, a.deviceId);
} }
static const DiveComputerNode *getDCExact(const QVector<DiveComputerNode> &dcs, const divecomputer *dc) static const device *getDCExact(const QVector<device> &dcs, const divecomputer *dc)
{ {
auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{dc->model, dc->deviceid, {}, {}, {}}); auto it = std::lower_bound(dcs.begin(), dcs.end(), device{dc->model, dc->deviceid, {}, {}, {}});
return it != dcs.end() && it->model == dc->model && it->deviceId == dc->deviceid ? &*it : NULL; return it != dcs.end() && it->model == dc->model && it->deviceId == dc->deviceid ? &*it : NULL;
} }
static const DiveComputerNode *getDC(const QVector<DiveComputerNode> &dcs, const divecomputer *dc) static const device *getDC(const QVector<device> &dcs, const divecomputer *dc)
{ {
auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{dc->model, 0, {}, {}, {}}); auto it = std::lower_bound(dcs.begin(), dcs.end(), device{dc->model, 0, {}, {}, {}});
return it != dcs.end() && it->model == dc->model ? &*it : NULL; return it != dcs.end() && it->model == dc->model ? &*it : NULL;
} }
@ -240,7 +240,7 @@ extern "C" void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid)
if (!dc->model) if (!dc->model)
return; return;
const DiveComputerNode *node = getDCExact(dcList.dcs, dc); const device *node = getDCExact(device_table.devices, dc);
if (!node) if (!node)
return; return;
@ -250,7 +250,7 @@ extern "C" void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid)
dc->fw_version = copy_qstring(node->firmware); dc->fw_version = copy_qstring(node->firmware);
} }
void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const void device::showchanges(const QString &n, const QString &s, const QString &f) const
{ {
if (nickName != n && !n.isEmpty()) if (nickName != n && !n.isEmpty())
qDebug("new nickname %s for DC model %s deviceId 0x%x", qPrintable(n), qPrintable(model), deviceId); qDebug("new nickname %s for DC model %s deviceId 0x%x", qPrintable(n), qPrintable(model), deviceId);
@ -260,11 +260,11 @@ void DiveComputerNode::showchanges(const QString &n, const QString &s, const QSt
qDebug("new firmware version %s for DC model %s deviceId 0x%x", qPrintable(f), qPrintable(model), deviceId); qDebug("new firmware version %s for DC model %s deviceId 0x%x", qPrintable(f), qPrintable(model), deviceId);
} }
static void addDC(QVector<DiveComputerNode> &dcs, const QString &m, uint32_t d, const QString &n, const QString &s, const QString &f) static void addDC(QVector<device> &dcs, const QString &m, uint32_t d, const QString &n, const QString &s, const QString &f)
{ {
if (m.isEmpty() || d == 0) if (m.isEmpty() || d == 0)
return; return;
auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, d, {}, {}, {}}); auto it = std::lower_bound(dcs.begin(), dcs.end(), device{m, d, {}, {}, {}});
if (it != dcs.end() && it->model == m && it->deviceId == d) { if (it != dcs.end() && it->model == m && it->deviceId == d) {
// debugging: show changes // debugging: show changes
if (verbose) if (verbose)
@ -277,21 +277,21 @@ static void addDC(QVector<DiveComputerNode> &dcs, const QString &m, uint32_t d,
if (!f.isEmpty()) if (!f.isEmpty())
it->firmware = f; it->firmware = f;
} else { } else {
dcs.insert(it, DiveComputerNode{m, d, s, f, n}); dcs.insert(it, device{m, d, s, f, n});
} }
} }
extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname)
{ {
addDC(dcList.dcs, model, deviceid, nickname, serial, firmware); addDC(device_table.devices, model, deviceid, nickname, serial, firmware);
} }
extern "C" void clear_device_nodes() extern "C" void clear_device_nodes()
{ {
dcList.dcs.clear(); device_table.devices.clear();
} }
static bool compareDCById(const DiveComputerNode &a, const DiveComputerNode &b) static bool compareDCById(const device &a, const device &b)
{ {
return a.deviceId < b.deviceId; return a.deviceId < b.deviceId;
} }
@ -299,9 +299,9 @@ static bool compareDCById(const DiveComputerNode &a, const DiveComputerNode &b)
extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char *, uint32_t, const char *, const char *, const char *), extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char *, uint32_t, const char *, const char *, const char *),
bool select_only) bool select_only)
{ {
QVector<DiveComputerNode> values = dcList.dcs; QVector<device> values = device_table.devices;
std::sort(values.begin(), values.end(), compareDCById); std::sort(values.begin(), values.end(), compareDCById);
for (const DiveComputerNode &node : values) { for (const device &node : values) {
bool found = false; bool found = false;
if (select_only) { if (select_only) {
for (dive *d: getDiveSelection()) { for (dive *d: getDiveSelection()) {
@ -343,9 +343,9 @@ extern "C" void set_dc_nickname(struct dive *dive)
for_each_dc (dive, dc) { for_each_dc (dive, dc) {
if (!empty_string(dc->model) && dc->deviceid && if (!empty_string(dc->model) && dc->deviceid &&
!getDCExact(dcList.dcs, dc)) { !getDCExact(device_table.devices, dc)) {
// we don't have this one, yet // we don't have this one, yet
const DiveComputerNode *existNode = getDC(dcList.dcs, dc); const device *existNode = getDC(device_table.devices, dc);
if (existNode) { if (existNode) {
// we already have this model but a different deviceid // we already have this model but a different deviceid
QString simpleNick(dc->model); QString simpleNick(dc->model);
@ -353,9 +353,9 @@ extern "C" void set_dc_nickname(struct dive *dive)
simpleNick.append(" (unknown deviceid)"); simpleNick.append(" (unknown deviceid)");
else else
simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")");
addDC(dcList.dcs, dc->model, dc->deviceid, simpleNick, {}, {}); addDC(device_table.devices, dc->model, dc->deviceid, simpleNick, {}, {});
} else { } else {
addDC(dcList.dcs, dc->model, dc->deviceid, {}, {}, {}); addDC(device_table.devices, dc->model, dc->deviceid, {}, {}, {});
} }
} }
} }
@ -363,7 +363,7 @@ extern "C" void set_dc_nickname(struct dive *dive)
QString get_dc_nickname(const struct divecomputer *dc) QString get_dc_nickname(const struct divecomputer *dc)
{ {
const DiveComputerNode *existNode = getDCExact(dcList.dcs, dc); const device *existNode = getDCExact(device_table.devices, dc);
if (existNode && !existNode->nickName.isEmpty()) if (existNode && !existNode->nickName.isEmpty())
return existNode->nickName; return existNode->nickName;

View file

@ -26,10 +26,10 @@ extern void clear_device_nodes();
#include <QString> #include <QString>
#include <QVector> #include <QVector>
struct DiveComputerNode { struct device {
bool operator==(const DiveComputerNode &a) const; bool operator==(const device &a) const;
bool operator!=(const DiveComputerNode &a) const; bool operator!=(const device &a) const;
bool operator<(const DiveComputerNode &a) const; bool operator<(const device &a) const;
void showchanges(const QString &n, const QString &s, const QString &f) const; void showchanges(const QString &n, const QString &s, const QString &f) const;
QString model; QString model;
uint32_t deviceId; uint32_t deviceId;
@ -38,13 +38,13 @@ struct DiveComputerNode {
QString nickName; QString nickName;
}; };
struct DiveComputerList { struct device_table {
// Keep the dive computers in a vector sorted by (model, deviceId) // Keep the dive computers in a vector sorted by (model, deviceId)
QVector<DiveComputerNode> dcs; QVector<device> devices;
}; };
QString get_dc_nickname(const struct divecomputer *dc); QString get_dc_nickname(const struct divecomputer *dc);
extern DiveComputerList dcList; extern struct device_table device_table;
#endif #endif

View file

@ -4,7 +4,7 @@
#include "core/divelist.h" #include "core/divelist.h"
DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent), DiveComputerModel::DiveComputerModel(QObject *parent) : CleanerTableModel(parent),
dcs(dcList.dcs) dcs(device_table.devices)
{ {
setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname")); setHeaderDataStrings(QStringList() << "" << tr("Model") << tr("Device ID") << tr("Nickname"));
} }
@ -13,7 +13,7 @@ QVariant DiveComputerModel::data(const QModelIndex &index, int role) const
{ {
if (index.row() < 0 || index.row() >= dcs.size()) if (index.row() < 0 || index.row() >= dcs.size())
return QVariant(); return QVariant();
const DiveComputerNode &node = dcs[index.row()]; const device &node = dcs[index.row()];
if (role == Qt::DisplayRole || role == Qt::EditRole) { if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) { switch (index.column()) {
@ -58,7 +58,7 @@ bool DiveComputerModel::setData(const QModelIndex &index, const QVariant &value,
if (index.row() < 0 || index.row() >= dcs.size()) if (index.row() < 0 || index.row() >= dcs.size())
return false; return false;
DiveComputerNode &node = dcs[index.row()]; device &node = dcs[index.row()];
node.nickName = value.toString(); node.nickName = value.toString();
emit dataChanged(index, index); emit dataChanged(index, index);
return true; return true;
@ -75,7 +75,7 @@ void DiveComputerModel::remove(const QModelIndex &index)
void DiveComputerModel::keepWorkingList() void DiveComputerModel::keepWorkingList()
{ {
if (dcList.dcs != dcs) if (device_table.devices != dcs)
mark_divelist_changed(true); mark_divelist_changed(true);
dcList.dcs = dcs; device_table.devices = dcs;
} }

View file

@ -26,7 +26,7 @@ slots:
void remove(const QModelIndex &index); void remove(const QModelIndex &index);
private: private:
QVector<DiveComputerNode> dcs; QVector<device> dcs;
}; };
#endif #endif