From fcc23139f622963c86e7f53c8b96f6f214294032 Mon Sep 17 00:00:00 2001 From: Ameer Date: Mon, 22 Jun 2020 18:11:59 -0400 Subject: [PATCH] std::array and const reference passing of non-trivial objects --- src/input_common/gcadapter/gc_adapter.cpp | 21 ++++++++++----------- src/input_common/gcadapter/gc_adapter.h | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index e3bcb24f6..745f1be27 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -21,7 +21,7 @@ Adapter::Adapter() { StartScanThread(); } -GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { +GCPadStatus Adapter::CheckStatus(int port, const std::array& adapter_payload) { GCPadStatus pad = {}; bool get_origin = false; @@ -88,7 +88,7 @@ GCPadStatus Adapter::CheckStatus(int port, u8 adapter_payload[37]) { return pad; } -void Adapter::PadToState(GCPadStatus pad, GCState& state) { +void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { state.buttons.insert_or_assign(PAD_BUTTON_A, pad.button & PAD_BUTTON_A); state.buttons.insert_or_assign(PAD_BUTTON_B, pad.button & PAD_BUTTON_B); state.buttons.insert_or_assign(PAD_BUTTON_X, pad.button & PAD_BUTTON_X); @@ -112,15 +112,15 @@ void Adapter::PadToState(GCPadStatus pad, GCState& state) { void Adapter::Read() { LOG_INFO(Input, "GC Adapter Read() thread started"); - int payload_size_in; - u8 adapter_payload[37]; + int payload_size_in, payload_size; + std::array adapter_payload; + std::array controller_payload_copy; + std::array pad; + while (adapter_thread_running) { - libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload, + libusb_interrupt_transfer(usb_adapter_handle, input_endpoint, adapter_payload.data(), sizeof(adapter_payload), &payload_size_in, 32); - - int payload_size = 0; - u8 controller_payload_copy[37]; - + payload_size = 0; { std::lock_guard lk(s_mutex); std::copy(std::begin(adapter_payload), std::end(adapter_payload), @@ -128,7 +128,6 @@ void Adapter::Read() { payload_size = payload_size_in; } - std::array pad; if (payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID) { LOG_ERROR(Input, "error reading payload (size: %d, type: %02x)", payload_size, @@ -365,4 +364,4 @@ const std::array& Adapter::GetPadState() const { return state; } -} // end of namespace GCAdapter +} // namespace GCAdapter diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index d7a57e7b7..ff0202e3b 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -101,9 +101,9 @@ public: const std::array& GetPadState() const; private: - GCPadStatus CheckStatus(int port, u8 adapter_payload[37]); + GCPadStatus CheckStatus(int port, const std::array& adapter_payload); - void PadToState(GCPadStatus pad, GCState& state); + void PadToState(const GCPadStatus& pad, GCState& state); void Read(); void ScanThreadFunc(); @@ -154,4 +154,4 @@ private: std::array state; }; -} // end of namespace GCAdapter +} // namespace GCAdapter