-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.hpp
159 lines (127 loc) · 4.23 KB
/
link.hpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include "object.hpp"
#include "json.hpp"
using json = nlohmann::json;
class Link : public Object {
private:
std::pair<Object*, Object*> link_;
public:
Link() : Object() {}
Link(json& data) : Object(data) {}
Link(Object* object1, Object* object2) : Object() {
link_ = std::make_pair(object1, object2);
}
Link(Object* object) : Object() {
if (object->GetType() == ObjectType::Gift) {
link_.first = object;
link_.second = nullptr;
}
if (object->GetType() == ObjectType::Prayer) {
link_.second = object;
link_.first = nullptr;
}
}
Link(const Link& other) : Object(other) {
link_ = std::make_pair(other.link_.first, other.link_.second);
}
Link(Link&& other) : Object(other) {
link_ = std::make_pair(other.link_.first, other.link_.second);
other.Unlink();
}
Link& operator=(Link&& other) {
if (this != &other) {
Object::operator=(std::move(other));
link_ = other.link_;
other.Unlink();
}
return *this;
}
Link& operator=(const Link& other) {
return *this = Link(other);
}
~Link() {
Unlink();
}
void Unlink() {
link_.first = nullptr;
link_.second = nullptr;
}
bool UnlinkObject(const Object& object) {
if (link_.first != nullptr) {
if (!uuid_compare(link_.first->GetId(), object.GetId())) {
link_.first = nullptr;
return true;
}
}
if (link_.second != nullptr) {
if (!uuid_compare(link_.second->GetId(), object.GetId())) {
link_.second = nullptr;
return true;
}
}
return false;
}
bool IsObjectInLink(const Object& object) const {
if (link_.first != nullptr) {
if (!uuid_compare(link_.first->GetId(), object.GetId())) {
return true;
} else {
return false;
}
}
if (link_.second != nullptr) {
if (!uuid_compare(link_.second->GetId(), object.GetId())) {
return true;
} else {
return false;
}
}
return false;
}
bool IsLinked() const {
if (link_.first != nullptr && link_.second != nullptr)
return true;
return false;
}
bool HasLinked() const {
if (link_.first != nullptr || link_.second != nullptr)
return true;
return false;
}
bool LinkObject(Object* object) {
if (object->GetType() == ObjectType::Gift) {
link_.first = object;
return true;
}
if (object->GetType() == ObjectType::Prayer) {
link_.second = object;
return true;
}
return false;
}
const std::pair<Object*, Object*>& Get() const {
return link_;
}
template <typename T, ObjectType U>
T* GetObjectByType() {
if (U == ObjectType::Gift) {
return static_cast<T*>(link_.first);
} else if (U == ObjectType::Prayer) {
return static_cast<T*>(link_.second);
}
return nullptr;
}
virtual std::string ToJson() const {
json data;
data["id"] = Uuid();
if (link_.first != nullptr)
data["gift_id"] = link_.first->Uuid();
if (link_.second != nullptr)
data["prayer_id"] = link_.second->Uuid();
std::string json_data = data.dump();
return json_data;
}
virtual ObjectType GetType() const {
return ObjectType::Link;
}
};