From 78020eae9d28cba9c4790ccfd6bdeb93be633706 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Thu, 26 Jan 2023 13:24:39 +0100 Subject: [PATCH] bindings: cxx: remove duplicate mapping helper We have two helpers that do the same thing: given a key and a map, they return the associated value or throw an exception if the map doesn't contain it. The difference is just in the type of the exception. Remove one and reuse the other. While at it: modify the names a bit for better readability - especially the mapping function which is not limited to just mapping enum types. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/edge-event.cpp | 2 +- bindings/cxx/info-event.cpp | 2 +- bindings/cxx/internal.hpp | 5 +- bindings/cxx/line-info.cpp | 2 +- bindings/cxx/line-settings.cpp | 94 +++++++++++++++------------------- 5 files changed, 46 insertions(+), 59 deletions(-) diff --git a/bindings/cxx/edge-event.cpp b/bindings/cxx/edge-event.cpp index 4de57a5..4fa3399 100644 --- a/bindings/cxx/edge-event.cpp +++ b/bindings/cxx/edge-event.cpp @@ -100,7 +100,7 @@ GPIOD_CXX_API edge_event::event_type edge_event::type() const { int evtype = ::gpiod_edge_event_get_event_type(this->_m_priv->get_event_ptr()); - return map_enum_c_to_cxx(evtype, event_type_mapping); + return get_mapped_value(evtype, event_type_mapping); } GPIOD_CXX_API timestamp edge_event::timestamp_ns() const noexcept diff --git a/bindings/cxx/info-event.cpp b/bindings/cxx/info-event.cpp index 37a3fed..0b19ee9 100644 --- a/bindings/cxx/info-event.cpp +++ b/bindings/cxx/info-event.cpp @@ -77,7 +77,7 @@ GPIOD_CXX_API info_event::event_type info_event::type() const { int type = ::gpiod_info_event_get_event_type(this->_m_priv->event.get()); - return map_enum_c_to_cxx(type, event_type_mapping); + return get_mapped_value(type, event_type_mapping); } GPIOD_CXX_API ::std::uint64_t info_event::timestamp_ns() const noexcept diff --git a/bindings/cxx/internal.hpp b/bindings/cxx/internal.hpp index 8322e12..6902495 100644 --- a/bindings/cxx/internal.hpp +++ b/bindings/cxx/internal.hpp @@ -18,8 +18,9 @@ namespace gpiod { -template cxx_enum_type -map_enum_c_to_cxx(c_enum_type value, const ::std::map& mapping) +template +cxx_enum_type get_mapped_value(c_enum_type value, + const ::std::map& mapping) { try { return mapping.at(value); diff --git a/bindings/cxx/line-info.cpp b/bindings/cxx/line-info.cpp index 4e70b29..9931ed0 100644 --- a/bindings/cxx/line-info.cpp +++ b/bindings/cxx/line-info.cpp @@ -114,7 +114,7 @@ GPIOD_CXX_API line::direction line_info::direction() const { int direction = ::gpiod_line_info_get_direction(this->_m_priv->info.get()); - return map_enum_c_to_cxx(direction, direction_mapping); + return get_mapped_value(direction, direction_mapping); } GPIOD_CXX_API bool line_info::active_low() const noexcept diff --git a/bindings/cxx/line-settings.cpp b/bindings/cxx/line-settings.cpp index 2159062..272a848 100644 --- a/bindings/cxx/line-settings.cpp +++ b/bindings/cxx/line-settings.cpp @@ -95,31 +95,17 @@ line_settings_ptr copy_line_settings(const line_settings_ptr& ptr) return settings; } -template -value_type map_setting(const key_type& key, const ::std::map& mapping) -{ - value_type ret; - - try { - ret = mapping.at(key); - } catch (const ::std::out_of_range& err) { - /* FIXME Demangle the name. */ - throw exception_type(::std::string("invalid value for ") + - typeid(key_type).name()); - } - - return ret; -} - template c_enum_type do_map_value(cxx_enum_type value, const ::std::map& mapping) -{ - return map_setting(value, mapping); +try { + return get_mapped_value(value, mapping); +} catch (const bad_mapping& ex) { + throw ::std::invalid_argument(ex.what()); } template -void set_mapped_value(::gpiod_line_settings* settings, cxx_enum_type value, - const ::std::map& mapping) +void set_mapped_prop(::gpiod_line_settings* settings, cxx_enum_type value, + const ::std::map& mapping) { c_enum_type mapped_val = do_map_value(value, mapping); @@ -129,12 +115,12 @@ void set_mapped_value(::gpiod_line_settings* settings, cxx_enum_type value, } template -cxx_enum_type get_mapped_value(::gpiod_line_settings* settings, - const ::std::map& mapping) +cxx_enum_type get_mapped_prop(::gpiod_line_settings* settings, + const ::std::map& mapping) { auto mapped_val = get_func(settings); - return map_enum_c_to_cxx(mapped_val, mapping); + return get_mapped_value(mapped_val, mapping); } } /* namespace */ @@ -202,68 +188,68 @@ GPIOD_CXX_API line_settings& line_settings::reset(void) noexcept GPIOD_CXX_API line_settings& line_settings::set_direction(line::direction direction) { - set_mapped_value(this->_m_priv->settings.get(), - direction, direction_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + direction, direction_mapping); return *this; } GPIOD_CXX_API line::direction line_settings::direction() const { - return get_mapped_value( + return get_mapped_prop( this->_m_priv->settings.get(), reverse_direction_mapping); } GPIOD_CXX_API line_settings& line_settings::set_edge_detection(line::edge edge) { - set_mapped_value(this->_m_priv->settings.get(), - edge, edge_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + edge, edge_mapping); return *this; } GPIOD_CXX_API line::edge line_settings::edge_detection() const { - return get_mapped_value( + return get_mapped_prop( this->_m_priv->settings.get(), reverse_edge_mapping); } GPIOD_CXX_API line_settings& line_settings::set_bias(line::bias bias) { - set_mapped_value(this->_m_priv->settings.get(), - bias, bias_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + bias, bias_mapping); return *this; } GPIOD_CXX_API line::bias line_settings::bias() const { - return get_mapped_value(this->_m_priv->settings.get(), + return get_mapped_prop(this->_m_priv->settings.get(), reverse_bias_mapping); } GPIOD_CXX_API line_settings& line_settings::set_drive(line::drive drive) { - set_mapped_value(this->_m_priv->settings.get(), - drive, drive_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + drive, drive_mapping); return *this; } GPIOD_CXX_API line::drive line_settings::drive() const { - return get_mapped_value(this->_m_priv->settings.get(), - reverse_drive_mapping); + return get_mapped_prop(this->_m_priv->settings.get(), + reverse_drive_mapping); } GPIOD_CXX_API line_settings& line_settings::set_active_low(bool active_low) @@ -294,34 +280,34 @@ GPIOD_CXX_API ::std::chrono::microseconds line_settings::debounce_period() const GPIOD_CXX_API line_settings& line_settings::set_event_clock(line::clock event_clock) { - set_mapped_value(this->_m_priv->settings.get(), - event_clock, clock_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + event_clock, clock_mapping); return *this; } GPIOD_CXX_API line::clock line_settings::event_clock() const { - return get_mapped_value( + return get_mapped_prop( this->_m_priv->settings.get(), reverse_clock_mapping); } GPIOD_CXX_API line_settings& line_settings::set_output_value(line::value value) { - set_mapped_value(this->_m_priv->settings.get(), - value, value_mapping); + set_mapped_prop(this->_m_priv->settings.get(), + value, value_mapping); return *this; } GPIOD_CXX_API line::value line_settings::output_value() const { - return get_mapped_value( + return get_mapped_prop( this->_m_priv->settings.get(), reverse_value_mapping); } -- 2.30.2