From 2a63bbf13f969a7aaa4c030e693acc1835416847 Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Sun, 8 Dec 2024 07:24:26 -0500 Subject: [PATCH] simplified initialization and cleanup. --- api/icsneo/icsneo.cpp | 35 +++++++++++++++++++++++++---------- include/icsneo/icsneo.h | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/api/icsneo/icsneo.cpp b/api/icsneo/icsneo.cpp index e133da4..5431c01 100644 --- a/api/icsneo/icsneo.cpp +++ b/api/icsneo/icsneo.cpp @@ -15,14 +15,34 @@ using namespace icsneo; typedef struct icsneo_device_t { std::shared_ptr device; + // Received messages from the device, we can automatically free them without the user. std::vector> messages; + // Seperate buffer for transmit messages for simplicity. User is responsible for freeing. + std::vector> tx_messages; std::vector events; icsneo_open_options_t options; + + icsneo_device_t(std::shared_ptr& device, icsneo_open_options_t options) : device(device), options(options) {} + + // Take care of cleaning up the device buffers like when closing the device + void clean_up() { + messages.clear(); + messages.shrink_to_fit(); + events.clear(); + events.shrink_to_fit(); + tx_messages.clear(); + tx_messages.shrink_to_fit(); + } } icsneo_device_t; +// Any new members to this struct should be initialized in icsneo_device_get_messages() typedef struct icsneo_message_t { std::shared_ptr message; + // Indicates this message is a transmit message and should be in the tx_messages vector + bool is_tx; + + icsneo_message_t(std::shared_ptr& message, bool is_tx) : message(message), is_tx(is_tx) {} } icsneo_message_t; typedef struct icsneo_event_t { @@ -113,10 +133,9 @@ ICSNEO_API icsneo_error_t icsneo_device_find_all(icsneo_device_t** devices, uint [&](const auto& device) { return device->device == found_device; })) { - auto device = std::make_shared(); - device->device = found_device; - device->options = icsneo_open_options_go_online | icsneo_open_options_enable_message_polling | icsneo_open_options_sync_rtc | icsneo_open_options_enable_auto_update; - g_devices.push_back(device); + auto default_options = icsneo_open_options_go_online | icsneo_open_options_enable_message_polling | icsneo_open_options_sync_rtc | icsneo_open_options_enable_auto_update; + auto device = std::make_shared(found_device, default_options); + g_devices.push_back(device); } } // Determine how many we can return to the caller @@ -201,10 +220,7 @@ ICSNEO_API icsneo_error_t icsneo_device_close(icsneo_device_t* device) { } dev->close(); // Clear out old messages and events - device->messages.clear(); - device->messages.shrink_to_fit(); - device->events.clear(); - device->events.shrink_to_fit(); + device->clean_up(); return icsneo_error_success; } @@ -381,8 +397,7 @@ ICSNEO_API icsneo_error_t icsneo_device_get_messages(icsneo_device_t* device, ic // Copy the messages into our device message container device->messages.clear(); for (auto& message : queried_messages) { - auto message_t = std::make_shared(); - message_t->message = message; + auto message_t = std::make_shared(message, false); device->messages.push_back(message_t); } device->messages.shrink_to_fit(); diff --git a/include/icsneo/icsneo.h b/include/icsneo/icsneo.h index e2dc7d8..0ff4bd7 100644 --- a/include/icsneo/icsneo.h +++ b/include/icsneo/icsneo.h @@ -170,7 +170,7 @@ ICSNEO_API icsneo_error_t icsneo_device_open(icsneo_device_t* device); /** @brief Close a connection to a previously opened device. * * After a successful call to icsneo_device_open(), this function must be called to close the device. - * An already closed device will still succeed. + * An already closed device will still succeed. All messages and events related to the device will be freed. * * @param[in] icsneo_device_t device The device to close. *