diff --git a/include/flucoma/clients/common/FluidBaseClient.hpp b/include/flucoma/clients/common/FluidBaseClient.hpp index ea7ac98f..3aeeb3f9 100644 --- a/include/flucoma/clients/common/FluidBaseClient.hpp +++ b/include/flucoma/clients/common/FluidBaseClient.hpp @@ -20,6 +20,8 @@ under the European Union’s Horizon 2020 research and innovation programme #include "TupleUtilities.hpp" #include "../../data/FluidIndex.hpp" #include "../../data/FluidMeta.hpp" +#include +#include #include namespace fluid { @@ -29,6 +31,18 @@ namespace client { struct ModelObject {}; +// Mixin for shared clients that want to know the name they are registered +// under (e.g. for printing). The name is not a parameter: it is owned by +// NRTSharedInstanceAdaptor, which keeps this copy up to date. +struct NamedObject +{ + void setName(std::string_view name) { mName = name; } + const std::string& name() const { return mName; } + +private: + std::string mName; +}; + enum ProcessState { kNoProcess, kProcessing, kDone, kDoneStillProcessing }; struct ControlChannel { diff --git a/include/flucoma/clients/common/MessageSet.hpp b/include/flucoma/clients/common/MessageSet.hpp index 1ffa090d..142e55ad 100644 --- a/include/flucoma/clients/common/MessageSet.hpp +++ b/include/flucoma/clients/common/MessageSet.hpp @@ -162,6 +162,9 @@ class MessageSet> return std::get(mMessages)(std::forward(us)...); } + /// The underlying tuple of messages, e.g. for concatenating sets + const MessagesType& messages() const noexcept { return mMessages; } + private: MessagesType mMessages; diff --git a/include/flucoma/clients/nrt/CorpusClient.hpp b/include/flucoma/clients/nrt/CorpusClient.hpp index dd700f32..fbd4c474 100644 --- a/include/flucoma/clients/nrt/CorpusClient.hpp +++ b/include/flucoma/clients/nrt/CorpusClient.hpp @@ -30,12 +30,14 @@ namespace fluid { namespace client { namespace corpus { -enum { kName }; constexpr auto CorpusParams = - defineParameters(StringParam>("name", "Corpus name")); + defineParameters(); -class CorpusClient : public FluidBaseClient, public OfflineIn, public OfflineOut +class CorpusClient : public FluidBaseClient, + public OfflineIn, + public OfflineOut, + public NamedObject { using string = std::string; using Buffer = typename BufferT::type; @@ -72,8 +74,6 @@ class CorpusClient : public FluidBaseClient, public OfflineIn, public OfflineOut return {}; } - std::string name() const { return get(); } - MessageResult addPoint(string label, Buffer buffer, size_t offset, int length) { diff --git a/include/flucoma/clients/nrt/DataClient.hpp b/include/flucoma/clients/nrt/DataClient.hpp index 292afd40..537fa6a2 100644 --- a/include/flucoma/clients/nrt/DataClient.hpp +++ b/include/flucoma/clients/nrt/DataClient.hpp @@ -37,7 +37,6 @@ class DataClient return OK(); } - MessageResult write(string fileName) { auto file = JSONFile(fileName, "w"); diff --git a/include/flucoma/clients/nrt/DataSetClient.hpp b/include/flucoma/clients/nrt/DataSetClient.hpp index 70a96f1c..1d7c4741 100644 --- a/include/flucoma/clients/nrt/DataSetClient.hpp +++ b/include/flucoma/clients/nrt/DataSetClient.hpp @@ -22,10 +22,9 @@ namespace fluid { namespace client { namespace dataset { -enum { kName }; constexpr auto DataSetParams = - defineParameters(StringParam>("name", "Name of the DataSet")); + defineParameters(); template double distance(FluidTensorView point1, FluidTensorView point2) @@ -53,6 +52,7 @@ auto sortedDistances(FluidTensorView x, FluidTensorView Y, class DataSetClient : public FluidBaseClient, OfflineIn, OfflineOut, + public NamedObject, public DataClient> { public: @@ -306,7 +306,7 @@ class DataSetClient : public FluidBaseClient, MessageResult print() { - return "DataSet " + std::string(get()) + ": " + mAlgorithm.print(); + return "DataSet " + name() + ": " + mAlgorithm.print(); } const DataSet& getDataSet() const { return mAlgorithm; } diff --git a/include/flucoma/clients/nrt/FluidSharedInstanceAdaptor.hpp b/include/flucoma/clients/nrt/FluidSharedInstanceAdaptor.hpp index 8c575b41..cd41279e 100644 --- a/include/flucoma/clients/nrt/FluidSharedInstanceAdaptor.hpp +++ b/include/flucoma/clients/nrt/FluidSharedInstanceAdaptor.hpp @@ -18,13 +18,19 @@ under the European Union’s Horizon 2020 research and innovation programme #include "../../data/FluidTensor.hpp" #include "../../data/TensorTypes.hpp" #include "../../data/FluidMemory.hpp" +#include #include #include +#include #include +#include namespace fluid { namespace client { +template +class NRTSharedInstanceAdaptor; + template class ParamAliasAdaptor; @@ -45,6 +51,7 @@ class ParamAliasAdaptor> ParamSetType params{ClientWrapper::getParameterDescriptors(), FluidDefaultAllocator()}; ListenersArray listeners; + rt::string name; // key in the lookup tables; shared by all aliases std::shared_ptr getShared() { return shared_from_this(); }; }; @@ -66,13 +73,10 @@ class ParamAliasAdaptor> ~ParamAliasAdaptor() { - if (mParams && - mParams.use_count() == 1) // is this the last remaining user of this - // Corpus, except the hash table? - { - auto name = mParams->params.template get<0>(); - mParamsTable.erase(name); // then remove it from the universe - } + // copies share one binding: only the last copy, if it is also the last + // user of the data (except the hash table), removes it from the universe + if (mParams.lastCopy() && mParams.data().use_count() == 1) + eraseEntryFor(mParams.data()); } typename WrappedClient::ParamSetType& instance() { return mParams->params; } @@ -112,20 +116,79 @@ class ParamAliasAdaptor> Result lookup(rt::string name) { - return mParamsTable.count(name) - ? Result{} - : Result{Result::Status::kWarning, name, " not found"}; + return find(name) ? Result{} + : Result{Result::Status::kWarning, name, " not found"}; } + /// Point this object, and every copy of it, at the data registered under + /// name. The listeners it added go along, and fire so that hosts can + /// refresh what they display. Result refer(rt::string name) { - if (mParamsTable.count(name)) + ParamsPointer target = find(name); + if (!target) return {Result::Status::kWarning, name, " not found"}; + if (target == mParams.data()) return {}; + + ParamsPointer old = mParams.data(); + auto moved = moveListeners(*old, *target); + mParams.data() = target; + // were we the last user of the old data? then remove it from the universe + if (old.use_count() == 1) eraseEntryFor(old); + for (auto&& l : moved) l.first(); + return {}; + } + + /// The name this object's shared data is registered under + const rt::string& name() const { return mParams->name; } + + /// Give this object its name at creation time: if the name is already in + /// use, start sharing that entry, otherwise register a new one. + Result setName(rt::string name) + { + if (!name.size()) + return {Result::Status::kWarning, + "Shared object given no name – won't be shared!"}; + + if (!find(name)) // no live entry under this name + { + mParams->name = name; + mParamsTable.insert_or_assign(name, mParams.data()); // over any dead one + } + + return refer(name); + } + + /// Change the name the shared data is registered under. The data and + /// parameters are untouched, and every object sharing them sees the new + /// name. Fails if the new name is empty or already in use. + Result rename(rt::string newName) + { + if (!newName.size()) + return {Result::Status::kError, "Can't rename: no name given"}; + + if (newName == mParams->name) return {}; + + // a client that outlived its data (still held somewhere) keeps its name + if (find(newName) || NRTSharedInstanceAdaptor::lookup(newName)) + return {Result::Status::kError, "Can't rename: ", newName, + " already exists"}; + + mParamsTable.erase(newName); // a dead entry, if any, is in the way + rt::string oldName = mParams->name; + auto entry = mParamsTable.find(oldName); + + if (entry != mParamsTable.end() && entry->second.lock() == mParams.data()) { - mParams = mParamsTable[name].lock()->getShared(); - return {}; + auto node = mParamsTable.extract(entry); + node.key() = newName; + mParamsTable.insert(std::move(node)); } - else - return {Result::Status::kWarning, name, " not found"}; + else // was unnamed (and hence unshared) until now + mParamsTable.emplace(newName, mParams.data()); + + mParams->name = newName; + NRTSharedInstanceAdaptor::rekey(oldName, newName); + return {}; } template @@ -137,26 +200,8 @@ class ParamAliasAdaptor> template