From b98159bce2de05ce05596f1494994bebfd857dac Mon Sep 17 00:00:00 2001
From: Alexandre Bique <bique.alexandre@gmail.com>
Date: Mon, 16 Oct 2023 15:20:50 +0200
Subject: [PATCH] Fixes compilation

---
 host/plugin-host.cc | 57 +++++++++++++++++------------------
 host/plugin-host.hh | 72 ++++++++++++++++++++++++---------------------
 2 files changed, 68 insertions(+), 61 deletions(-)

diff --git a/host/plugin-host.cc b/host/plugin-host.cc
index 6fd5393..5523549 100644
--- a/host/plugin-host.cc
+++ b/host/plugin-host.cc
@@ -15,6 +15,7 @@
 #include "plugin-host.hh"
 #include "settings.hh"
 
+#include <clap/helpers/host.hxx>
 #include <clap/helpers/reducing-param-queue.hxx>
 
 enum class ThreadType {
@@ -26,13 +27,15 @@ enum class ThreadType {
 
 thread_local ThreadType g_thread_type = ThreadType::Unknown;
 
+template class clap::helpers::Host<PluginHost_MH, PluginHost_CL>;
+
 PluginHost::PluginHost(Engine &engine)
-   : clap::helpers::Host("Clap Test Host",                    // name
-                         "clap",                              // vendor
-                         "0.1.0",                             // version
-                         "https://github.com/free-audio/clap" // url
-                         ),
-     QObject(&engine), _engine(engine), _settings(engine._settings.pluginHostSettings()) {
+   : QObject(&engine), _engine(engine), _settings(engine._settings.pluginHostSettings()),
+     BaseHost("Clap Test Host",                    // name
+              "clap",                              // vendor
+              "0.1.0",                             // version
+              "https://github.com/free-audio/clap" // url
+     ) {
    g_thread_type = ThreadType::MainThread;
 
    initThreadPool();
@@ -356,19 +359,13 @@ void PluginHost::setPluginWindowVisibility(bool isVisible) {
    }
 }
 
-void PluginHost::requestCallback() noexcept {
-   _scheduleMainThreadCallback = true;
-}
+void PluginHost::requestCallback() noexcept { _scheduleMainThreadCallback = true; }
 
-void PluginHost::requestProcess() noexcept {
-   _scheduleProcess = true;
-}
+void PluginHost::requestProcess() noexcept { _scheduleProcess = true; }
 
-void PluginHost::requestRestart() noexcept {
-   _scheduleRestart = true;
-}
+void PluginHost::requestRestart() noexcept { _scheduleRestart = true; }
 
-void PluginHost::logLog(clap_log_severity severity, const char *msg) noexcept {
+void PluginHost::logLog(clap_log_severity severity, const char *msg) const noexcept {
    switch (severity) {
    case CLAP_LOG_DEBUG:
       qDebug() << msg;
@@ -403,14 +400,18 @@ bool PluginHost::threadCheckIsAudioThread() noexcept {
    return g_thread_type == ThreadType::AudioThread;
 }
 
-void PluginHost::checkForMainThread() noexcept {
-   if (g_thread_type != ThreadType::MainThread)
-      throw std::logic_error("Requires Main Thread!");
+void PluginHost::checkForMainThread() {
+   if (g_thread_type != ThreadType::MainThread) [[unlikely]] {
+      qFatal() << "Requires Main Thread!";
+      std::terminate();
+   }
 }
 
 void PluginHost::checkForAudioThread() {
-   if (g_thread_type != ThreadType::AudioThread)
-      throw std::logic_error("Requires Audio Thread!");
+   if (g_thread_type != ThreadType::AudioThread) {
+      qFatal() << "Requires Audio Thread!";
+      std::terminate();
+   }
 }
 
 bool PluginHost::threadPoolRequestExec(uint32_t num_tasks) noexcept {
@@ -571,7 +572,7 @@ bool PluginHost::guiRequestResize(uint32_t width, uint32_t height) noexcept {
    return true;
 }
 
-bool PluginHost::clapGuiRequestShow() noexcept {
+bool PluginHost::guiRequestShow() noexcept {
    QMetaObject::invokeMethod(
       Application::instance().mainWindow(),
       [] { Application::instance().mainWindow()->showPluginWindow(); },
@@ -580,7 +581,7 @@ bool PluginHost::clapGuiRequestShow() noexcept {
    return true;
 }
 
-bool PluginHost::clapGuiRequestHide() noexcept {
+bool PluginHost::guiRequestHide() noexcept {
    QMetaObject::invokeMethod(
       Application::instance().mainWindow(),
       [] { Application::instance().mainWindow()->hidePluginWindow(); },
@@ -589,7 +590,7 @@ bool PluginHost::clapGuiRequestHide() noexcept {
    return true;
 }
 
-void PluginHost::clapGuiClosed(bool wasDestroyed) noexcept { checkForMainThread(); }
+void PluginHost::guiClosed(bool wasDestroyed) noexcept { checkForMainThread(); }
 
 void PluginHost::processBegin(int nframes) {
    g_thread_type = ThreadType::AudioThread;
@@ -1062,8 +1063,7 @@ void PluginHost::paramsRescan(uint32_t flags) noexcept {
       paramsChanged();
 }
 
-void PluginHost::paramsClear(clap_id param_id,
-                             clap_param_clear_flags flags) noexcept {
+void PluginHost::paramsClear(clap_id param_id, clap_param_clear_flags flags) noexcept {
    checkForMainThread();
 }
 
@@ -1188,7 +1188,7 @@ void PluginHost::remoteControlsChanged() noexcept {
       std::ostringstream msg;
       msg << "Plugin called clap_host_remote_controls.changed() but does not provide "
              "clap_plugin_remote_controls";
-      throw std::logic_error(msg.str());
+      std::terminate();
    }
 
    scanQuickControls();
@@ -1216,7 +1216,8 @@ bool PluginHost::loadNativePluginPreset(const std::string &path) {
    if (!_pluginPresetLoad->from_location)
       throw std::logic_error("clap_plugin_preset_load does not implement load_from_uri");
 
-   return _pluginPresetLoad->from_location(_plugin, CLAP_PRESET_DISCOVERY_LOCATION_FILE, path.c_str(), nullptr);
+   return _pluginPresetLoad->from_location(
+      _plugin, CLAP_PRESET_DISCOVERY_LOCATION_FILE, path.c_str(), nullptr);
 }
 
 void PluginHost::stateMarkDirty() noexcept {
diff --git a/host/plugin-host.hh b/host/plugin-host.hh
index a75e83d..4b18acc 100644
--- a/host/plugin-host.hh
+++ b/host/plugin-host.hh
@@ -23,7 +23,14 @@
 
 class Engine;
 class PluginHostSettings;
-class PluginHost final : public clap::helpers::Host, public QObject {
+
+constexpr auto PluginHost_MH = clap::helpers::MisbehaviourHandler::Terminate;
+constexpr auto PluginHost_CL = clap::helpers::CheckingLevel::Maximal;
+
+using BaseHost = clap::helpers::Host<PluginHost_MH, PluginHost_CL>;
+extern template class clap::helpers::Host<PluginHost_MH, PluginHost_CL>;
+
+class PluginHost final : public QObject, public BaseHost {
    Q_OBJECT;
 
 public:
@@ -89,56 +96,55 @@ protected:
    /////////////////////////
 
    // clap_host
-   virtual void requestRestart() noexcept override;
-   virtual void requestProcess() noexcept override;
-   virtual void requestCallback() noexcept override;
+   void requestRestart() noexcept override;
+   void requestProcess() noexcept override;
+   void requestCallback() noexcept override;
 
    // clap_host_gui
-   virtual bool implementsGui() const noexcept override { return true; }
-   virtual void guiResizeHintsChanged() noexcept override;
-   virtual bool guiRequestResize(uint32_t width, uint32_t height) noexcept override;
-   virtual bool guiRequestShow() noexcept override;
-   virtual bool guiRequestHide() noexcept override;
-   virtual void guiClosed(bool wasDestroyed) noexcept override;
+   bool implementsGui() const noexcept override { return true; }
+   void guiResizeHintsChanged() noexcept override;
+   bool guiRequestResize(uint32_t width, uint32_t height) noexcept override;
+   bool guiRequestShow() noexcept override;
+   bool guiRequestHide() noexcept override;
+   void guiClosed(bool wasDestroyed) noexcept override;
 
    // clap_host_log
-   virtual bool implementsLog() const noexcept override { return true; }
-   virtual void logLog(clap_log_severity severity, const char *message) const noexcept override;
+   bool implementsLog() const noexcept override { return true; }
+   void logLog(clap_log_severity severity, const char *message) const noexcept override;
 
    // clap_host_params
-   virtual bool implementsParams() const noexcept override { return true; }
-   virtual void paramsRescan(clap_param_rescan_flags flags) noexcept override;
-   virtual void paramsClear(clap_id paramId, clap_param_clear_flags flags) noexcept override;
-   virtual void paramsRequestFlush() noexcept;
+   bool implementsParams() const noexcept override { return true; }
+   void paramsRescan(clap_param_rescan_flags flags) noexcept override;
+   void paramsClear(clap_id paramId, clap_param_clear_flags flags) noexcept override;
+   void paramsRequestFlush() noexcept override;
 
    // clap_host_posix_fd_support
-   virtual bool implementsPosixFdSupport() const noexcept override { return true; }
-   virtual bool posixFdSupportRegisterFd(int fd, clap_posix_fd_flags_t flags) noexcept override;
-   virtual bool posixFdSupportModifyFd(int fd, clap_posix_fd_flags_t flags) noexcept override;
-   virtual bool posixFdSupportUnregisterFd(int fd) noexcept override;
+   bool implementsPosixFdSupport() const noexcept override { return true; }
+   bool posixFdSupportRegisterFd(int fd, clap_posix_fd_flags_t flags) noexcept override;
+   bool posixFdSupportModifyFd(int fd, clap_posix_fd_flags_t flags) noexcept override;
+   bool posixFdSupportUnregisterFd(int fd) noexcept override;
 
    // clap_host_remote_controls
-   virtual bool implementsRemoteControls() const noexcept override { return true; }
-   virtual void remoteControlsChanged() noexcept override;
-   virtual void remoteControlsSuggestPage(clap_id pageId) noexcept override {}
+   bool implementsRemoteControls() const noexcept override { return true; }
+   void remoteControlsChanged() noexcept override;
+   void remoteControlsSuggestPage(clap_id pageId) noexcept override;
 
    // clap_host_state
-   virtual bool implementsState() const noexcept override { return true; }
-   virtual void stateMarkDirty() noexcept override;
+   bool implementsState() const noexcept override { return true; }
+   void stateMarkDirty() noexcept override;
 
    // clap_host_timer_support
-   virtual bool implementsTimerSupport() const noexcept override { return true; }
-   virtual bool timerSupportRegisterTimer(uint32_t periodMs, clap_id *timerId) noexcept override;
-   virtual bool timerSupportUnregisterTimer(clap_id timerId) noexcept override;
+   bool implementsTimerSupport() const noexcept override { return true; }
+   bool timerSupportRegisterTimer(uint32_t periodMs, clap_id *timerId) noexcept override;
+   bool timerSupportUnregisterTimer(clap_id timerId) noexcept override;
 
    // clap_host_thread_check
-   virtual bool implementsThreadCheck() const noexcept override { return true; }
-   virtual bool threadCheckIsMainThread() noexcept override;
-   virtual bool threadCheckIsAudioThread() noexcept override;
+   bool threadCheckIsMainThread() noexcept override;
+   bool threadCheckIsAudioThread() noexcept override;
 
    // clap_host_thread_pool
-   virtual bool implementsThreadPool() const noexcept override { return true; }
-   virtual bool threadPoolRequestExec(uint32_t numTasks) noexcept override;
+   bool implementsThreadPool() const noexcept override { return true; }
+   bool threadPoolRequestExec(uint32_t numTasks) noexcept override;
 
 private:
    template <typename T>