osb/source/core/StarDirectives.cpp

134 lines
3.3 KiB
C++
Raw Normal View History

2023-06-23 15:30:55 +00:00
#include "StarImage.hpp"
#include "StarImageProcessing.hpp"
#include "StarDirectives.hpp"
2023-06-24 03:06:13 +00:00
#include "StarXXHash.hpp"
2023-06-23 15:30:55 +00:00
namespace Star {
2023-06-24 09:41:52 +00:00
Directives::Directives() {}
Directives::Directives(String const& directives) {
parse(directives);
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
Directives::Directives(String&& directives) {
String mine = move(directives);
parse(mine);
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
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());
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
void Directives::buildString(String& out) const {
for (auto& entry : *entries) {
out += "?";
out += entry.string;
}
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
DirectivesGroup::DirectivesGroup() : m_count(0) {}
DirectivesGroup::DirectivesGroup(String const& directives) {
m_directives.emplace_back(directives);
m_count = m_directives.back().entries->size();
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
DirectivesGroup::DirectivesGroup(String&& directives) {
m_directives.emplace_back(move(directives));
m_count = m_directives.back().entries->size();
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
inline bool DirectivesGroup::empty() const {
return m_count == 0;
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
inline bool DirectivesGroup::compare(DirectivesGroup const& other) const {
if (m_count != other.m_count)
return false;
2023-06-23 15:30:55 +00:00
2023-06-24 09:41:52 +00:00
if (empty())
return true;
2023-06-23 15:30:55 +00:00
2023-06-24 09:41:52 +00:00
return hash() == other.hash();
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
inline bool DirectivesGroup::operator==(DirectivesGroup const& other) const {
return compare(other);
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
inline bool DirectivesGroup::operator!=(DirectivesGroup const& other) const {
return !compare(other);
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
void DirectivesGroup::append(Directives const& directives) {
m_directives.push_back(directives);
m_count += m_directives.back().entries->size();
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
DirectivesGroup& DirectivesGroup::operator+=(Directives const& other) {
append(other);
return *this;
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
inline String DirectivesGroup::toString() const {
String string;
addToString(string);
return string;
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
void DirectivesGroup::addToString(String& string) const {
for (auto& directives : m_directives)
directives.buildString(string);
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
void DirectivesGroup::forEach(DirectivesCallback callback) const {
for (auto& directives : m_directives) {
for (auto& entry : *directives.entries)
callback(entry);
2023-06-24 03:06:13 +00:00
}
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
bool DirectivesGroup::forEachAbortable(AbortableDirectivesCallback callback) const {
for (auto& directives : m_directives) {
for (auto& entry : *directives.entries) {
if (!callback(entry))
return false;
2023-06-24 03:06:13 +00:00
}
}
2023-06-24 09:41:52 +00:00
return true;
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
inline Image DirectivesGroup::applyNewImage(Image const& image) const {
Image result = image;
applyExistingImage(result);
return result;
2023-06-24 03:06:13 +00:00
}
2023-06-24 09:41:52 +00:00
void DirectivesGroup::applyExistingImage(Image& image) const {
forEach([&](auto const& entry) {
processImageOperation(entry.operation, image);
});
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
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();
2023-06-23 15:30:55 +00:00
}
2023-06-24 09:41:52 +00:00
inline size_t hash<DirectivesGroup>::operator()(DirectivesGroup const& s) const {
return s.hash();
2023-06-23 15:30:55 +00:00
}
}