-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshNetwork.mpp
63 lines (51 loc) · 1.63 KB
/
MeshNetwork.mpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
export module CppUtils.Container.MeshNetwork;
import std;
import CppUtils.Thread.SharedLocker;
import CppUtils.Container.NetworkPtr;
export namespace CppUtils::Container
{
template<class Key, class Value>
struct MeshNode final
{
using SharedPtr = NetworkPtr<MeshNode<Key, Value>>::SharedPtr;
using WeakPtr = NetworkPtr<MeshNode<Key, Value>>::WeakPtr;
explicit inline MeshNode(auto&&... args):
value{std::forward<decltype(args)>(args)...}
{}
[[nodiscard]] inline auto operator[](const Key& key) const -> const auto&
{
return m_branches.at(key);
}
[[nodiscard]] inline auto operator[](const Key& key) -> decltype(auto)
{
return m_branches[key];
}
static inline auto attach(SharedPtr& node0, const Key& key1, SharedPtr& node1) -> void
{
auto node0Accessor = node0->uniqueAccess();
node0Accessor->attachChild(node1);
node0Accessor->value[key1].push_back(node1);
}
static inline auto detach(SharedPtr& node0, const Key& key1, SharedPtr& node1) -> void
{
auto node0Accessor = node0->uniqueAccess();
node0Accessor->value.erase(key1);
node0Accessor->detachChild(node1);
}
static inline auto attach(const Key& key0, SharedPtr& node0, const Key& key1, SharedPtr& node1) -> void
{
attach(node0, key1, node1);
attach(node1, key0, node0);
}
static inline auto detach(const Key& key0, SharedPtr& node0, const Key& key1, SharedPtr& node1) -> void
{
detach(node0, key1, node1);
detach(node1, key0, node0);
}
Value value;
private:
std::unordered_map<Key, std::vector<WeakPtr>> m_branches;
};
template<class Key, class Value>
using MeshNodePtr = NetworkPtr<MeshNode<Key, Value>>;
}