From 5799bdba7b086f33c27ba394a8c5fcefac33c633 Mon Sep 17 00:00:00 2001 From: Eran Date: Sun, 24 Sep 2023 14:50:49 +0300 Subject: [PATCH] join context callback handlers together --- src/context.cpp | 27 +++++---------------------- src/context.h | 2 -- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/src/context.cpp b/src/context.cpp index 96e0694614..e15e3e2ceb 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -19,7 +19,6 @@ namespace librealsense context::context( json const & settings ) : _settings( settings ) , _device_mask( rsutils::json::get< unsigned >( settings, "device-mask", RS2_PRODUCT_LINE_ANY ) ) - , _devices_changed_callback( nullptr, []( rs2_devices_changed_callback * ) {} ) { static bool version_logged = false; if( ! version_logged ) @@ -106,31 +105,13 @@ namespace librealsense kvp.second->on_devices_changed( new rs2_device_list( { shared_from_this(), rs2_devices_info_removed } ), new rs2_device_list( { shared_from_this(), rs2_devices_info_added } ) ); } - catch( ... ) - { - LOG_ERROR( "Exception thrown from user callback handler" ); - } - } - - raise_devices_changed( rs2_devices_info_removed, rs2_devices_info_added ); - } - - void context::raise_devices_changed(const std::vector& removed, const std::vector& added) - { - if (_devices_changed_callback) - { - try - { - _devices_changed_callback->on_devices_changed(new rs2_device_list({ shared_from_this(), removed }), - new rs2_device_list({ shared_from_this(), added })); - } catch( std::exception const & e ) { LOG_ERROR( "Exception thrown from user callback handler: " << e.what() ); } - catch (...) + catch( ... ) { - LOG_ERROR("Exception thrown from user callback handler"); + LOG_ERROR( "Exception thrown from user callback handler" ); } } } @@ -153,7 +134,9 @@ namespace librealsense void context::set_devices_changed_callback(devices_changed_callback_ptr callback) { std::lock_guard lock(_devices_changed_callbacks_mtx); - _devices_changed_callback = std::move(callback); + // unique_id::generate_id() will never be 0; so we use 0 for the "public" callback so it'll get overriden on + // subsequent calls + _devices_changed_callbacks[0] = std::move( callback ); } diff --git a/src/context.h b/src/context.h index fef0d444ea..2a5fbc1646 100644 --- a/src/context.h +++ b/src/context.h @@ -76,7 +76,6 @@ namespace librealsense private: void invoke_devices_changed_callbacks( std::vector & rs2_devices_info_removed, std::vector & rs2_devices_info_added ); - void raise_devices_changed(const std::vector& removed, const std::vector& added); std::map< std::string, std::weak_ptr< device_info > > _user_devices; std::map _devices_changed_callbacks; @@ -86,7 +85,6 @@ namespace librealsense std::vector< std::shared_ptr< device_factory > > _factories; - devices_changed_callback_ptr _devices_changed_callback; std::mutex _devices_changed_callbacks_mtx; };