1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00

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 <bartosz.golaszewski@linaro.org>
This commit is contained in:
Bartosz Golaszewski
2023-01-26 13:24:39 +01:00
parent 39b2d3e152
commit 78020eae9d
5 changed files with 46 additions and 59 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -18,8 +18,9 @@
namespace gpiod {
template<class cxx_enum_type, class c_enum_type> cxx_enum_type
map_enum_c_to_cxx(c_enum_type value, const ::std::map<c_enum_type, cxx_enum_type>& mapping)
template<class cxx_enum_type, class c_enum_type>
cxx_enum_type get_mapped_value(c_enum_type value,
const ::std::map<c_enum_type, cxx_enum_type>& mapping)
{
try {
return mapping.at(value);

View File

@@ -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

View File

@@ -95,31 +95,17 @@ line_settings_ptr copy_line_settings(const line_settings_ptr& ptr)
return settings;
}
template<class key_type, class value_type, class exception_type>
value_type map_setting(const key_type& key, const ::std::map<key_type, value_type>& 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<class cxx_enum_type, class c_enum_type>
c_enum_type do_map_value(cxx_enum_type value, const ::std::map<cxx_enum_type, c_enum_type>& mapping)
{
return map_setting<cxx_enum_type, c_enum_type, ::std::invalid_argument>(value, mapping);
try {
return get_mapped_value(value, mapping);
} catch (const bad_mapping& ex) {
throw ::std::invalid_argument(ex.what());
}
template<class cxx_enum_type, class c_enum_type, int set_func(::gpiod_line_settings*, c_enum_type)>
void set_mapped_value(::gpiod_line_settings* settings, cxx_enum_type value,
const ::std::map<cxx_enum_type, c_enum_type>& mapping)
void set_mapped_prop(::gpiod_line_settings* settings, cxx_enum_type value,
const ::std::map<cxx_enum_type, c_enum_type>& 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<class cxx_enum_type, class c_enum_type, c_enum_type get_func(::gpiod_line_settings*)>
cxx_enum_type get_mapped_value(::gpiod_line_settings* settings,
const ::std::map<c_enum_type, cxx_enum_type>& mapping)
cxx_enum_type get_mapped_prop(::gpiod_line_settings* settings,
const ::std::map<c_enum_type, cxx_enum_type>& 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<line::direction, ::gpiod_line_direction,
::gpiod_line_settings_set_direction>(this->_m_priv->settings.get(),
direction, direction_mapping);
set_mapped_prop<line::direction, ::gpiod_line_direction,
::gpiod_line_settings_set_direction>(this->_m_priv->settings.get(),
direction, direction_mapping);
return *this;
}
GPIOD_CXX_API line::direction line_settings::direction() const
{
return get_mapped_value<line::direction, ::gpiod_line_direction,
::gpiod_line_settings_get_direction>(
return get_mapped_prop<line::direction, ::gpiod_line_direction,
::gpiod_line_settings_get_direction>(
this->_m_priv->settings.get(),
reverse_direction_mapping);
}
GPIOD_CXX_API line_settings& line_settings::set_edge_detection(line::edge edge)
{
set_mapped_value<line::edge, ::gpiod_line_edge,
::gpiod_line_settings_set_edge_detection>(this->_m_priv->settings.get(),
edge, edge_mapping);
set_mapped_prop<line::edge, ::gpiod_line_edge,
::gpiod_line_settings_set_edge_detection>(this->_m_priv->settings.get(),
edge, edge_mapping);
return *this;
}
GPIOD_CXX_API line::edge line_settings::edge_detection() const
{
return get_mapped_value<line::edge, ::gpiod_line_edge,
::gpiod_line_settings_get_edge_detection>(
return get_mapped_prop<line::edge, ::gpiod_line_edge,
::gpiod_line_settings_get_edge_detection>(
this->_m_priv->settings.get(),
reverse_edge_mapping);
}
GPIOD_CXX_API line_settings& line_settings::set_bias(line::bias bias)
{
set_mapped_value<line::bias, ::gpiod_line_bias,
::gpiod_line_settings_set_bias>(this->_m_priv->settings.get(),
bias, bias_mapping);
set_mapped_prop<line::bias, ::gpiod_line_bias,
::gpiod_line_settings_set_bias>(this->_m_priv->settings.get(),
bias, bias_mapping);
return *this;
}
GPIOD_CXX_API line::bias line_settings::bias() const
{
return get_mapped_value<line::bias, ::gpiod_line_bias,
::gpiod_line_settings_get_bias>(this->_m_priv->settings.get(),
return get_mapped_prop<line::bias, ::gpiod_line_bias,
::gpiod_line_settings_get_bias>(this->_m_priv->settings.get(),
reverse_bias_mapping);
}
GPIOD_CXX_API line_settings& line_settings::set_drive(line::drive drive)
{
set_mapped_value<line::drive, ::gpiod_line_drive,
::gpiod_line_settings_set_drive>(this->_m_priv->settings.get(),
drive, drive_mapping);
set_mapped_prop<line::drive, ::gpiod_line_drive,
::gpiod_line_settings_set_drive>(this->_m_priv->settings.get(),
drive, drive_mapping);
return *this;
}
GPIOD_CXX_API line::drive line_settings::drive() const
{
return get_mapped_value<line::drive, ::gpiod_line_drive,
::gpiod_line_settings_get_drive>(this->_m_priv->settings.get(),
reverse_drive_mapping);
return get_mapped_prop<line::drive, ::gpiod_line_drive,
::gpiod_line_settings_get_drive>(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<line::clock, ::gpiod_line_clock,
::gpiod_line_settings_set_event_clock>(this->_m_priv->settings.get(),
event_clock, clock_mapping);
set_mapped_prop<line::clock, ::gpiod_line_clock,
::gpiod_line_settings_set_event_clock>(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<line::clock, ::gpiod_line_clock,
::gpiod_line_settings_get_event_clock>(
return get_mapped_prop<line::clock, ::gpiod_line_clock,
::gpiod_line_settings_get_event_clock>(
this->_m_priv->settings.get(),
reverse_clock_mapping);
}
GPIOD_CXX_API line_settings& line_settings::set_output_value(line::value value)
{
set_mapped_value<line::value, ::gpiod_line_value,
::gpiod_line_settings_set_output_value>(this->_m_priv->settings.get(),
value, value_mapping);
set_mapped_prop<line::value, ::gpiod_line_value,
::gpiod_line_settings_set_output_value>(this->_m_priv->settings.get(),
value, value_mapping);
return *this;
}
GPIOD_CXX_API line::value line_settings::output_value() const
{
return get_mapped_value<line::value, ::gpiod_line_value,
::gpiod_line_settings_get_output_value>(
return get_mapped_prop<line::value, ::gpiod_line_value,
::gpiod_line_settings_get_output_value>(
this->_m_priv->settings.get(),
reverse_value_mapping);
}