mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-29 02:40:05 +00:00
LV2 Client: Update Atom parsing to use Optional
This commit is contained in:
parent
208fc05480
commit
4805b86ca4
2 changed files with 55 additions and 54 deletions
|
|
@ -297,12 +297,12 @@ public:
|
|||
// Carla always seems to give us an integral 'beat' even though I'd expect
|
||||
// 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;
|
||||
}
|
||||
|
|
@ -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 blockSize = parser.parseNumericOption<int64_t> (findMatchingOption (options, blockLengthUrid));
|
||||
|
||||
if (! blockSize.successful)
|
||||
if (! blockSize.hasValue())
|
||||
{
|
||||
// The host doesn't specify a maximum block size
|
||||
jassertfalse;
|
||||
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)
|
||||
{
|
||||
|
|
@ -1434,8 +1434,7 @@ LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
|
|||
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)
|
||||
return {};
|
||||
|
|
@ -1459,7 +1458,7 @@ public:
|
|||
LV2UI_Widget parentIn,
|
||||
const LV2_URID_Map* symapIn,
|
||||
const LV2UI_Resize* resizeFeatureIn,
|
||||
lv2_shared::NumericAtomParser::ParseResult<float> scaleFactorIn)
|
||||
Optional<float> scaleFactorIn)
|
||||
: writeFunction (writeFunctionIn),
|
||||
controller (controllerIn),
|
||||
plugin (pluginIn),
|
||||
|
|
@ -1544,11 +1543,11 @@ public:
|
|||
if (opt->context != LV2_OPTIONS_INSTANCE || opt->subject != 0 || opt->key != scaleFactorUrid)
|
||||
continue;
|
||||
|
||||
if (scaleFactor.successful)
|
||||
if (scaleFactor.hasValue())
|
||||
{
|
||||
opt->type = floatUrid;
|
||||
opt->size = sizeof (float);
|
||||
opt->value = &scaleFactor.value;
|
||||
opt->value = &(*scaleFactor);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1571,8 +1570,7 @@ public:
|
|||
continue;
|
||||
}
|
||||
|
||||
scaleFactor.successful = true;
|
||||
scaleFactor.value = *static_cast<const float*> (opt->value);
|
||||
scaleFactor = *static_cast<const float*> (opt->value);
|
||||
updateScale();
|
||||
}
|
||||
|
||||
|
|
@ -1596,7 +1594,7 @@ private:
|
|||
|
||||
float getScaleFactor() const noexcept
|
||||
{
|
||||
return scaleFactor.successful ? scaleFactor.value : 1.0f;
|
||||
return scaleFactor.hasValue() ? *scaleFactor : 1.0f;
|
||||
}
|
||||
|
||||
void componentMovedOrResized (Component&, bool, bool wasResized) override
|
||||
|
|
@ -1637,7 +1635,7 @@ private:
|
|||
LV2UI_Widget parent;
|
||||
const LV2_URID_Map* symap = nullptr;
|
||||
const LV2UI_Resize* resizeFeature = nullptr;
|
||||
lv2_shared::NumericAtomParser::ParseResult<float> scaleFactor;
|
||||
Optional<float> scaleFactor;
|
||||
std::unique_ptr<AudioProcessorEditor> editor;
|
||||
bool hostRequestedResize = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "juce_lv2_config.h"
|
||||
#include "juce_core/containers/juce_Optional.h"
|
||||
|
||||
#ifdef Bool
|
||||
#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 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
|
||||
{
|
||||
explicit NumericAtomParser (LV2_URID_Map mapFeatureIn)
|
||||
: mapFeature (mapFeatureIn) {}
|
||||
|
||||
template <typename Type>
|
||||
struct ParseResult
|
||||
template <typename T> struct Tag { LV2_URID urid; };
|
||||
|
||||
template <typename Target, typename... Types>
|
||||
static Optional<Target> tryParse (const LV2_Atom&, const void*)
|
||||
{
|
||||
ParseResult (Type type) : value (type), successful (true) {}
|
||||
ParseResult() : value(), successful (false) {}
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
ParseResult andThen (Fn&& fn) const
|
||||
{
|
||||
if (successful)
|
||||
fn (value);
|
||||
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));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() const noexcept { return successful; }
|
||||
|
||||
Type value;
|
||||
bool successful;
|
||||
};
|
||||
return tryParse<Target> (atom, data, tail...);
|
||||
}
|
||||
|
||||
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)
|
||||
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>
|
||||
ParseResult<Target> parseNumericAtom (const LV2_Atom* atom) const
|
||||
Optional<Target> parseNumericAtom (const LV2_Atom* atom) const
|
||||
{
|
||||
return parseNumericAtom<Target> (atom, atom + 1);
|
||||
}
|
||||
|
||||
template <typename Target>
|
||||
ParseResult<Target> parseNumericOption (const LV2_Options_Option* option) const
|
||||
Optional<Target> parseNumericOption (const LV2_Options_Option* option) const
|
||||
{
|
||||
if (option != nullptr)
|
||||
{
|
||||
|
|
@ -240,8 +241,10 @@ struct PatchSetHelper
|
|||
|
||||
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>
|
||||
|
|
@ -270,14 +273,14 @@ struct PatchSetHelper
|
|||
|
||||
const auto parseResult = parser.parseNumericAtom<float> (value);
|
||||
|
||||
if (! parseResult.successful)
|
||||
if (! parseResult.hasValue())
|
||||
{
|
||||
// Didn't understand the type of this atom.
|
||||
jassertfalse;
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue