-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkingcontext.cpp
58 lines (49 loc) · 1.2 KB
/
linkingcontext.cpp
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
#include "linkingcontext.h"
LinkingContext::LinkingContext() : _nextId{kNotFound}
{
}
id_t LinkingContext::getId(SObject *obj, bool createIfNotFound)
{
Q_ASSERT(obj != nullptr);
id_t result = _objectToIdMap.value(obj, kNotFound);
if (result == kNotFound && createIfNotFound) {
result = _nextId + 1;
this->add(result, obj);
}
return result;
}
SObject *LinkingContext::getObject(id_t id)
{
return _idToObjectMap.value(id, nullptr);
}
void LinkingContext::add(id_t id, SObject *obj)
{
if (obj) {
_idToObjectMap.insert(id, obj);
_objectToIdMap.insert(obj, id);
if (id > _nextId) {
_nextId = id;
}
}
}
void LinkingContext::removeObject(SObject *obj)
{
if (obj) {
auto id = _objectToIdMap.value(obj, kNotFound);
if (id != kNotFound) {
this->doRemove(id, obj);
}
}
}
void LinkingContext::removeId(id_t id)
{
auto obj = _idToObjectMap.value(id, nullptr);
if (obj) {
this->doRemove(id, obj);
}
}
void LinkingContext::doRemove(id_t id, SObject *obj)
{
_idToObjectMap.remove(id);
_objectToIdMap.remove(obj);
}