Browse Source

LV2 Client: Update Atom parsing to use Optional

pull/22/head
reuk 3 years ago
parent
commit
4805b86ca4
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
2 changed files with 55 additions and 54 deletions
  1. +15
    -17
      modules/juce_audio_plugin_client/LV2/juce_LV2_Client.cpp
  2. +40
    -37
      modules/juce_audio_processors/format_types/juce_LV2Common.h

+ 15
- 17
modules/juce_audio_plugin_client/LV2/juce_LV2_Client.cpp View File

@@ -297,12 +297,12 @@ public:
// Carla always seems to give us an integral 'beat' even though I'd expect // Carla always seems to give us an integral 'beat' even though I'd expect
// it to be a floating-point value // it to be a floating-point value
if ( parser.parseNumericAtom<float> (atomBeatsPerMinute).andThen ([&] (float value) { info.bpm = value; })
&& parser.parseNumericAtom<float> (atomBeatsPerBar) .andThen ([&] (float value) { info.timeSigNumerator = (int) value; })
&& parser.parseNumericAtom<int32_t> (atomBeatUnit) .andThen ([&] (int32_t value) { info.timeSigDenominator = value; })
&& parser.parseNumericAtom<double> (atomBeat) .andThen ([&] (double value) { info.ppqPosition = value; })
&& parser.parseNumericAtom<float> (atomSpeed) .andThen ([&] (float value) { info.isPlaying = value != 0.0f; })
&& parser.parseNumericAtom<int64_t> (atomFrame) .andThen (setTimeInFrames))
if ( lv2_shared::withValue (parser.parseNumericAtom<float> (atomBeatsPerMinute), [&] (float value) { info.bpm = value; })
&& lv2_shared::withValue (parser.parseNumericAtom<float> (atomBeatsPerBar), [&] (float value) { info.timeSigNumerator = (int) value; })
&& lv2_shared::withValue (parser.parseNumericAtom<int32_t> (atomBeatUnit), [&] (int32_t value) { info.timeSigDenominator = value; })
&& lv2_shared::withValue (parser.parseNumericAtom<double> (atomBeat), [&] (double value) { info.ppqPosition = value; })
&& lv2_shared::withValue (parser.parseNumericAtom<float> (atomSpeed), [&] (float value) { info.isPlaying = value != 0.0f; })
&& lv2_shared::withValue (parser.parseNumericAtom<int64_t> (atomFrame), setTimeInFrames))
{ {
valid = true; valid = true;
} }
@@ -1371,14 +1371,14 @@ LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
const auto blockLengthUrid = mapFeature->map (mapFeature->handle, LV2_BUF_SIZE__maxBlockLength); const auto blockLengthUrid = mapFeature->map (mapFeature->handle, LV2_BUF_SIZE__maxBlockLength);
const auto blockSize = parser.parseNumericOption<int64_t> (findMatchingOption (options, blockLengthUrid)); const auto blockSize = parser.parseNumericOption<int64_t> (findMatchingOption (options, blockLengthUrid));
if (! blockSize.successful)
if (! blockSize.hasValue())
{ {
// The host doesn't specify a maximum block size // The host doesn't specify a maximum block size
jassertfalse; jassertfalse;
return nullptr; return nullptr;
} }
return new LV2PluginInstance { sampleRate, blockSize.value, pathToBundle, *mapFeature };
return new LV2PluginInstance { sampleRate, *blockSize, pathToBundle, *mapFeature };
}, },
[] (LV2_Handle instance, uint32_t port, void* data) [] (LV2_Handle instance, uint32_t port, void* data)
{ {
@@ -1434,8 +1434,7 @@ LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
return &descriptor; return &descriptor;
} }
static lv2_shared::NumericAtomParser::ParseResult<float> findScaleFactor (const LV2_URID_Map* symap,
const LV2_Options_Option* options)
static Optional<float> findScaleFactor (const LV2_URID_Map* symap, const LV2_Options_Option* options)
{ {
if (options == nullptr || symap == nullptr) if (options == nullptr || symap == nullptr)
return {}; return {};
@@ -1459,7 +1458,7 @@ public:
LV2UI_Widget parentIn, LV2UI_Widget parentIn,
const LV2_URID_Map* symapIn, const LV2_URID_Map* symapIn,
const LV2UI_Resize* resizeFeatureIn, const LV2UI_Resize* resizeFeatureIn,
lv2_shared::NumericAtomParser::ParseResult<float> scaleFactorIn)
Optional<float> scaleFactorIn)
: writeFunction (writeFunctionIn), : writeFunction (writeFunctionIn),
controller (controllerIn), controller (controllerIn),
plugin (pluginIn), plugin (pluginIn),
@@ -1544,11 +1543,11 @@ public:
if (opt->context != LV2_OPTIONS_INSTANCE || opt->subject != 0 || opt->key != scaleFactorUrid) if (opt->context != LV2_OPTIONS_INSTANCE || opt->subject != 0 || opt->key != scaleFactorUrid)
continue; continue;
if (scaleFactor.successful)
if (scaleFactor.hasValue())
{ {
opt->type = floatUrid; opt->type = floatUrid;
opt->size = sizeof (float); opt->size = sizeof (float);
opt->value = &scaleFactor.value;
opt->value = &(*scaleFactor);
} }
} }
@@ -1571,8 +1570,7 @@ public:
continue; continue;
} }
scaleFactor.successful = true;
scaleFactor.value = *static_cast<const float*> (opt->value);
scaleFactor = *static_cast<const float*> (opt->value);
updateScale(); updateScale();
} }
@@ -1596,7 +1594,7 @@ private:
float getScaleFactor() const noexcept float getScaleFactor() const noexcept
{ {
return scaleFactor.successful ? scaleFactor.value : 1.0f;
return scaleFactor.hasValue() ? *scaleFactor : 1.0f;
} }
void componentMovedOrResized (Component&, bool, bool wasResized) override void componentMovedOrResized (Component&, bool, bool wasResized) override
@@ -1637,7 +1635,7 @@ private:
LV2UI_Widget parent; LV2UI_Widget parent;
const LV2_URID_Map* symap = nullptr; const LV2_URID_Map* symap = nullptr;
const LV2UI_Resize* resizeFeature = nullptr; const LV2UI_Resize* resizeFeature = nullptr;
lv2_shared::NumericAtomParser::ParseResult<float> scaleFactor;
Optional<float> scaleFactor;
std::unique_ptr<AudioProcessorEditor> editor; std::unique_ptr<AudioProcessorEditor> editor;
bool hostRequestedResize = false; bool hostRequestedResize = false;


