osb/source/core/StarDirectives.hpp

144 lines
4.2 KiB
C++
Raw Normal View History

#pragma once
2023-06-23 15:30:55 +00:00
#include "StarImageProcessing.hpp"
2023-06-24 09:41:52 +00:00
#include "StarHash.hpp"
2023-06-24 12:49:47 +00:00
#include "StarDataStream.hpp"
#include "StarStringView.hpp"
#include "StarThread.hpp"
2023-06-23 15:30:55 +00:00
namespace Star {
2023-06-24 12:49:47 +00:00
STAR_CLASS(Directives);
2023-06-24 09:41:52 +00:00
STAR_CLASS(DirectivesGroup);
2023-06-23 15:30:55 +00:00
STAR_EXCEPTION(DirectivesException, StarException);
// Kae: My attempt at reducing memory allocation and per-frame string parsing for extremely long directives
2023-06-24 12:49:47 +00:00
class Directives {
public:
struct Shared;
2023-06-24 09:41:52 +00:00
struct Entry {
mutable ImageOperation operation;
size_t begin;
size_t length;
2023-06-23 15:30:55 +00:00
ImageOperation const& loadOperation(Shared const& parent) const;
StringView string(Shared const& parent) const;
Entry(ImageOperation&& newOperation, size_t begin, size_t end);
Entry(ImageOperation const& newOperation, size_t begin, size_t end);
2023-06-24 12:49:47 +00:00
Entry(Entry const& other);
2023-06-24 09:41:52 +00:00
};
2023-06-23 15:30:55 +00:00
struct Shared {
List<Entry> entries;
String string;
size_t hash = 0;
mutable Mutex mutex;
bool empty() const;
Shared();
Shared(List<Entry>&& givenEntries, String&& givenString);
};
2023-06-24 09:41:52 +00:00
Directives();
Directives(String const& directives);
Directives(String&& directives);
2023-06-24 12:49:47 +00:00
Directives(const char* directives);
2024-01-03 08:17:19 +00:00
Directives(Directives const& directives);
Directives(Directives&& directives) noexcept;
2024-01-03 08:17:19 +00:00
~Directives();
2023-06-23 15:30:55 +00:00
Directives& operator=(String const& s);
Directives& operator=(String&& s);
Directives& operator=(const char* s);
Directives& operator=(Directives&& other) noexcept;
2024-01-03 08:17:19 +00:00
Directives& operator=(Directives const& other);
void loadOperations() const;
void parse(String&& directives);
String string() const;
String const* stringPtr() const;
String buildString() const;
String& addToString(String& out) const;
size_t hash() const;
size_t size() const;
2023-06-25 08:27:36 +00:00
bool empty() const;
operator bool() const;
2023-06-24 12:49:47 +00:00
Shared const& operator*() const;
Shared const* operator->() const;
bool equals(Directives const& other) const;
bool equals(String const& string) const;
bool operator==(Directives const& other) const;
bool operator==(String const& string) const;
2023-06-24 12:49:47 +00:00
friend DataStream& operator>>(DataStream& ds, Directives& directives);
friend DataStream& operator<<(DataStream& ds, Directives const& directives);
2023-06-23 15:30:55 +00:00
//friend bool operator==(Directives const& d1, String const& d2);
//friend bool operator==(Directives const& directives, String const& string);
//friend bool operator==(String const& string, Directives const& directives);
std::shared_ptr<Shared const> m_shared;
2023-06-24 09:41:52 +00:00
};
2023-06-23 15:30:55 +00:00
2023-06-24 09:41:52 +00:00
class DirectivesGroup {
public:
DirectivesGroup();
DirectivesGroup(String const& directives);
DirectivesGroup(String&& directives);
2023-06-23 15:30:55 +00:00
2023-06-25 08:27:36 +00:00
bool empty() const;
operator bool() const;
2023-06-24 09:41:52 +00:00
bool compare(DirectivesGroup const& other) const;
void append(Directives const& other);
2023-06-24 12:49:47 +00:00
void clear();
2023-06-24 09:41:52 +00:00
DirectivesGroup& operator+=(Directives const& other);
2023-06-23 15:30:55 +00:00
2023-06-25 08:27:36 +00:00
String toString() const;
2023-06-23 15:30:55 +00:00
void addToString(String& string) const;
typedef function<void(Directives::Entry const&, Directives const&)> DirectivesCallback;
typedef function<bool(Directives::Entry const&, Directives const&)> AbortableDirectivesCallback;
2023-06-23 15:30:55 +00:00
2023-06-24 09:41:52 +00:00
void forEach(DirectivesCallback callback) const;
bool forEachAbortable(AbortableDirectivesCallback callback) const;
2023-06-25 08:27:36 +00:00
Image applyNewImage(const Image& image) const;
2023-06-24 09:41:52 +00:00
void applyExistingImage(Image& image) const;
2023-06-25 08:27:36 +00:00
size_t hash() const;
2023-06-25 08:12:54 +00:00
const List<Directives>& list() const;
2023-06-24 10:10:53 +00:00
friend bool operator==(DirectivesGroup const& a, DirectivesGroup const& b);
friend bool operator!=(DirectivesGroup const& a, DirectivesGroup const& b);
2023-06-25 08:12:54 +00:00
friend DataStream& operator>>(DataStream& ds, DirectivesGroup& directives);
friend DataStream& operator<<(DataStream& ds, DirectivesGroup const& directives);
2023-06-23 15:30:55 +00:00
private:
2023-06-24 09:41:52 +00:00
void buildString(String& string, const DirectivesGroup& directives) const;
List<Directives> m_directives;
size_t m_count = 0;
2023-06-24 09:41:52 +00:00
};
2023-06-23 15:30:55 +00:00
2023-06-24 09:41:52 +00:00
template <>
struct hash<DirectivesGroup> {
size_t operator()(DirectivesGroup const& s) const;
2023-06-23 15:30:55 +00:00
};
2023-06-24 09:41:52 +00:00
typedef DirectivesGroup ImageDirectives;
2023-06-23 15:30:55 +00:00
inline Directives::Shared const& Directives::operator*() const {
return *m_shared;
}
inline Directives::Shared const* Directives::operator->() const {
if (!m_shared)
throw DirectivesException("Directives::operator-> nullptr");
return m_shared.get();
}
2023-06-23 15:30:55 +00:00
}