Skip to content

Commit

Permalink
hygiene: clang-tidy cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jan 18, 2025
1 parent 2e95206 commit 91b471c
Show file tree
Hide file tree
Showing 40 changed files with 424 additions and 380 deletions.
10 changes: 10 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Checks: '-*,performance-*,clang-*,boost-*,concurrency-*,portability-*,-boost-use-ranges,-performance-enum-size'
CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
- { key: readability-identifier-naming.ClassCase, value: lower_case }
- { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ }
- { key: readability-identifier-naming.StructCase, value: lower_case }
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.GlobalConstantCase, value: lower_case }

7 changes: 7 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Diagnostics:
ClangTidy:
Add: [performance*, boost*, clang*, concurrency*, portability*]
Remove: [modernize*, readability*, llvm-qualified-auto, misc-non-private-member-variables-in-classes, cppcoreguidelines-pro-type-reinterpret-cast, llvmlibc-*, altera-*, misc-no-recursion]
CompileFlags:
Add: -Wno-gnu-zero-variadic-macro-arguments
Remove: [-Wgnu-zero-variadic-macro-arguments, -mno-direct-extern-access]
1 change: 1 addition & 0 deletions include/libremidi/backends/alsa_raw/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <libremidi/config.hpp>

#include <chrono>
#include <cstdint>
#include <functional>
#include <optional>
#include <span>
Expand Down
9 changes: 8 additions & 1 deletion include/libremidi/backends/alsa_raw/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
#include <libremidi/backends/linux/alsa.hpp>
#include <libremidi/config.hpp>
#include <libremidi/detail/observer.hpp>
#include <libremidi/observer_configuration.hpp>

#include <functional>
#include <alsa/asoundlib.h>

#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>

