From: Breuninger Matthias (ETAS-ICA/XPC-Fe3) Date: Mon, 2 Feb 2026 20:05:48 +0000 (+0100) Subject: update: Remove ctx pointers from C++ wrapper change callbacks X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8a7384ea8e5fd83e025d52677f98f5ff53d4ef59;p=thirdparty%2Flldpd.git update: Remove ctx pointers from C++ wrapper change callbacks Instead, use lambdas if private contexts are required. They are way more C++-idiomatic. --- diff --git a/src/lib/lldpctl.hpp b/src/lib/lldpctl.hpp index fd36f970..1baba1ae 100644 --- a/src/lib/lldpctl.hpp +++ b/src/lib/lldpctl.hpp @@ -460,34 +460,23 @@ class LldpCtl { /** * @brief Wrapper for change callback registration. - * - * @tparam X Context pointer type for the general optional callback passed to the - * constructor. - * @tparam Y Context pointer type for the interface specific callbacks passed to @ref - * RegisterInterfaceCallback. */ -template class LldpWatch { +class LldpWatch { public: - template using ChangeCallback = const std::function; + const LldpAtom &interface, const LldpAtom &neighbor)>; /** * @brief Construct a new Lldp Watch object. * * @param callback Optional callback to trigger on remote changes. * Additionally, interface specific callbacks can be registered - * using @ref RegisterInterfaceCallback. - * @param ctx Optional context passed to @p callback. + * using @ref RegisterInterfaceCallback. */ explicit LldpWatch( - const std::optional> &callback = std::nullopt, - const X *ctx = nullptr) - : general_callback_(callback.has_value() ? - std::make_optional( - std::make_pair(*callback, const_cast(ctx))) : - std::nullopt) + const std::optional &callback = std::nullopt) + : general_callback_(callback) { if (!conn_) { throw std::system_error(LLDPCTL_ERR_NOMEM, @@ -495,7 +484,7 @@ template class LldpWatch { } CHECK_LLDP_N(::lldpctl_watch_callback2(conn_, - &LldpWatch::WatchCallback, + &LldpWatch::WatchCallback, static_cast(this)), conn_); @@ -529,12 +518,11 @@ template class LldpWatch { * * @param if_name The local interface to monitor. * @param callback Callback to trigger on remote changes. - * @param ctx Optional context passed to @p callback. * @param trigger_init It @p true then @p callback is invoked during * registration for all existing neighbors. */ void RegisterInterfaceCallback(const std::string &if_name, - ChangeCallback callback, const Y *ctx, bool trigger_init = false) + const ChangeCallback &callback, bool trigger_init = false) { const auto interface { LldpCtl().GetInterface(if_name) @@ -561,12 +549,11 @@ template class LldpWatch { for (const auto &neighbor : interface->GetPort().GetAtomList( lldpctl_k_port_neighbors)) { callback(if_name, lldpctl_change_t::lldpctl_c_added, - *interface, neighbor, const_cast(ctx)); + *interface, neighbor); } } - interface_callbacks_.try_emplace(if_name, - std::make_pair(callback, const_cast(ctx))); + interface_callbacks_.try_emplace(if_name, callback); } /** @@ -613,7 +600,7 @@ template class LldpWatch { const auto if_name { *interface_atom.GetValue( lldpctl_k_interface_name) }; - auto self { static_cast *>(p) }; + auto self { static_cast(p) }; /* * Run the callbacks without holding the mutex to avoid a deadlock that occurs when @@ -623,8 +610,8 @@ template class LldpWatch { * We also need to track the number of active callbacks to allow UnregisterInterfaceCallback to wait * for any running callbacks to complete. */ - std::optional, X *>> general_cb; - std::optional, Y *>> interface_cb; + std::optional general_cb; + std::optional interface_cb; { std::unique_lock lock{ self->mutex_ }; @@ -642,13 +629,11 @@ template class LldpWatch { /* Run the callbacks without holding the mutex. */ if (general_cb.has_value()) { - auto [callback, ctx]{ general_cb.value() }; - callback(if_name, change, interface_atom, neighbor_atom, ctx); + (*general_cb)(if_name, change, interface_atom, neighbor_atom); } if (interface_cb.has_value()) { - auto [callback, ctx]{ interface_cb.value() }; - callback(if_name, change, interface_atom, neighbor_atom, ctx); + (*interface_cb)(if_name, change, interface_atom, neighbor_atom); } /* Decrement active callbacks and notify waiting threads. */ @@ -664,9 +649,8 @@ template class LldpWatch { std::mutex mutex_; std::condition_variable cv_; size_t active_callbacks_{ 0 }; - const std::optional, X *>> general_callback_; - std::map, Y *>, std::less<>> - interface_callbacks_; + const std::optional general_callback_; + std::map> interface_callbacks_; }; } // namespace lldpcli