|
| 1 | +#include "NUIE_NodeRegistry.hpp" |
| 2 | + |
| 3 | +namespace NUIE |
| 4 | +{ |
| 5 | + |
| 6 | +NodeRegistry::NodeData::NodeData (NodeRegistryId id, const std::wstring& name, const CreatorFunction& creator) : |
| 7 | + id (id), |
| 8 | + name (name), |
| 9 | + creator (creator) |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +NodeRegistry::GroupData::GroupData (const std::wstring& name) : |
| 14 | + name (name), |
| 15 | + nodes (nodes) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +void NodeRegistry::GroupData::AddNode (NodeRegistryId id) |
| 20 | +{ |
| 21 | + nodes.push_back (id); |
| 22 | +} |
| 23 | + |
| 24 | +const std::wstring& NodeRegistry::GroupData::GetName () const |
| 25 | +{ |
| 26 | + return name; |
| 27 | +} |
| 28 | + |
| 29 | +const std::vector<NodeRegistryId>& NodeRegistry::GroupData::GetNodes () const |
| 30 | +{ |
| 31 | + return nodes; |
| 32 | +} |
| 33 | + |
| 34 | +NodeRegistryId NodeRegistry::NodeData::GetNodeId () const |
| 35 | +{ |
| 36 | + return id; |
| 37 | +} |
| 38 | + |
| 39 | +const std::wstring& NodeRegistry::NodeData::GetNodeName () const |
| 40 | +{ |
| 41 | + return name; |
| 42 | +} |
| 43 | + |
| 44 | +const CreatorFunction & NodeRegistry::NodeData::GetCreatorFunction () const |
| 45 | +{ |
| 46 | + return creator; |
| 47 | +} |
| 48 | + |
| 49 | +NUIE::UINodePtr NodeRegistry::NodeData::CreateNode (const NUIE::Point& position) const |
| 50 | +{ |
| 51 | + return creator (position); |
| 52 | +} |
| 53 | + |
| 54 | +NodeRegistry::NodeRegistry () : |
| 55 | + nextNodeId (0) |
| 56 | +{ |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +void NodeRegistry::RegisterNode (const std::wstring& group, const std::wstring& name, const CreatorFunction& creator) |
| 61 | +{ |
| 62 | + auto foundGroup = nameToGroupData.find (group); |
| 63 | + if (foundGroup == nameToGroupData.end ()) { |
| 64 | + GroupData groupData (group); |
| 65 | + nameToGroupData.insert ({ group, groupRegistry.size () }); |
| 66 | + groupRegistry.push_back (groupData); |
| 67 | + } |
| 68 | + |
| 69 | + NodeRegistryId id = nextNodeId++; |
| 70 | + NodeData nodeData = { |
| 71 | + id, |
| 72 | + name, |
| 73 | + creator |
| 74 | + }; |
| 75 | + |
| 76 | + idToNodeData.insert ({ id, nodeRegistry.size () }); |
| 77 | + nameToNodeData.insert ({ name, nodeRegistry.size () }); |
| 78 | + groupRegistry[nameToGroupData[group]].AddNode (nodeRegistry.size ()); |
| 79 | + nodeRegistry.push_back (nodeData); |
| 80 | +} |
| 81 | + |
| 82 | +bool NodeRegistry::Contains (size_t id) const |
| 83 | +{ |
| 84 | + return GetNodeData (id) != nullptr; |
| 85 | +} |
| 86 | + |
| 87 | +bool NodeRegistry::Contains (const std::wstring& name) const |
| 88 | +{ |
| 89 | + return GetNodeData (name) != nullptr; |
| 90 | +} |
| 91 | + |
| 92 | +const NodeRegistry::NodeData* NodeRegistry::GetNodeData (size_t id) const |
| 93 | +{ |
| 94 | + auto found = idToNodeData.find (id); |
| 95 | + if (found == idToNodeData.end ()) { |
| 96 | + return nullptr; |
| 97 | + } |
| 98 | + return &nodeRegistry.at (found->second); |
| 99 | +} |
| 100 | + |
| 101 | +const NodeRegistry::NodeData* NodeRegistry::GetNodeData (const std::wstring& name) const |
| 102 | +{ |
| 103 | + auto found = nameToNodeData.find (name); |
| 104 | + if (found == nameToNodeData.end ()) { |
| 105 | + return nullptr; |
| 106 | + } |
| 107 | + return &nodeRegistry.at (found->second); |
| 108 | +} |
| 109 | + |
| 110 | +void NodeRegistry::EnumerateGroups (const std::function<void (const std::wstring&)>& processor) const |
| 111 | +{ |
| 112 | + for (const GroupData& group : groupRegistry) { |
| 113 | + processor (group.GetName ()); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void NodeRegistry::EnumerateGroupNodes (const std::wstring& groupName, const std::function<void (const NodeData&)>& processor) const |
| 118 | +{ |
| 119 | + auto found = nameToGroupData.find (groupName); |
| 120 | + if (found == nameToGroupData.end ()) { |
| 121 | + return; |
| 122 | + } |
| 123 | + const GroupData& groupData = groupRegistry[found->second]; |
| 124 | + for (NodeRegistryId nodeId : groupData.GetNodes ()) { |
| 125 | + processor (nodeRegistry[nodeId]); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} |
0 commit comments