Skip to content

Commit b58a36c

Browse files
committed
Add NodeRegistry.
1 parent b021b5d commit b58a36c

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef NUIE_NODEREGISTRY_HPP
2+
#define NUIE_NODEREGISTRY_HPP
3+
4+
#include "NUIE_UINode.hpp"
5+
6+
namespace NUIE
7+
{
8+
9+
using NodeRegistryId = size_t;
10+
using CreatorFunction = std::function<NUIE::UINodePtr (const NUIE::Point& position)>;
11+
12+
class NodeRegistry
13+
{
14+
public:
15+
class NodeData
16+
{
17+
public:
18+
NodeData (NodeRegistryId id, const std::wstring& name, const CreatorFunction& creator);
19+
20+
NodeRegistryId GetNodeId () const;
21+
const std::wstring& GetNodeName () const;
22+
const CreatorFunction& GetCreatorFunction () const;
23+
NUIE::UINodePtr CreateNode (const NUIE::Point& position) const;
24+
25+
private:
26+
NodeRegistryId id;
27+
std::wstring name;
28+
CreatorFunction creator;
29+
};
30+
31+
class GroupData
32+
{
33+
public:
34+
GroupData (const std::wstring& name);
35+
36+
void AddNode (NodeRegistryId id);
37+
const std::wstring& GetName () const;
38+
const std::vector<NodeRegistryId>& GetNodes () const;
39+
40+
private:
41+
std::wstring name;
42+
std::vector<NodeRegistryId> nodes;
43+
};
44+
45+
NodeRegistry ();
46+
47+
void RegisterNode (const std::wstring& group, const std::wstring& name, const CreatorFunction& creator);
48+
49+
bool Contains (size_t id) const;
50+
bool Contains (const std::wstring& name) const;
51+
52+
const NodeData* GetNodeData (size_t id) const;
53+
const NodeData* GetNodeData (const std::wstring& name) const;
54+
55+
void EnumerateGroups (const std::function<void (const std::wstring&)>& processor) const;
56+
void EnumerateGroupNodes (const std::wstring& groupName, const std::function<void (const NodeData&)>& processor) const;
57+
58+
private:
59+
std::vector<GroupData> groupRegistry;
60+
std::vector<NodeData> nodeRegistry;
61+
std::unordered_map<NodeRegistryId, size_t> idToNodeData;
62+
std::unordered_map<std::wstring, size_t> nameToNodeData;
63+
std::unordered_map<std::wstring, size_t> nameToGroupData;
64+
NodeRegistryId nextNodeId;
65+
};
66+
67+
}
68+
69+
#endif

0 commit comments

Comments
 (0)