Repository navigation
Expand file tree
/
Copy pathShape.cpp
More file actions
87 lines (71 loc) · 2.42 KB
/
Copy pathShape.cpp
File metadata and controls
87 lines (71 loc) · 2.42 KB
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
#include <algorithm>
#include "drawing.h"
namespace drawing {
void swap(Shape &first, Shape &second) {
using std::swap;
swap(first.colour, second.colour);
swap(first.selected, second.selected);
swap(first.observers, second.observers);
for_each(first.observers.cbegin(), first.observers.cend(), [&](const auto& observer) {
observer->Delete(second, false);
observer->Update(first, false);
});
for_each(second.observers.cbegin(), second.observers.cend(), [&](const auto& observer) {
observer->Delete(first, false);
observer->Update(second, false);
});
}
Shape::Shape(const int &colour, const bool &selected) noexcept:
colour{colour}, selected{selected} { }
Shape::Shape(const Shape& other) noexcept:
colour{other.colour}, selected{other.selected} { }
Shape::~Shape() noexcept {
for_each(this->observers.cbegin(), this->observers.cend(), [&](const auto& observer) {
observer->Delete(*this, this->selected);
});
}
void Shape::Attach(Observer& observer) {
this->observers.push_back(&observer);
}
uint Shape::Detach(const Observer& observer, bool all) {
uint erased{};
auto iterator = this->observers.begin();
while (iterator != this->observers.end() && (all or not erased)) {
if (*iterator == &observer) {
iterator = this->observers.erase(iterator);
erased++;
}
else
++iterator;
}
return erased;
}
[[nodiscard]] bool Shape::Selected() const {
return this->selected;
}
bool Shape::Selected(bool value) {
const auto prev_value = this->selected;
if (prev_value != value) {
this->selected = value;
if (value)
this->Notify();
}
return prev_value;
}
[[nodiscard]] int Shape::Colour() const {
return this->colour;
}
int Shape::Colour(int value) {
const auto prev_value = this->colour;
if (prev_value != value) {
this->colour = value;
this->Notify();
}
return prev_value;
}
void Shape::Notify() {
for_each(this->observers.cbegin(), this->observers.cend(), [&](const auto& observer) {
observer->Update(*this, this->selected);
});
}
}