subsurface/core/device.h
Berthold Stoeger fd8bd9d5c7 cleanup: use std::string in struct device
struct device is a core data structure and therefore shouldn't use QString.
QString stores as UTF-16 (which is a very questionable choice in itself).
However, the real problem is that this puts us in lifetime-management
hell when interfacing with C code: The UTF-16 has to be converted to
UTF-8, but when returning such a string, this puts burden on the caller
who has to free it. In fact, instead of looping over devices from C-code
we had a callback that sent down temporary C-strings with qPrintable.

In contrast, std::string is guaranteed to store its data as
contiguous null-terminated and C-compatible strings. Therefore,
replace the QString by std::string. Keep the QString just in
one place that formats a hexadecimal number to avoid any
potential change.

The disadvantage of using std::string is that it will crash
when constructed with a NULL argument, consistent with C-style
functions such as strcmp, etc. Arguably, NULL is different
from the empty string even though we treat both as the same.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-10-16 14:26:37 -07:00

52 lines
1.4 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#ifndef DEVICE_H
#define DEVICE_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct divecomputer;
extern void fake_dc(struct divecomputer *dc);
extern void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid);
extern void set_dc_nickname(struct dive *dive);
extern void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname);
extern void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t,
const char *, const char *, const char *), bool select_only);
extern void clear_device_nodes();
const char *get_dc_nickname(const struct divecomputer *dc);
#ifdef __cplusplus
}
#endif
// Functions and global variables that are only available to C++ code
#ifdef __cplusplus
#include <string>
#include <QVector>
struct device {
bool operator==(const device &a) const;
bool operator!=(const device &a) const;
bool operator<(const device &a) const;
void showchanges(const std::string &n, const std::string &s, const std::string &f) const;
std::string model;
uint32_t deviceId;
std::string serialNumber;
std::string firmware;
std::string nickName;
};
struct device_table {
// Keep the dive computers in a vector sorted by (model, deviceId)
QVector<device> devices;
};
extern struct device_table device_table;
#endif
#endif // DEVICE_H