From fadea413cdb4644b821369b27fad6e5f019d32c2 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 21 Oct 2020 15:25:18 +0200 Subject: devices: return index from function adding / removing devices This will be used to keep the model representing the device-list up to date. Signed-off-by: Berthold Stoeger --- core/device.cpp | 13 ++++++++++--- core/device.h | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/device.cpp b/core/device.cpp index 512d17d57..764531414 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -299,17 +299,24 @@ extern "C" void create_device_node(struct device_table *device_table, const char } /* Does not check for duplicates! */ -extern "C" void add_to_device_table(struct device_table *device_table, const struct device *dev) +extern "C" int add_to_device_table(struct device_table *device_table, const struct device *dev) { auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev); + int idx = it - device_table->devices.begin(); device_table->devices.insert(it, *dev); + return idx; } -extern "C" void remove_device(struct device_table *device_table, const struct device *dev) +extern "C" int remove_device(struct device_table *device_table, const struct device *dev) { auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev); - if (it != device_table->devices.end() && same_device(*it, *dev)) + if (it != device_table->devices.end() && same_device(*it, *dev)) { + int idx = it - device_table->devices.begin(); device_table->devices.erase(it); + return idx; + } else { + return -1; + } } extern "C" void clear_device_table(struct device_table *device_table) diff --git a/core/device.h b/core/device.h index 9c7fff31c..590609c45 100644 --- a/core/device.h +++ b/core/device.h @@ -29,8 +29,8 @@ extern bool device_used_by_selected_dive(const struct device *dev); extern const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc); extern bool device_exists(const struct device_table *table, const struct device *dev); -extern void add_to_device_table(struct device_table *table, const struct device *dev); -extern void remove_device(struct device_table *table, const struct device *dev); +extern int add_to_device_table(struct device_table *table, const struct device *dev); // returns index +extern int remove_device(struct device_table *table, const struct device *dev); // returns index or -1 if not found // struct device accessors for C-code. The returned strings are not stable! const char *device_get_model(const struct device *dev); -- cgit v1.2.3-70-g09d2