From f8cd55ab87d2fc58dc63faf76471ce209b75d6ed Mon Sep 17 00:00:00 2001 From: Nick Thompson Date: Sun, 19 Nov 2023 19:47:55 -0500 Subject: [PATCH] Add a Runtime::snapshot method for nodeMap state serialization --- runtime/elem/GraphNode.h | 8 ++++++++ runtime/elem/Runtime.h | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/runtime/elem/GraphNode.h b/runtime/elem/GraphNode.h index c14b752..ec60888 100644 --- a/runtime/elem/GraphNode.h +++ b/runtime/elem/GraphNode.h @@ -59,6 +59,9 @@ namespace elem template ValueType getPropertyWithDefault(std::string const& key, ValueType const& df); + // Returns a copy of the entire property object in its current state + js::Object getProperties(); + // Process the next block of audio data. // // Users must override this method when creating a custom GraphNode. @@ -122,4 +125,9 @@ namespace elem return df; } + template + js::Object GraphNode::getProperties() { + return js::Object(props.begin(), props.end()); + } + } // namespace elem diff --git a/runtime/elem/Runtime.h b/runtime/elem/Runtime.h index 22a4312..051393e 100644 --- a/runtime/elem/Runtime.h +++ b/runtime/elem/Runtime.h @@ -87,6 +87,7 @@ namespace elem // Intentionally, this does not provide access to the values in the map. typename SharedResourceMap::KeyViewType getSharedResourceMapKeys(); + //============================================================================== // For registering custom GraphNode factory functions. // // New node types must inherit GraphNode, and the factory function must produce a shared @@ -98,6 +99,10 @@ namespace elem using NodeFactoryFn = std::function>(NodeId const id, double sampleRate, int const blockSize)>; int registerNodeType (std::string const& type, NodeFactoryFn && fn); + //============================================================================== + // Returns a copy of the internal graph state representing all known nodes and properties + js::Object snapshot(); + private: //============================================================================== // The rendering interface @@ -419,6 +424,18 @@ namespace elem return ReturnCode::Ok(); } + template + js::Object Runtime::snapshot() + { + js::Object ret; + + for (auto& [nodeId, node] : nodeTable) { + ret.insert({nodeIdToHex(nodeId), node->getProperties()}); + } + + return ret; + } + //============================================================================== template void Runtime::traverse(std::set& visited, std::vector& visitOrder, NodeId const& n) {