+ 40
- 37
modules/juce_audio_processors/format_types/juce_LV2Common.h View File

@@ -19,6 +19,7 @@
#pragma once #pragma once
#include "juce_lv2_config.h" #include "juce_lv2_config.h"
#include "juce_core/containers/juce_Optional.h"
#ifdef Bool #ifdef Bool
#undef Bool // previously defined in X11/Xlib.h #undef Bool // previously defined in X11/Xlib.h
@@ -127,61 +128,61 @@ struct ObjectTraits { static constexpr auto construct = lv2_atom_forge_object;
using SequenceFrame = ScopedFrame<SequenceTraits>; using SequenceFrame = ScopedFrame<SequenceTraits>;
using ObjectFrame = ScopedFrame<ObjectTraits>; using ObjectFrame = ScopedFrame<ObjectTraits>;
template <typename Value, typename Callback>
bool withValue (const Optional<Value>& opt, Callback&& callback)
{
if (! opt.hasValue())
return false;
callback (*opt);
return true;
}
struct NumericAtomParser struct NumericAtomParser
{ {
explicit NumericAtomParser (LV2_URID_Map mapFeatureIn) explicit NumericAtomParser (LV2_URID_Map mapFeatureIn)
: mapFeature (mapFeatureIn) {} : mapFeature (mapFeatureIn) {}
template <typename Type>
struct ParseResult
{
ParseResult (Type type) : value (type), successful (true) {}
ParseResult() : value(), successful (false) {}
template <typename T> struct Tag { LV2_URID urid; };
template <typename Fn>
ParseResult andThen (Fn&& fn) const
{
if (successful)
fn (value);
return *this;
}
template <typename Target, typename... Types>
static Optional<Target> tryParse (const LV2_Atom&, const void*)
{
return {};
}
operator bool() const noexcept { return successful; }
template <typename Target, typename Head, typename... Tail>
static Optional<Target> tryParse (const LV2_Atom& atom, const void* data, Tag<Head> head, Tag<Tail>... tail)
{
if (atom.type == head.urid && atom.size == sizeof (Head))
return static_cast<Target> (*reinterpret_cast<const Head*> (data));
Type value;
bool successful;
};
return tryParse<Target> (atom, data, tail...);
}
template <typename Target> template <typename Target>
ParseResult<Target> parseNumericAtom (const LV2_Atom* atom, const void* data) const
Optional<Target> parseNumericAtom (const LV2_Atom* atom, const void* data) const
{ {
if (atom == nullptr) if (atom == nullptr)
return {}; return {};
if (atom->type == mLV2_ATOM__Int && atom->size == sizeof (int32_t))
return { static_cast<Target> (*static_cast<const int32_t*> (data)) };
if (atom->type == mLV2_ATOM__Long && atom->size == sizeof (int64_t))
return { static_cast<Target> (*static_cast<const int64_t*> (data)) };
if (atom->type == mLV2_ATOM__Float && atom->size == sizeof (float))
return { static_cast<Target> (*static_cast<const float*> (data)) };
if (atom->type == mLV2_ATOM__Double && atom->size == sizeof (double))
return { static_cast<Target> (*static_cast<const double*> (data)) };
return {};
return tryParse<Target> (*atom,
data,
Tag<int32_t> { mLV2_ATOM__Bool },
Tag<int32_t> { mLV2_ATOM__Int },
Tag<int64_t> { mLV2_ATOM__Long },
Tag<float> { mLV2_ATOM__Float },
Tag<double> { mLV2_ATOM__Double });
} }
template <typename Target> template <typename Target>
ParseResult<Target> parseNumericAtom (const LV2_Atom* atom) const
Optional<Target> parseNumericAtom (const LV2_Atom* atom) const
{ {
return parseNumericAtom<Target> (atom, atom + 1); return parseNumericAtom<Target> (atom, atom + 1);
} }
template <typename Target> template <typename Target>
ParseResult<Target> parseNumericOption (const LV2_Options_Option* option) const
Optional<Target> parseNumericOption (const LV2_Options_Option* option) const
{ {
if (option != nullptr) if (option != nullptr)
{ {
@@ -240,8 +241,10 @@ struct PatchSetHelper
lv2_atom_object_query (object, query); lv2_atom_object_query (object, query);
if (isPlugin (subject))
setPluginProperty (property, value, std::forward<Callback> (callback));
if (! isPlugin (subject))
return;
setPluginProperty (property, value, std::forward<Callback> (callback));
} }
template <typename Callback> template <typename Callback>
@@ -270,14 +273,14 @@ struct PatchSetHelper
const auto parseResult = parser.parseNumericAtom<float> (value); const auto parseResult = parser.parseNumericAtom<float> (value);
if (! parseResult.successful)
if (! parseResult.hasValue())
{ {
// Didn't understand the type of this atom. // Didn't understand the type of this atom.
jassertfalse; jassertfalse;
return; return;
} }
callback.setParameter (reinterpret_cast<const LV2_Atom_URID*> (property)->body, parseResult.value);
callback.setParameter (reinterpret_cast<const LV2_Atom_URID*> (property)->body, *parseResult);
} }
NumericAtomParser parser; NumericAtomParser parser;


Loading…
Cancel
Save