MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
CachedNode.hpp
1 #pragma once
2 
3 #include "Export.hpp"
4 
5 #include "Graph.hpp"
6 #include "StreamConsumer.hpp"
7 
8 #include "ValueStream.hpp"
9 #include "ValueStreamChannels.hpp"
10 
11 #include "Hashes.hpp"
12 
13 #include <unordered_map>
14 
15 namespace Showcase
16 {
17 
18  class SHOWCASE_API CachedNode : public std::enable_shared_from_this<CachedNode>
19  {
20  public:
21  struct Cache
22  {
23  PropertyMap properties;
24  std::map<NodeId, PropertyMap> edges;
25 
26  NodeEventStreamPtr childStream;
27  PropertyEventStreamPtr propertyStream;
28  std::unordered_map<EdgeId, PropertyEventStreamPtr> edgePropertyStreams;
29  };
30 
33  struct PausedCache
34  {
35  public:
36  PropertyMap properties;
37  std::map<NodeId, PropertyMap> edges;
38 
39  private:
41  std::vector<ChannelGuard<ValueStreamChannel<PropertyEvent>>> propertyGuards;
42 
43  friend class CachedNode;
44  };
45 
46  typedef std::function<void(const PropertyEvent& ev)> PropFunc;
47  typedef std::function<void(const NodeId& child, const PropertyEvent& ev)> EdgePropFunc;
48  typedef std::function<void(const NodeEvent& ev)> ChildFunc;
49 
55 
58  CachedNode(GraphPtr graph, const NodeId& node, std::size_t maxChildren=0);
59  ~CachedNode();
60 
61  void startStreams();
62 
65  void injectEvent(const NodeEvent& ev);
66 
67  const NodeId& nodeId() const;
68 
69  PausedCache pausedCache();
70  Cache cache();
72  std::shared_ptr<CachedNode> clone();
73 
75  void setOnChildEvent(ChildFunc f);
76  void setOnPropertyEvent(PropFunc f);
77  void setOnEdgePropertyEvent(EdgePropFunc f);
78 
79  void addOnChildEvent(ChildFunc f);
80  void addOnPropertyEvent(PropFunc f);
81  void addOnEdgePropertyEvent(EdgePropFunc f);
82 
83  void setChildrenPaused(bool paused);
84 
85  private:
86  void onChildEvent(const NodeEvent& ev);
87  void onEdgePropertyEvent(const NodeId& childId, const PropertyEvent& ev);
88  void onPropertyEvent(const PropertyEvent& ev);
89 
90  struct Key {};
91 
92  public:
94  CachedNode(Key& k);
95 
96  private:
97  typedef CallbackLock<PropertyEvent> PropEvType;
98  typedef CallbackLock<NodeEvent> NodeEvType;
99 
100  typedef CloneableValueStreamChannelPtr<NodeEvent> NodeEventChannelPtr;
101  typedef CloneableValueStreamChannelPtr<PropertyEvent> PropertyEventChannelPtr;
102 
103  void initChildConsumer();
104  void initPropertyConsumer();
105  StreamConsumerPtr<PropEvType> initEdgePropertyConsumer(const NodeId& childId);
106 
109  void handlePropertyEvent(PropertyMap& targetMap, const PropertyEvent& ev);
110 
111  NodeId m_nodeId;
112  std::weak_ptr<Graph> m_graph;
113 
115  std::size_t m_maxChildren;
116 
118  PropertyMap m_properties;
122  std::map<NodeId, PropertyMap> m_edges;
123 
125  std::mutex m_childMutex;
126 
128  StreamConsumerPtr<NodeEvType> m_childConsumer;
129  StreamConsumerPtr<PropEvType> m_propertyConsumer;
130  std::unordered_map<EdgeId, StreamConsumerPtr<PropEvType>> m_edgePropertyConsumers;
131 
134  NodeEventChannelPtr m_childStream;
135  PropertyEventChannelPtr m_propertyStream;
136  std::unordered_map<EdgeId, PropertyEventChannelPtr> m_edgePropertyStreams;
137 
138  std::vector<ChildFunc> m_onChild;
139  std::vector<EdgePropFunc> m_onEdgeProp;
140  std::vector<PropFunc> m_onProp;
141  };
142  typedef std::shared_ptr<CachedNode> CachedNodePtr;
143 
144 }