From 1904aec674e9443737ce2f16d11135b9f066b31e Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 31 Aug 2020 12:52:23 +0200 Subject: [PATCH] Replace absl::variant --- editor/CMakeLists.txt | 2 +- editor/src/editor/EditValue.h | 67 ++++++++++++++++++++++++++++ editor/src/editor/Editor.cpp | 32 ++++++------- editor/src/editor/EditorController.h | 4 +- lv2/sfizz_ui.cpp | 18 ++++---- vst/SfizzVstEditor.cpp | 20 ++++----- 6 files changed, 105 insertions(+), 38 deletions(-) create mode 100644 editor/src/editor/EditValue.h diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt index 777939d4..65cdb6d9 100644 --- a/editor/CMakeLists.txt +++ b/editor/CMakeLists.txt @@ -18,4 +18,4 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL src/editor/utility/vstgui_before.h) target_include_directories(sfizz_editor PUBLIC "src") target_link_libraries(sfizz_editor PRIVATE sfizz-vstgui) -target_link_libraries(sfizz_editor PUBLIC absl::strings absl::variant) +target_link_libraries(sfizz_editor PUBLIC absl::strings) diff --git a/editor/src/editor/EditValue.h b/editor/src/editor/EditValue.h new file mode 100644 index 00000000..b8d911dd --- /dev/null +++ b/editor/src/editor/EditValue.h @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include +#include +#include + +class EditValue { +public: + constexpr EditValue() : tag(Nil) {} + EditValue(float value) { reset(value); } + EditValue(std::string value) { reset(value); } + ~EditValue() { reset(); } + + void reset() noexcept + { + if (tag == String) + destruct(u.s); + tag = Nil; + } + + void reset(float value) noexcept + { + reset(); + u.f = value; + tag = Float; + } + + void reset(std::string value) noexcept + { + reset(); + new (&u.s) std::string(std::move(value)); + tag = String; + } + + float to_float() const + { + if (tag != Float) + throw std::runtime_error("the tagged union does not contain `float`"); + return u.f; + } + + const std::string& to_string() const + { + if (tag != String) + throw std::runtime_error("the tagged union does not contain `string`"); + return u.s; + } + +private: + template static void destruct(T& obj) { obj.~T(); } + +private: + enum TypeTag { Nil, Float, String }; + union Union { + constexpr explicit Union(float f = 0.0f) noexcept : f(f) {} + ~Union() noexcept {} + float f; + std::string s; + }; + TypeTag tag { Nil }; + Union u; +}; diff --git a/editor/src/editor/Editor.cpp b/editor/src/editor/Editor.cpp index edad917d..dccae14c 100644 --- a/editor/src/editor/Editor.cpp +++ b/editor/src/editor/Editor.cpp @@ -158,13 +158,13 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) switch (id) { case EditId::SfzFile: { - const std::string& value = absl::get(v); + const std::string& value = v.to_string(); updateSfzFileLabel(value); } break; case EditId::Volume: { - const float value = absl::get(v); + const float value = v.to_float(); if (volumeSlider_) volumeSlider_->setValue(value); updateVolumeLabel(value); @@ -172,7 +172,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::Polyphony: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (numVoicesSlider_) numVoicesSlider_->setValue(value); updateNumVoicesLabel(value); @@ -180,7 +180,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::Oversampling: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); int log2Value = 0; for (int f = value; f > 1; f /= 2) @@ -193,7 +193,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::PreloadSize: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (preloadSizeSlider_) preloadSizeSlider_->setValue(value); updatePreloadSizeLabel(value); @@ -201,13 +201,13 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::ScalaFile: { - const std::string& value = absl::get(v); + const std::string& value = v.to_string(); updateScalaFileLabel(value); } break; case EditId::ScalaRootKey: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (scalaRootKeySlider_) scalaRootKeySlider_->setValue(value); updateScalaRootKeyLabel(value); @@ -215,7 +215,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::TuningFrequency: { - const float value = absl::get(v); + const float value = v.to_float(); if (tuningFrequencySlider_) tuningFrequencySlider_->setValue(value); updateTuningFrequencyLabel(value); @@ -223,7 +223,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::StretchTuning: { - const float value = absl::get(v); + const float value = v.to_float(); if (stretchedTuningSlider_) stretchedTuningSlider_->setValue(value); updateStretchedTuningLabel(value); @@ -231,49 +231,49 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v) break; case EditId::UINumCurves: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoCurvesLabel_) formatLabel(label, "%u", value); } break; case EditId::UINumMasters: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoMastersLabel_) formatLabel(label, "%u", value); } break; case EditId::UINumGroups: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoGroupsLabel_) formatLabel(label, "%u", value); } break; case EditId::UINumRegions: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoRegionsLabel_) formatLabel(label, "%u", value); } break; case EditId::UINumPreloadedSamples: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoSamplesLabel_) formatLabel(label, "%u", value); } break; case EditId::UINumActiveVoices: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); if (CTextLabel* label = infoVoicesLabel_) formatLabel(label, "%u", value); } break; case EditId::UIActivePanel: { - const int value = static_cast(absl::get(v)); + const int value = static_cast(v.to_float()); setActivePanel(value); } break; diff --git a/editor/src/editor/EditorController.h b/editor/src/editor/EditorController.h index 6b6988c1..4d3db4c9 100644 --- a/editor/src/editor/EditorController.h +++ b/editor/src/editor/EditorController.h @@ -5,11 +5,11 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #pragma once +#include "EditValue.h" #include -#include +#include #include enum class EditId : int; -typedef absl::variant EditValue; class EditorController { public: diff --git a/lv2/sfizz_ui.cpp b/lv2/sfizz_ui.cpp index 6baf4021..069d4942 100644 --- a/lv2/sfizz_ui.cpp +++ b/lv2/sfizz_ui.cpp @@ -410,31 +410,31 @@ void sfizz_ui_t::uiSendValue(EditId id, const EditValue& v) switch (id) { case EditId::Volume: - sendFloat(SFIZZ_VOLUME, absl::get(v)); + sendFloat(SFIZZ_VOLUME, v.to_float()); break; case EditId::Polyphony: - sendFloat(SFIZZ_POLYPHONY, absl::get(v)); + sendFloat(SFIZZ_POLYPHONY, v.to_float()); break; case EditId::Oversampling: - sendFloat(SFIZZ_OVERSAMPLING, absl::get(v)); + sendFloat(SFIZZ_OVERSAMPLING, v.to_float()); break; case EditId::PreloadSize: - sendFloat(SFIZZ_PRELOAD, absl::get(v)); + sendFloat(SFIZZ_PRELOAD, v.to_float()); break; case EditId::ScalaRootKey: - sendFloat(SFIZZ_SCALA_ROOT_KEY, absl::get(v)); + sendFloat(SFIZZ_SCALA_ROOT_KEY, v.to_float()); break; case EditId::TuningFrequency: - sendFloat(SFIZZ_TUNING_FREQUENCY, absl::get(v)); + sendFloat(SFIZZ_TUNING_FREQUENCY, v.to_float()); break; case EditId::StretchTuning: - sendFloat(SFIZZ_STRETCH_TUNING, absl::get(v)); + sendFloat(SFIZZ_STRETCH_TUNING, v.to_float()); break; case EditId::SfzFile: - sendPath(sfizz_sfz_file_uri, absl::get(v)); + sendPath(sfizz_sfz_file_uri, v.to_string()); break; case EditId::ScalaFile: - sendPath(sfizz_scala_file_uri, absl::get(v)); + sendPath(sfizz_scala_file_uri, v.to_string()); break; default: break; diff --git a/vst/SfizzVstEditor.cpp b/vst/SfizzVstEditor.cpp index 163034cc..1d0a127f 100644 --- a/vst/SfizzVstEditor.cpp +++ b/vst/SfizzVstEditor.cpp @@ -109,9 +109,9 @@ void SfizzVstEditor::onStateChanged() void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v) { if (id == EditId::SfzFile) - loadSfzFile(absl::get(v)); + loadSfzFile(v.to_string()); else if (id == EditId::ScalaFile) - loadScalaFile(absl::get(v)); + loadScalaFile(v.to_string()); else { SfizzVstController* ctrl = getController(); @@ -123,14 +123,14 @@ void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v) switch (id) { case EditId::Volume: - normalizeAndSet(kPidVolume, kParamVolumeRange, absl::get(v)); + normalizeAndSet(kPidVolume, kParamVolumeRange, v.to_float()); break; case EditId::Polyphony: - normalizeAndSet(kPidNumVoices, kParamNumVoicesRange, absl::get(v)); + normalizeAndSet(kPidNumVoices, kParamNumVoicesRange, v.to_float()); break; case EditId::Oversampling: { - const int32 value = static_cast(absl::get(v)); + const int32 value = static_cast(v.to_float()); int32 log2Value = 0; for (int32 f = value; f > 1; f /= 2) @@ -140,20 +140,20 @@ void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v) } break; case EditId::PreloadSize: - normalizeAndSet(kPidPreloadSize, kParamPreloadSizeRange, absl::get(v)); + normalizeAndSet(kPidPreloadSize, kParamPreloadSizeRange, v.to_float()); break; case EditId::ScalaRootKey: - normalizeAndSet(kPidScalaRootKey, kParamScalaRootKeyRange, absl::get(v)); + normalizeAndSet(kPidScalaRootKey, kParamScalaRootKeyRange, v.to_float()); break; case EditId::TuningFrequency: - normalizeAndSet(kPidTuningFrequency, kParamTuningFrequencyRange, absl::get(v)); + normalizeAndSet(kPidTuningFrequency, kParamTuningFrequencyRange, v.to_float()); break; case EditId::StretchTuning: - normalizeAndSet(kPidStretchedTuning, kParamStretchedTuningRange, absl::get(v)); + normalizeAndSet(kPidStretchedTuning, kParamStretchedTuningRange, v.to_float()); break; case EditId::UIActivePanel: - ctrl->getSfizzUiState().activePanel = static_cast(absl::get(v)); + ctrl->getSfizzUiState().activePanel = static_cast(v.to_float()); break; default: