From 321a229dfda73096ae3ac092cccfe57f6fc27b6d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 10 Sep 2019 21:00:56 -0400 Subject: [PATCH] AudioGroupPool: Use std::array where applicable Makes the array types strongly-typed and also allows for size querying. --- Editor/SoundMacroEditor.cpp | 9 ++-- include/amuse/AudioGroupPool.hpp | 4 +- lib/AudioGroupPool.cpp | 71 ++++++++++++++++---------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/Editor/SoundMacroEditor.cpp b/Editor/SoundMacroEditor.cpp index 97c0e3f..f7a830e 100644 --- a/Editor/SoundMacroEditor.cpp +++ b/Editor/SoundMacroEditor.cpp @@ -239,14 +239,15 @@ CommandWidget::CommandWidget(QWidget* parent, amuse::SoundMacro::ICmd* cmd, amus break; } case amuse::SoundMacro::CmdIntrospection::Field::Type::Choice: { - FieldComboBox* cb = new FieldComboBox(this); + auto* const cb = new FieldComboBox(this); cb->setFixedHeight(30); cb->setProperty("fieldIndex", f); cb->setProperty("fieldName", fieldName); - for (int j = 0; j < 4; ++j) { - if (field.m_choices[j].empty()) + for (const auto choice : field.m_choices) { + if (choice.empty()) { break; - cb->addItem(tr(field.m_choices[j].data())); + } + cb->addItem(tr(choice.data())); } cb->setCurrentIndex(int(amuse::AccessField(m_cmd, field))); connect(cb, qOverload(&FieldComboBox::currentIndexChanged), this, &CommandWidget::numChanged); diff --git a/include/amuse/AudioGroupPool.hpp b/include/amuse/AudioGroupPool.hpp index 74374ba..0d4c4c1 100644 --- a/include/amuse/AudioGroupPool.hpp +++ b/include/amuse/AudioGroupPool.hpp @@ -150,12 +150,12 @@ struct SoundMacro { size_t m_offset; std::string_view m_name; int64_t m_min, m_max, m_default; - std::string_view m_choices[4]; + std::array m_choices; }; CmdType m_tp; std::string_view m_name; std::string_view m_description; - Field m_fields[7]; + std::array m_fields; }; /** Base command interface. All versions of MusyX encode little-endian parameters */ diff --git a/lib/AudioGroupPool.cpp b/lib/AudioGroupPool.cpp index 4860fdd..0960416 100644 --- a/lib/AudioGroupPool.cpp +++ b/lib/AudioGroupPool.cpp @@ -37,41 +37,42 @@ struct MakeDefaultCmdOp { static std::unique_ptr Do(R& r) { std::unique_ptr ret = std::make_unique(); if (const SoundMacro::CmdIntrospection* introspection = SoundMacro::GetCmdIntrospection(r)) { - for (int f = 0; f < 7; ++f) { - const amuse::SoundMacro::CmdIntrospection::Field& field = introspection->m_fields[f]; - if (!field.m_name.empty()) { - switch (field.m_tp) { - case amuse::SoundMacro::CmdIntrospection::Field::Type::Bool: - AccessField(ret.get(), field) = bool(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::Int8: - case amuse::SoundMacro::CmdIntrospection::Field::Type::Choice: - AccessField(ret.get(), field) = int8_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt8: - AccessField(ret.get(), field) = uint8_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::Int16: - AccessField(ret.get(), field) = int16_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt16: - AccessField(ret.get(), field) = uint16_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::Int32: - AccessField(ret.get(), field) = int32_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt32: - AccessField(ret.get(), field) = uint32_t(field.m_default); - break; - case amuse::SoundMacro::CmdIntrospection::Field::Type::SoundMacroId: - case amuse::SoundMacro::CmdIntrospection::Field::Type::SoundMacroStep: - case amuse::SoundMacro::CmdIntrospection::Field::Type::TableId: - case amuse::SoundMacro::CmdIntrospection::Field::Type::SampleId: - AccessField>(ret.get(), field).id = uint16_t(field.m_default); - break; - default: - break; - } + for (const auto& field : introspection->m_fields) { + if (field.m_name.empty()) { + continue; + } + + switch (field.m_tp) { + case amuse::SoundMacro::CmdIntrospection::Field::Type::Bool: + AccessField(ret.get(), field) = bool(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::Int8: + case amuse::SoundMacro::CmdIntrospection::Field::Type::Choice: + AccessField(ret.get(), field) = int8_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt8: + AccessField(ret.get(), field) = uint8_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::Int16: + AccessField(ret.get(), field) = int16_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt16: + AccessField(ret.get(), field) = uint16_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::Int32: + AccessField(ret.get(), field) = int32_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::UInt32: + AccessField(ret.get(), field) = uint32_t(field.m_default); + break; + case amuse::SoundMacro::CmdIntrospection::Field::Type::SoundMacroId: + case amuse::SoundMacro::CmdIntrospection::Field::Type::SoundMacroStep: + case amuse::SoundMacro::CmdIntrospection::Field::Type::TableId: + case amuse::SoundMacro::CmdIntrospection::Field::Type::SampleId: + AccessField>(ret.get(), field).id = uint16_t(field.m_default); + break; + default: + break; } } }