Enzo
Loading...
Searching...
No Matches
ExpressionContext.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/NetworkGraph/Unit.h"
4#include <atomic>
5#include <map>
6#include <mutex>
7#include <optional>
8#include <tuple>
9#include <variant>
10#include <vector>
11
12namespace enzo::expr {
13
28{
29 public:
30 explicit ExpressionContext(nt::NodeId currentNode) : currentNode_(currentNode) {}
31
33 nt::NodeId currentNode() const { return currentNode_; }
34
42 template <typename Value>
43 std::optional<Value> readParameter(const String& path, unsigned int index) const;
44
46 const std::vector<nt::Unit>& getExpressionDependencies() const
47 {
48 return expressionDependencies_;
49 }
50
52 void recordTimeDependency() const { readsTime_ = true; }
53
55 bool dependsOnTime() const { return readsTime_; }
56
57 private:
58 using ParameterValue = std::variant<floatT, intT, String>;
59
60 // A read's path, component index and value type.
61 using ParameterRead = std::tuple<String, unsigned int, size_t>;
62
63 nt::NodeId currentNode_;
64
65 // Filled as prm() and friends resolve, so const reads can still accumulate.
66 mutable std::mutex readMutex_;
67 mutable std::map<ParameterRead, ParameterValue> parameterValues_;
68 mutable std::vector<nt::Unit> expressionDependencies_;
69 mutable std::atomic<bool> readsTime_ = false;
70};
71
73void submitExpressionDependencies(const ExpressionContext& context);
74
75extern template std::optional<floatT>
76ExpressionContext::readParameter<floatT>(const String&, unsigned int) const;
77extern template std::optional<intT>
78ExpressionContext::readParameter<intT>(const String&, unsigned int) const;
79extern template std::optional<String>
80ExpressionContext::readParameter<String>(const String&, unsigned int) const;
81
82} // namespace enzo::expr
Basic attribute, parameter, and node types for Enzo.
uint64_t NodeId
The unique ID assigned to each node in the network.
Definition Types.h:335
The world a single expression evaluation reads and writes to.
Definition ExpressionContext.h:28
bool dependsOnTime() const
Returns whether the expression read the scene time during this evaluation.
Definition ExpressionContext.h:55
std::optional< Value > readParameter(const String &path, unsigned int index) const
Returns a parameter's value, evaluated on the first read and reused after.
Definition ExpressionContext.cpp:19
void recordTimeDependency() const
Notes that the expression read the scene time.
Definition ExpressionContext.h:52
const std::vector< nt::Unit > & getExpressionDependencies() const
Every parameter the expression read during this evaluation.
Definition ExpressionContext.h:46
nt::NodeId currentNode() const
The node a relative parameter path resolves against.
Definition ExpressionContext.h:33