Replace absl::variant

This commit is contained in:
Jean Pierre Cimalando 2020-08-31 12:52:23 +02:00
parent 58fe6fb115
commit 1904aec674
6 changed files with 105 additions and 38 deletions

View file

@ -18,4 +18,4 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL
src/editor/utility/vstgui_before.h) src/editor/utility/vstgui_before.h)
target_include_directories(sfizz_editor PUBLIC "src") target_include_directories(sfizz_editor PUBLIC "src")
target_link_libraries(sfizz_editor PRIVATE sfizz-vstgui) 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)

View file

@ -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 <string>
#include <new>
#include <stdexcept>
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 <class T> 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;
};

View file

@ -158,13 +158,13 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
switch (id) { switch (id) {
case EditId::SfzFile: case EditId::SfzFile:
{ {
const std::string& value = absl::get<std::string>(v); const std::string& value = v.to_string();
updateSfzFileLabel(value); updateSfzFileLabel(value);
} }
break; break;
case EditId::Volume: case EditId::Volume:
{ {
const float value = absl::get<float>(v); const float value = v.to_float();
if (volumeSlider_) if (volumeSlider_)
volumeSlider_->setValue(value); volumeSlider_->setValue(value);
updateVolumeLabel(value); updateVolumeLabel(value);
@ -172,7 +172,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::Polyphony: case EditId::Polyphony:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (numVoicesSlider_) if (numVoicesSlider_)
numVoicesSlider_->setValue(value); numVoicesSlider_->setValue(value);
updateNumVoicesLabel(value); updateNumVoicesLabel(value);
@ -180,7 +180,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::Oversampling: case EditId::Oversampling:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
int log2Value = 0; int log2Value = 0;
for (int f = value; f > 1; f /= 2) for (int f = value; f > 1; f /= 2)
@ -193,7 +193,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::PreloadSize: case EditId::PreloadSize:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (preloadSizeSlider_) if (preloadSizeSlider_)
preloadSizeSlider_->setValue(value); preloadSizeSlider_->setValue(value);
updatePreloadSizeLabel(value); updatePreloadSizeLabel(value);
@ -201,13 +201,13 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::ScalaFile: case EditId::ScalaFile:
{ {
const std::string& value = absl::get<std::string>(v); const std::string& value = v.to_string();
updateScalaFileLabel(value); updateScalaFileLabel(value);
} }
break; break;
case EditId::ScalaRootKey: case EditId::ScalaRootKey:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (scalaRootKeySlider_) if (scalaRootKeySlider_)
scalaRootKeySlider_->setValue(value); scalaRootKeySlider_->setValue(value);
updateScalaRootKeyLabel(value); updateScalaRootKeyLabel(value);
@ -215,7 +215,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::TuningFrequency: case EditId::TuningFrequency:
{ {
const float value = absl::get<float>(v); const float value = v.to_float();
if (tuningFrequencySlider_) if (tuningFrequencySlider_)
tuningFrequencySlider_->setValue(value); tuningFrequencySlider_->setValue(value);
updateTuningFrequencyLabel(value); updateTuningFrequencyLabel(value);
@ -223,7 +223,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::StretchTuning: case EditId::StretchTuning:
{ {
const float value = absl::get<float>(v); const float value = v.to_float();
if (stretchedTuningSlider_) if (stretchedTuningSlider_)
stretchedTuningSlider_->setValue(value); stretchedTuningSlider_->setValue(value);
updateStretchedTuningLabel(value); updateStretchedTuningLabel(value);
@ -231,49 +231,49 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)
break; break;
case EditId::UINumCurves: case EditId::UINumCurves:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoCurvesLabel_) if (CTextLabel* label = infoCurvesLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UINumMasters: case EditId::UINumMasters:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoMastersLabel_) if (CTextLabel* label = infoMastersLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UINumGroups: case EditId::UINumGroups:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoGroupsLabel_) if (CTextLabel* label = infoGroupsLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UINumRegions: case EditId::UINumRegions:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoRegionsLabel_) if (CTextLabel* label = infoRegionsLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UINumPreloadedSamples: case EditId::UINumPreloadedSamples:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoSamplesLabel_) if (CTextLabel* label = infoSamplesLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UINumActiveVoices: case EditId::UINumActiveVoices:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
if (CTextLabel* label = infoVoicesLabel_) if (CTextLabel* label = infoVoicesLabel_)
formatLabel(label, "%u", value); formatLabel(label, "%u", value);
} }
break; break;
case EditId::UIActivePanel: case EditId::UIActivePanel:
{ {
const int value = static_cast<int>(absl::get<float>(v)); const int value = static_cast<int>(v.to_float());
setActivePanel(value); setActivePanel(value);
} }
break; break;

View file

@ -5,11 +5,11 @@
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#pragma once #pragma once
#include "EditValue.h"
#include <absl/strings/string_view.h> #include <absl/strings/string_view.h>
#include <absl/types/variant.h> #include <string>
#include <cstdint> #include <cstdint>
enum class EditId : int; enum class EditId : int;
typedef absl::variant<float, std::string> EditValue;
class EditorController { class EditorController {
public: public:

View file

@ -410,31 +410,31 @@ void sfizz_ui_t::uiSendValue(EditId id, const EditValue& v)
switch (id) { switch (id) {
case EditId::Volume: case EditId::Volume:
sendFloat(SFIZZ_VOLUME, absl::get<float>(v)); sendFloat(SFIZZ_VOLUME, v.to_float());
break; break;
case EditId::Polyphony: case EditId::Polyphony:
sendFloat(SFIZZ_POLYPHONY, absl::get<float>(v)); sendFloat(SFIZZ_POLYPHONY, v.to_float());
break; break;
case EditId::Oversampling: case EditId::Oversampling:
sendFloat(SFIZZ_OVERSAMPLING, absl::get<float>(v)); sendFloat(SFIZZ_OVERSAMPLING, v.to_float());
break; break;
case EditId::PreloadSize: case EditId::PreloadSize:
sendFloat(SFIZZ_PRELOAD, absl::get<float>(v)); sendFloat(SFIZZ_PRELOAD, v.to_float());
break; break;
case EditId::ScalaRootKey: case EditId::ScalaRootKey:
sendFloat(SFIZZ_SCALA_ROOT_KEY, absl::get<float>(v)); sendFloat(SFIZZ_SCALA_ROOT_KEY, v.to_float());
break; break;
case EditId::TuningFrequency: case EditId::TuningFrequency:
sendFloat(SFIZZ_TUNING_FREQUENCY, absl::get<float>(v)); sendFloat(SFIZZ_TUNING_FREQUENCY, v.to_float());
break; break;
case EditId::StretchTuning: case EditId::StretchTuning:
sendFloat(SFIZZ_STRETCH_TUNING, absl::get<float>(v)); sendFloat(SFIZZ_STRETCH_TUNING, v.to_float());
break; break;
case EditId::SfzFile: case EditId::SfzFile:
sendPath(sfizz_sfz_file_uri, absl::get<std::string>(v)); sendPath(sfizz_sfz_file_uri, v.to_string());
break; break;
case EditId::ScalaFile: case EditId::ScalaFile:
sendPath(sfizz_scala_file_uri, absl::get<std::string>(v)); sendPath(sfizz_scala_file_uri, v.to_string());
break; break;
default: default:
break; break;

View file

@ -109,9 +109,9 @@ void SfizzVstEditor::onStateChanged()
void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v) void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v)
{ {
if (id == EditId::SfzFile) if (id == EditId::SfzFile)
loadSfzFile(absl::get<std::string>(v)); loadSfzFile(v.to_string());
else if (id == EditId::ScalaFile) else if (id == EditId::ScalaFile)
loadScalaFile(absl::get<std::string>(v)); loadScalaFile(v.to_string());
else { else {
SfizzVstController* ctrl = getController(); SfizzVstController* ctrl = getController();
@ -123,14 +123,14 @@ void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v)
switch (id) { switch (id) {
case EditId::Volume: case EditId::Volume:
normalizeAndSet(kPidVolume, kParamVolumeRange, absl::get<float>(v)); normalizeAndSet(kPidVolume, kParamVolumeRange, v.to_float());
break; break;
case EditId::Polyphony: case EditId::Polyphony:
normalizeAndSet(kPidNumVoices, kParamNumVoicesRange, absl::get<float>(v)); normalizeAndSet(kPidNumVoices, kParamNumVoicesRange, v.to_float());
break; break;
case EditId::Oversampling: case EditId::Oversampling:
{ {
const int32 value = static_cast<int32>(absl::get<float>(v)); const int32 value = static_cast<int32>(v.to_float());
int32 log2Value = 0; int32 log2Value = 0;
for (int32 f = value; f > 1; f /= 2) for (int32 f = value; f > 1; f /= 2)
@ -140,20 +140,20 @@ void SfizzVstEditor::uiSendValue(EditId id, const EditValue& v)
} }
break; break;
case EditId::PreloadSize: case EditId::PreloadSize:
normalizeAndSet(kPidPreloadSize, kParamPreloadSizeRange, absl::get<float>(v)); normalizeAndSet(kPidPreloadSize, kParamPreloadSizeRange, v.to_float());
break; break;
case EditId::ScalaRootKey: case EditId::ScalaRootKey:
normalizeAndSet(kPidScalaRootKey, kParamScalaRootKeyRange, absl::get<float>(v)); normalizeAndSet(kPidScalaRootKey, kParamScalaRootKeyRange, v.to_float());
break; break;
case EditId::TuningFrequency: case EditId::TuningFrequency:
normalizeAndSet(kPidTuningFrequency, kParamTuningFrequencyRange, absl::get<float>(v)); normalizeAndSet(kPidTuningFrequency, kParamTuningFrequencyRange, v.to_float());
break; break;
case EditId::StretchTuning: case EditId::StretchTuning:
normalizeAndSet(kPidStretchedTuning, kParamStretchedTuningRange, absl::get<float>(v)); normalizeAndSet(kPidStretchedTuning, kParamStretchedTuningRange, v.to_float());
break; break;
case EditId::UIActivePanel: case EditId::UIActivePanel:
ctrl->getSfizzUiState().activePanel = static_cast<int32>(absl::get<float>(v)); ctrl->getSfizzUiState().activePanel = static_cast<int32>(v.to_float());
break; break;
default: default: