Skip to content

Commit

Permalink
Make more variants of the plugin undo testing
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Sep 17, 2024
1 parent 02933b2 commit 902e85a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 39 deletions.
22 changes: 12 additions & 10 deletions plugins/clap-entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

#include <cstring>
#include <functional>
#include <vector>
#include <mutex>
#include <iostream>
#include <mutex>
#include <vector>

#include "plugs/adsr/adsr-plug.hh"
#include "plugs/char-check/char-check.hh"
#include "plugs/dc-offset/dc-offset.hh"
#include "plugs/gain/gain.hh"
#include "plugs/transport/transport-info.hh"
#include "plugs/char-check/char-check.hh"
#include "plugs/synth/synth.hh"
#include "plugs/svf/svf-plug.hh"
#include "plugs/latency/latency.hh"
#include "plugs/offline-latency/offline-latency.hh"
#include "plugs/realtime-requirement/realtime-requirement.hh"
#include "plugs/svf/svf-plug.hh"
#include "plugs/synth/synth.hh"
#include "plugs/track-info/track-info.hh"
#include "plugs/transport/transport-info.hh"
#include "plugs/undo-test/undo-test.hh"

struct PluginEntry {
Expand Down Expand Up @@ -54,7 +54,9 @@ static bool clap_init(const char *plugin_path) {
addPlugin<clap::OfflineLatency>();
addPlugin<clap::RealtimeRequirement>();
addPlugin<clap::TrackInfo>();
addPlugin<clap::UndoTest>();
addPlugin<clap::UndoTest<true, true>>();
addPlugin<clap::UndoTest<true, false>>();
addPlugin<clap::UndoTest<false, false>>();
return true;
}

Expand Down Expand Up @@ -89,7 +91,8 @@ static uint32_t clap_get_plugin_count(const clap_plugin_factory *) { return g_pl
static const clap_plugin_descriptor *clap_get_plugin_descriptor(const clap_plugin_factory *,
uint32_t index) {
if (index < 0 || index >= g_plugins.size()) {
std::cerr << "index out of bounds: " << index << " not in 0.." << g_plugins.size() << std::endl;
std::cerr << "index out of bounds: " << index << " not in 0.." << g_plugins.size()
<< std::endl;
std::terminate();
}

Expand All @@ -110,8 +113,7 @@ static const clap_plugin_factory g_clap_plugin_factory = {
clap_create_plugin,
};

const void *clap_get_factory(const char *factory_id)
{
const void *clap_get_factory(const char *factory_id) {
if (!::strcmp(factory_id, CLAP_PLUGIN_FACTORY_ID))
return &g_clap_plugin_factory;
return nullptr;
Expand Down
99 changes: 71 additions & 28 deletions plugins/plugs/undo-test/undo-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,79 @@ namespace clap {
};
} // namespace

template <bool hasDelta, bool areDeltasPersistant>
class UndoTestModule final : public Module {
public:
UndoTestModule(UndoTest &plugin) : Module(plugin, "", 0) {}
UndoTestModule(UndoTest<hasDelta, areDeltasPersistant> &plugin) : Module(plugin, "", 0) {}

clap_process_status process(const Context &c, uint32_t numFrames) noexcept override {
return CLAP_PROCESS_SLEEP;
}
};

const clap_plugin_descriptor *UndoTest::descriptor() {
template <bool hasDelta, bool areDeltasPersistant>
const clap_plugin_descriptor *UndoTest<hasDelta, areDeltasPersistant>::descriptor() {
static const char *features[] = {
CLAP_PLUGIN_FEATURE_UTILITY, CLAP_PLUGIN_FEATURE_ANALYZER, nullptr};

static const clap_plugin_descriptor desc = {CLAP_VERSION,
"com.github.free-audio.clap.undo-test",
"Undo Test",
"clap",
"https://github.com/free-audio/clap",
nullptr,
nullptr,
"0.1",
"Help testing the undo extension",
features};
static const clap_plugin_descriptor desc = {
CLAP_VERSION,
areDeltasPersistant ? "com.github.free-audio.clap.undo-test"
: hasDelta ? "com.github.free-audio.clap.undo-test-not-persistent"
: "com.github.free-audio.clap.undo-test-no-deltas",
areDeltasPersistant ? "Undo Test"
: hasDelta ? "UndoTest (deltas not persistent)"
: "UndoTest (no deltas)",
"clap",
"https://github.com/free-audio/clap",
nullptr,
nullptr,
"0.1",
"Help testing the undo extension",
features};

return &desc;
}

UndoTest::UndoTest(const std::string &pluginPath, const clap_host &host)
: CorePlugin(PathProvider::create(pluginPath, "undo-test"), descriptor(), host) {
template <bool hasDelta, bool areDeltasPersistant>
UndoTest<hasDelta, areDeltasPersistant>::UndoTest(const std::string &pluginPath,
const clap_host &host)
: CorePlugin(PathProvider::create(
pluginPath, UndoTest<hasDelta, areDeltasPersistant>::descriptor()->name),
descriptor(),
host) {
_rootModule = std::make_unique<UndoTestModule>(*this);
}

bool UndoTest::implementsUndo() const noexcept { return true; }
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::implementsUndo() const noexcept {
return true;
}

void UndoTest::undoGetDeltaProperties(clap_undo_delta_properties_t *properties) noexcept {
properties->has_delta = true;
properties->are_deltas_persistent = true;
properties->format_version = UNDO_FORMAT_VERSION;
template <bool hasDelta, bool areDeltasPersistant>
void UndoTest<hasDelta, areDeltasPersistant>::undoGetDeltaProperties(
clap_undo_delta_properties_t *properties) noexcept {
properties->has_delta = hasDelta;
properties->are_deltas_persistent = areDeltasPersistant;
properties->format_version = hasDelta ? UNDO_FORMAT_VERSION : CLAP_INVALID_ID;
}

bool UndoTest::undoCanUseDeltaFormatVersion(clap_id format_version) noexcept {
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::undoCanUseDeltaFormatVersion(
clap_id format_version) noexcept {
if constexpr (!hasDelta)
return false;

return format_version == UNDO_FORMAT_VERSION;
}

bool UndoTest::undoUndo(clap_id format_version, const void *delta, size_t delta_size) noexcept {
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::undoUndo(clap_id format_version,
const void *delta,
size_t delta_size) noexcept {
if constexpr (!hasDelta)
return false;

if (format_version != UNDO_FORMAT_VERSION) {
hostMisbehaving("invalid undo delta format version");
return false;
Expand All @@ -76,7 +104,13 @@ namespace clap {
return true;
}

bool UndoTest::undoRedo(clap_id format_version, const void *delta, size_t delta_size) noexcept {
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::undoRedo(clap_id format_version,
const void *delta,
size_t delta_size) noexcept {
if constexpr (!hasDelta)
return false;

if (format_version != UNDO_FORMAT_VERSION) {
hostMisbehaving("invalid undo delta format version");
return false;
Expand All @@ -97,7 +131,8 @@ namespace clap {
return true;
}

void UndoTest::incrementState() {
template <bool hasDelta, bool areDeltasPersistant>
void UndoTest<hasDelta, areDeltasPersistant>::incrementState() {
if (!_host.canUseUndo())
return;

Expand All @@ -109,10 +144,14 @@ namespace clap {
snprintf(buffer, sizeof(buffer), "UNDO increment %d -> %d", delta.old_value, delta.new_value);
_host.log(CLAP_LOG_INFO, buffer);

_host.undoChangeMade(buffer, &delta, sizeof(delta), true);
if constexpr (hasDelta)
_host.undoChangeMade(buffer, &delta, sizeof(delta), true);
else
_host.undoChangeMade(buffer, nullptr, 0, 0);
}

bool UndoTest::init() noexcept {
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::init() noexcept {
if (!super::init())
return false;

Expand All @@ -123,7 +162,8 @@ namespace clap {
}

#ifndef CLAP_PLUGINS_HEADLESS
void UndoTest::onGuiInvoke(
template <bool hasDelta, bool areDeltasPersistant>
void UndoTest<hasDelta, areDeltasPersistant>::onGuiInvoke(
const std::string &method,
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) {
if (method == "incrementState")
Expand All @@ -133,11 +173,14 @@ namespace clap {
}
#endif

std::vector<uint8_t> UndoTest::stateSaveExtra() noexcept {
template <bool hasDelta, bool areDeltasPersistant>
std::vector<uint8_t> UndoTest<hasDelta, areDeltasPersistant>::stateSaveExtra() noexcept {
return std::vector<uint8_t>((const uint8_t *)&_state, (const uint8_t *)(&_state + 1));
}

bool UndoTest::stateLoadExtra(const std::vector<uint8_t> &data) noexcept {
template <bool hasDelta, bool areDeltasPersistant>
bool UndoTest<hasDelta, areDeltasPersistant>::stateLoadExtra(
const std::vector<uint8_t> &data) noexcept {
if (data.size() != sizeof(_state))
return false;
_state = *(const uint32_t *)data.data();
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugs/undo-test/undo-test.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "../../core-plugin.hh"

namespace clap {
template <bool hasDelta, bool areDeltasPersistant>
class UndoTest final : public CorePlugin {
private:
using super = CorePlugin;

public:
Expand Down

0 comments on commit 902e85a

Please sign in to comment.