Skip to content

Commit

Permalink
Add a Runtime::snapshot method for nodeMap state serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Nov 20, 2023
1 parent 65047e5 commit f8cd55a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions runtime/elem/GraphNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace elem
template <typename ValueType>
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.
Expand Down Expand Up @@ -122,4 +125,9 @@ namespace elem
return df;
}

template <typename FloatType>
js::Object GraphNode<FloatType>::getProperties() {
return js::Object(props.begin(), props.end());
}

} // namespace elem
17 changes: 17 additions & 0 deletions runtime/elem/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ namespace elem
// Intentionally, this does not provide access to the values in the map.
typename SharedResourceMap<FloatType>::KeyViewType getSharedResourceMapKeys();

//==============================================================================
// For registering custom GraphNode factory functions.
//
// New node types must inherit GraphNode, and the factory function must produce a shared
Expand All @@ -98,6 +99,10 @@ namespace elem
using NodeFactoryFn = std::function<std::shared_ptr<GraphNode<FloatType>>(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
Expand Down Expand Up @@ -419,6 +424,18 @@ namespace elem
return ReturnCode::Ok();
}

template <typename FloatType>
js::Object Runtime<FloatType>::snapshot()
{
js::Object ret;

for (auto& [nodeId, node] : nodeTable) {
ret.insert({nodeIdToHex(nodeId), node->getProperties()});
}

return ret;
}

//==============================================================================
template <typename FloatType>
void Runtime<FloatType>::traverse(std::set<NodeId>& visited, std::vector<NodeId>& visitOrder, NodeId const& n) {
Expand Down

0 comments on commit f8cd55a

Please sign in to comment.