blob: 8dcf8da5b4c5caae911b9d14318ebea477b1210e (
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#include "StarImage.hpp"
#include "StarImageProcessing.hpp"
#include "StarDirectives.hpp"
#include "StarXXHash.hpp"
namespace Star {
Directives::Entry::Entry(ImageOperation&& newOperation, String&& newString) {
operation = move(newOperation);
string = move(newString);
}
Directives::Entry::Entry(ImageOperation const& newOperation, String const& newString) {
operation = newOperation;
string = newString;
}
Directives::Entry::Entry(Entry const& other) {
operation = other.operation;
string = other.string;
}
Directives::Directives() {}
Directives::Directives(String const& directives) {
parse(directives);
}
Directives::Directives(String&& directives) {
String mine = move(directives);
parse(mine);
}
Directives::Directives(const char* directives) {
String string(directives);
parse(string);
}
Directives::Directives(List<Entry>&& newEntries) {
entries = std::make_shared<List<Entry> const>(move(newEntries));
String directives = toString(); // This needs to be better
hash = XXH3_64bits(directives.utf8Ptr(), directives.utf8Size());
}
void Directives::parse(String const& directives) {
List<Entry> newList;
StringList split = directives.split('?');
newList.reserve(split.size());
for (String& str : split) {
if (!str.empty()) {
ImageOperation operation = imageOperationFromString(str);
newList.emplace_back(move(operation), move(str));
}
}
entries = std::make_shared<List<Entry> const>(move(newList));
hash = XXH3_64bits(directives.utf8Ptr(), directives.utf8Size());
}
void Directives::buildString(String& out) const {
for (auto& entry : *entries) {
out += "?";
out += entry.string;
}
}
String Directives::toString() const {
String result;
buildString(result);
return result;
}
inline bool Directives::empty() const {
return entries->empty();
}
DataStream& operator>>(DataStream& ds, Directives& directives) {
String string;
ds.read(string);
directives.parse(string);
return ds;
}
DataStream& operator<<(DataStream& ds, Directives const& directives) {
ds.write(directives.toString());
return ds;
}
DirectivesGroup::DirectivesGroup() : m_count(0) {}
DirectivesGroup::DirectivesGroup(String const& directives) {
m_directives.emplace_back(directives);
m_count = m_directives.back().entries->size();
}
DirectivesGroup::DirectivesGroup(String&& directives) {
m_directives.emplace_back(move(directives));
m_count = m_directives.back().entries->size();
}
inline bool DirectivesGroup::empty() const {
return m_count == 0;
}
inline bool DirectivesGroup::compare(DirectivesGroup const& other) const {
if (m_count != other.m_count)
return false;
if (empty())
return true;
return hash() == other.hash();
}
void DirectivesGroup::append(Directives const& directives) {
m_directives.emplace_back(directives);
m_count += m_directives.back().entries->size();
}
void DirectivesGroup::append(List<Directives::Entry>&& entries) {
size_t size = entries.size();
m_directives.emplace_back(move(entries));
m_count += size;
}
void DirectivesGroup::clear() {
m_directives.clear();
m_count = 0;
}
DirectivesGroup& DirectivesGroup::operator+=(Directives const& other) {
append(other);
return *this;
}
inline String DirectivesGroup::toString() const {
String string;
addToString(string);
return string;
}
void DirectivesGroup::addToString(String& string) const {
for (auto& directives : m_directives)
directives.buildString(string);
}
void DirectivesGroup::forEach(DirectivesCallback callback) const {
for (auto& directives : m_directives) {
for (auto& entry : *directives.entries)
callback(entry);
}
}
bool DirectivesGroup::forEachAbortable(AbortableDirectivesCallback callback) const {
for (auto& directives : m_directives) {
for (auto& entry : *directives.entries) {
if (!callback(entry))
return false;
}
}
return true;
}
inline Image DirectivesGroup::applyNewImage(Image const& image) const {
Image result = image;
applyExistingImage(result);
return result;
}
void DirectivesGroup::applyExistingImage(Image& image) const {
forEach([&](auto const& entry) {
processImageOperation(entry.operation, image);
});
}
inline size_t DirectivesGroup::hash() const {
XXHash3 hasher;
for (auto& directives : m_directives)
hasher.push((const char*)&directives.hash, sizeof(directives.hash));
return hasher.digest();
}
bool operator==(DirectivesGroup const& a, DirectivesGroup const& b) {
return a.compare(b);
}
bool operator!=(DirectivesGroup const& a, DirectivesGroup const& b) {
return !a.compare(b);
}
size_t hash<DirectivesGroup>::operator()(DirectivesGroup const& s) const {
return s.hash();
}
}
|