From 561cf0faf019f98da7c1986852111943f575fe4d Mon Sep 17 00:00:00 2001 From: Owen Green Date: Fri, 1 May 2020 16:04:20 +0100 Subject: [PATCH] Add persistence mechanism for shared clients --- include/FluidSCWrapper.hpp | 109 +++++++++++++++++++++- release-packaging/Classes/FluidDataSet.sc | 4 + 2 files changed, 109 insertions(+), 4 deletions(-) diff --git a/include/FluidSCWrapper.hpp b/include/FluidSCWrapper.hpp index b7766ae..8d446b7 100644 --- a/include/FluidSCWrapper.hpp +++ b/include/FluidSCWrapper.hpp @@ -12,6 +12,7 @@ under the European Union’s Horizon 2020 research and innovation programme #include "SCBufferAdaptor.hpp" #include +#include #include #include #include @@ -21,6 +22,7 @@ under the European Union’s Horizon 2020 research and innovation programme #include #include #include +#include #include #include @@ -188,6 +190,11 @@ public: mClient.process(mAudioInputs, mOutputs, mContext); } + ParamSetType& params() + { + return mParams; + } + private: std::vector mInputConnections; std::vector mOutputConnections; @@ -392,6 +399,11 @@ public: static_cast(unit)->mClient.cancel(); } + ParamSetType& params() + { + return mParams; + } + private: static Result validateParameters(NonRealTime* w) { @@ -473,6 +485,75 @@ class NonRealTimeAndRealTime : public RealTime, //////////////////////////////////////////////////////////////////////////////// +//Discovery for clients that need persistent storage (Dataset and friends) + +template +struct IsPersistent : std::false_type +{}; + +//TODO: make less tied to current implementation +template +struct IsPersistent>> + : std::true_type +{}; + + +template +struct ObjectPersistance; + +template +struct ObjectPersistance{ + void init(){} + static void setup(InterfaceTable*, const char*){} +}; + +template +struct ObjectPersistance +{ + + template struct GetSharedType; + + template + struct GetSharedType>> + { + using type = NRTSharedInstanceAdaptor; + }; + + using SharedType = typename GetSharedType::type; + using ClientPointer = typename SharedType::ClientPointer; + + void init() + { + auto clientRef = getClientPointer(static_cast(this)); + auto pos = mRegistry.find(clientRef); + if(pos == mRegistry.end()) mRegistry.emplace(clientRef); + } + + static void setup(InterfaceTable* ft, const char* name) + { + ft->fDefineUnitCmd(name, "free", [](Unit* unit, sc_msg_iter*) + { + auto clientRef = getClientPointer(static_cast(unit)); + auto pos = mRegistry.find(clientRef); + if(pos != mRegistry.end()) mRegistry.erase(clientRef); + }); + } + +private: + + static ClientPointer getClientPointer(Wrapper* wrapper) + { + auto& params = wrapper->params(); + auto name = params.template get<0>(); + return SharedType::lookup(name); + } + + static std::unordered_set mRegistry; +}; + +template +std::unordered_set::ClientPointer> + ObjectPersistance::mRegistry; // Template Specialisations for NRT/RT template @@ -480,13 +561,33 @@ class FluidSCWrapperImpl; template class FluidSCWrapperImpl - : public NonRealTime -{}; + : public NonRealTime, public ObjectPersistance::value> +{ +public: + void init(){ + NonRealTime::init(); + ObjectPersistance::value>::init(); + } + static void setup(InterfaceTable* ft, const char* name){ + NonRealTime::setup(ft,name); + ObjectPersistance::value>::setup(ft,name); + } +}; template class FluidSCWrapperImpl - : public RealTime -{}; + : public RealTime, public ObjectPersistance::value> +{ +public: + void init(){ + RealTime::init(); + ObjectPersistance::value>::init(); + } + static void setup(InterfaceTable* ft, const char* name){ + RealTime::setup(ft,name); + ObjectPersistance::value>::setup(ft,name); + } +}; //////////////////////////////////////////////////////////////////////////////// diff --git a/release-packaging/Classes/FluidDataSet.sc b/release-packaging/Classes/FluidDataSet.sc index dc1d074..d519509 100644 --- a/release-packaging/Classes/FluidDataSet.sc +++ b/release-packaging/Classes/FluidDataSet.sc @@ -57,4 +57,8 @@ FluidDataSet : FluidManipulationClient { this.pr_sendMsg(\clear,[],action); } + free { |action| + this.pr_sendMsg(\free,[],action); + } + } \ No newline at end of file