blob: 981835fb787743bab238306824d0cd9b612bcba2 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
|
#pragma once
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
using cid = struct concept *;
struct concept
{
// a concept is made of concept-typed links to other concepts
std::multimap<cid,cid> links;
using array = std::vector<cid>;
cid id();
cid get(cid type); // returns first
array getAll(cid type);
void link(cid type, cid target);
void unlink(cid type, cid target);
};
template <typename T>
struct value : public concept, public T
{
static value<T>& of(cid c)
{
return *static_cast<value<T>*>(c);
}
};
cid concept::id()
{
return this;
}
cid concept::get(cid type)
{
auto result = links.equal_range(type);
if (result.first == result.second) {
throw std::out_of_range("no such concept link to get");
}
return result.first->second;
}
concept::array concept::getAll(cid type)
{
array ret;
for (
auto range = links.equal_range(type);
range.first != range.second;
++ range.first
) {
ret.push_back(range.first->second);
}
return ret;
}
void concept::link(cid type, cid target)
{
links.insert({type, target});
}
void concept::unlink(cid type, cid target)
{
auto ls = links.equal_range(type);
for (auto l = ls.first; l != ls.second; ++ l) {
if (l->second == target) {
links.erase(l);
return;
}
}
throw std::out_of_range("no such concept link to erase");
}
|