// Credits: greatly inspired from
Expand Down
22 changes: 11 additions & 11 deletions include/libremidi/backends/alsa_raw/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class midi_in_alsa_raw_threaded : public midi_in_impl
midi_in_alsa_raw_threaded(input_configuration&& conf, alsa_raw_input_configuration&& apiconf)
: midi_in_impl{std::move(conf), std::move(apiconf)}
{
if (this->termination_event < 0)
if (this->m_termination_event < 0)
{
libremidi_handle_error(this->configuration, "error creating eventfd.");
return;
Expand All @@ -220,7 +220,7 @@ class midi_in_alsa_raw_threaded : public midi_in_impl
private:
void run_thread(auto parse_func)
{
fds_.push_back(this->termination_event);
fds_.push_back(this->m_termination_event);
const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();
Expand All @@ -233,7 +233,7 @@ class midi_in_alsa_raw_threaded : public midi_in_impl
continue;
else if (err < 0)
return;
else if (termination_event.ready(fds_.back()))
else if (m_termination_event.ready(fds_.back()))
break;

err = do_read_events(parse_func, {fds_.data(), fds_.size() - 1});
Expand All @@ -250,11 +250,11 @@ class midi_in_alsa_raw_threaded : public midi_in_impl
{
if (configuration.timestamps == timestamp_mode::NoTimestamp)
{
this->thread_ = std::thread{[this] { run_thread(&midi_in_impl::read_input_buffer); }};
this->m_thread = std::thread{[this] { run_thread(&midi_in_impl::read_input_buffer); }};
}
else
{
this->thread_ = std::thread{
this->m_thread = std::thread{
[this] { run_thread(&midi_in_impl::read_input_buffer_with_timestamps); }};
}
return stdx::error{};
Expand Down Expand Up @@ -282,16 +282,16 @@ class midi_in_alsa_raw_threaded : public midi_in_impl

stdx::error close_port() override
{
termination_event.notify();
if (thread_.joinable())
thread_.join();
termination_event.consume(); // Reset to zero
m_termination_event.notify();
if (m_thread.joinable())
m_thread.join();
m_termination_event.consume(); // Reset to zero

return midi_in_impl::close_port();
}

std::thread thread_;
eventfd_notifier termination_event{};
std::thread m_thread;
eventfd_notifier m_termination_event{};
};

class midi_in_alsa_raw_manual : public midi_in_impl
Expand Down
76 changes: 38 additions & 38 deletions include/libremidi/backends/alsa_raw/observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ class observer_impl_base
if (!configuration.has_callbacks())
return;

fds[0] = this->udev;
fds[1] = termination_event;
fds[2] = timer_fd;
m_fds[0] = this->m_udev;
m_fds[1] = m_termination_event;
m_fds[2] = m_timer_fd;

// Set-up initial state
if (configuration.notify_in_constructor)
this->check_devices();

// Start thread
thread = std::thread{[this] { this->run(); }};
m_thread = std::thread{[this] { this->run(); }};
}

~observer_impl_base()
{
termination_event.notify();
m_termination_event.notify();

if (thread.joinable())
thread.join();
if (m_thread.joinable())
m_thread.join();
}

std::vector<libremidi::input_port> get_input_ports() const noexcept override
Expand Down Expand Up @@ -82,7 +82,7 @@ class observer_impl_base
{
for (;;)
{
if (int err = poll(fds, 3, -1); err < 0)
if (int err = poll(m_fds, 3, -1); err < 0)
{
if (err == -EAGAIN)
continue;
Expand All @@ -91,49 +91,49 @@ class observer_impl_base
}

// Check udev
if (fds[0].revents & POLLIN)
if (m_fds[0].revents & POLLIN)
{
udev_device* dev = udev.udev.monitor_receive_device(udev.monitor);
udev_device* dev = m_udev.udev.monitor_receive_device(m_udev.monitor);
if (!dev)
continue;

std::string_view act = udev.udev.device_get_action(dev);
std::string_view ss = udev.udev.device_get_subsystem(dev);
std::string_view act = m_udev.udev.device_get_action(dev);
std::string_view ss = m_udev.udev.device_get_subsystem(dev);
if (!act.empty() && ss == "snd_seq")
{
if (act == "add" || act == "remove")
{
// Check every 100 milliseconds for ten seconds
this->timer_fd.restart(configuration.poll_period.count());
timer_check_counts = 100;
this->m_timer_fd.restart(configuration.poll_period.count());
m_timer_check_counts = 100;
}
}

udev.udev.device_unref(dev);
m_udev.udev.device_unref(dev);

fds[0].revents = 0;
m_fds[0].revents = 0;
}

// Check eventfd
if (fds[1].revents & POLLIN)
if (m_fds[1].revents & POLLIN)
{
break;
}

// Check timer
if (fds[2].revents & POLLIN)
if (m_fds[2].revents & POLLIN)
{
if (this->timer_check_counts-- <= 0)
this->timer_fd.cancel();
fds[2].revents = 0;
if (this->m_timer_check_counts-- <= 0)
this->m_timer_fd.cancel();
m_fds[2].revents = 0;

check_devices();
}
}
}

template <bool Input>
auto to_port_info(alsa_raw::alsa_raw_port_info p) const noexcept
auto to_port_info(const alsa_raw::alsa_raw_port_info& p) const noexcept
-> std::conditional_t<Input, input_port, output_port>
{
return {
Expand All @@ -151,7 +151,7 @@ class observer_impl_base

new_devs.enumerate_cards();

for (auto& in_prev : current_inputs)
for (auto& in_prev : m_current_inputs)
{
if (auto it = std::find(new_devs.inputs.begin(), new_devs.inputs.end(), in_prev);
it == new_devs.inputs.end())
Expand All @@ -165,8 +165,8 @@ class observer_impl_base

for (auto& in_next : new_devs.inputs)
{
if (auto it = std::find(current_inputs.begin(), current_inputs.end(), in_next);
it == current_inputs.end())
if (auto it = std::find(m_current_inputs.begin(), m_current_inputs.end(), in_next);
it == m_current_inputs.end())
{
if (auto& cb = this->configuration.input_added)
{
Expand All @@ -175,7 +175,7 @@ class observer_impl_base
}
}

for (auto& out_prev : current_outputs)
for (auto& out_prev : m_current_outputs)
{
if (auto it = std::find(new_devs.outputs.begin(), new_devs.outputs.end(), out_prev);
it == new_devs.outputs.end())
Expand All @@ -189,28 +189,28 @@ class observer_impl_base

for (auto& out_next : new_devs.outputs)
{
if (auto it = std::find(current_outputs.begin(), current_outputs.end(), out_next);
it == current_outputs.end())
if (auto it = std::find(m_current_outputs.begin(), m_current_outputs.end(), out_next);
it == m_current_outputs.end())
{
if (auto& cb = this->configuration.output_added)
{
cb(to_port_info<false>(out_next));
}
}
}
current_inputs = std::move(new_devs.inputs);
current_outputs = std::move(new_devs.outputs);
m_current_inputs = std::move(new_devs.inputs);
m_current_outputs = std::move(new_devs.outputs);
}

udev_helper udev{};
eventfd_notifier termination_event{};
timerfd_timer timer_fd{};
int timer_check_counts = 0;
std::thread thread;
std::vector<alsa_raw_port_info> current_inputs;
std::vector<alsa_raw_port_info> current_outputs;
udev_helper m_udev{};
eventfd_notifier m_termination_event{};
timerfd_timer m_timer_fd{};
int m_timer_check_counts = 0;
std::thread m_thread;
std::vector<alsa_raw_port_info> m_current_inputs;
std::vector<alsa_raw_port_info> m_current_outputs;

pollfd fds[3]{};
pollfd m_fds[3]{};
};
}
#else
Expand Down
3 changes: 3 additions & 0 deletions include/libremidi/backends/alsa_raw_ump/config.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once
#include <libremidi/backends/alsa_raw/config.hpp>

#include <chrono>
#include <functional>

namespace libremidi::alsa_raw_ump
{
struct input_configuration
Expand Down
2 changes: 2 additions & 0 deletions include/libremidi/backends/alsa_raw_ump/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <libremidi/config.hpp>
#include <libremidi/detail/observer.hpp>

#include <cstdio>

namespace libremidi::alsa_raw_ump
{
struct midi2_enumerator : alsa_raw::enumerator
Expand Down
26 changes: 13 additions & 13 deletions include/libremidi/backends/alsa_raw_ump/midi_in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class midi_in_impl_threaded : public midi_in_impl
libremidi::ump_input_configuration&& conf, alsa_raw_ump::input_configuration&& apiconf)
: midi_in_impl{std::move(conf), std::move(apiconf)}
{
if (this->termination_event < 0)
if (this->m_termination_event < 0)
{
libremidi_handle_error(this->configuration, "error creating eventfd.");
return;
Expand All @@ -206,14 +206,14 @@ class midi_in_impl_threaded : public midi_in_impl
~midi_in_impl_threaded()
{
// Close a connection if it exists.
this->close_port();
midi_in_impl_threaded::close_port();
client_open_ = std::errc::not_connected;
}

private:
void run_thread(auto parse_func)
{
fds_.push_back(this->termination_event);
fds_.push_back(this->m_termination_event);
const auto period
= std::chrono::duration_cast<std::chrono::milliseconds>(this->configuration.poll_period)
.count();
Expand All @@ -226,7 +226,7 @@ class midi_in_impl_threaded : public midi_in_impl
continue;
else if (err < 0)
return;
else if (termination_event.ready(fds_.back()))
else if (m_termination_event.ready(fds_.back()))
break;

err = do_read_events(parse_func, {fds_.data(), fds_.size() - 1});
Expand All @@ -243,11 +243,11 @@ class midi_in_impl_threaded : public midi_in_impl
{
if (configuration.timestamps == timestamp_mode::NoTimestamp)
{
this->thread_ = std::thread{[this] { run_thread(&midi_in_impl::read_input_buffer); }};
this->m_thread = std::thread{[this] { run_thread(&midi_in_impl::read_input_buffer); }};
}
else
{
this->thread_ = std::thread{
this->m_thread = std::thread{
[this] { run_thread(&midi_in_impl::read_input_buffer_with_timestamps); }};
}
return stdx::error{};
Expand Down Expand Up @@ -275,16 +275,16 @@ class midi_in_impl_threaded : public midi_in_impl

stdx::error close_port() override
{
termination_event.notify();
if (thread_.joinable())
thread_.join();
termination_event.consume(); // Reset to zero
m_termination_event.notify();
if (m_thread.joinable())
m_thread.join();
m_termination_event.consume(); // Reset to zero

return midi_in_impl::close_port();
}

std::thread thread_;
eventfd_notifier termination_event{};
std::thread m_thread;
eventfd_notifier m_termination_event{};
};

class midi_in_impl_manual : public midi_in_impl
Expand All @@ -299,7 +299,7 @@ class midi_in_impl_manual : public midi_in_impl
~midi_in_impl_manual()
{
// Close a connection if it exists.
this->close_port();
midi_in_impl_manual::close_port();

client_open_ = std::errc::not_connected;
}
Expand Down
Loading

0 comments on commit 91b471c

Please sign in to comment.