Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core
diff options
context:
space:
mode:
Diffstat (limited to 'source/core')
-rw-r--r--source/core/StarAssetPath.cpp22
-rw-r--r--source/core/StarAssetPath.hpp5
-rw-r--r--source/core/StarDirectives.cpp69
-rw-r--r--source/core/StarDirectives.hpp8
-rw-r--r--source/core/StarLexicalCast.hpp2
5 files changed, 79 insertions, 27 deletions
diff --git a/source/core/StarAssetPath.cpp b/source/core/StarAssetPath.cpp
index b49f24d..82eca93 100644
--- a/source/core/StarAssetPath.cpp
+++ b/source/core/StarAssetPath.cpp
@@ -147,8 +147,13 @@ bool AssetPath::operator==(AssetPath const& rhs) const {
return tie(basePath, subPath, directives) == tie(rhs.basePath, rhs.subPath, rhs.directives);
}
+AssetPath::AssetPath(const char* path) {
+ *this = move(AssetPath::split(path));
+}
+
+
AssetPath::AssetPath(String const& path) {
- *this = move(AssetPath::split(path)); // split code should probably be in here, but whatever
+ *this = move(AssetPath::split(path));
}
AssetPath::AssetPath(String&& basePath, Maybe<String>&& subPath, DirectivesGroup&& directives) {
@@ -182,4 +187,19 @@ size_t hash<AssetPath>::operator()(AssetPath const& s) const {
return hashOf(s.basePath, s.subPath, s.directives);
}
+DataStream& operator>>(DataStream& ds, AssetPath& path) {
+ String string;
+ ds.read(string);
+
+ path = move(string);
+
+ return ds;
+}
+
+DataStream& operator<<(DataStream& ds, AssetPath const& path) {
+ ds.write(AssetPath::join(path));
+
+ return ds;
+}
+
}
diff --git a/source/core/StarAssetPath.hpp b/source/core/StarAssetPath.hpp
index fd77005..93fa8eb 100644
--- a/source/core/StarAssetPath.hpp
+++ b/source/core/StarAssetPath.hpp
@@ -3,6 +3,7 @@
#include "StarDirectives.hpp"
#include "StarHash.hpp"
+#include "StarDataStream.hpp"
namespace Star {
@@ -53,6 +54,7 @@ struct AssetPath {
static String relativeTo(String const& sourcePath, String const& givenPath);
AssetPath() = default;
+ AssetPath(const char* path);
AssetPath(String const& path);
AssetPath(String&& basePath, Maybe<String>&& subPath, DirectivesGroup&& directives);
AssetPath(const String& basePath, const Maybe<String>& subPath, const DirectivesGroup& directives);
@@ -63,6 +65,9 @@ struct AssetPath {
bool operator==(AssetPath const& rhs) const;
};
+DataStream& operator>>(DataStream& ds, AssetPath& path);
+DataStream& operator<<(DataStream& ds, AssetPath const& path);
+
std::ostream& operator<<(std::ostream& os, AssetPath const& rhs);
template <>
diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp
index 4a2ca4c..9214abf 100644
--- a/source/core/StarDirectives.cpp
+++ b/source/core/StarDirectives.cpp
@@ -23,23 +23,24 @@ Directives::Entry::Entry(Entry const& other) {
Directives::Directives() : hash(0) {}
Directives::Directives(String const& directives) : hash(0) {
- parse(directives);
+ string = directives;
+ parse(string);
}
Directives::Directives(String&& directives) : hash(0) {
- String mine = move(directives);
- parse(mine);
+ string = move(directives);
+ parse(string);
}
-Directives::Directives(const char* directives) : hash(0) {
- String string(directives);
+Directives::Directives(const char* directives) : hash(0), 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());
+ String newString;
+ string = move(buildString(newString));
+ hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size());
}
void Directives::parse(String const& directives) {
@@ -51,8 +52,12 @@ void Directives::parse(String const& directives) {
newList.reserve(split.size());
for (String& str : split) {
if (!str.empty()) {
- ImageOperation operation = imageOperationFromString(str);
- newList.emplace_back(move(operation), move(str));
+ try {
+ ImageOperation operation = imageOperationFromString(str);
+ newList.emplace_back(move(operation), move(str));
+ } catch (StarException const& e) {
+ Logger::logf(LogLevel::Error, "Error parsing image operation: %s", e.what());
+ }
}
}
@@ -65,27 +70,29 @@ void Directives::parse(String const& directives) {
// Logger::logf(LogLevel::Debug, "Directives: Parsed %u character long string", directives.utf8Size());
}
-void Directives::buildString(String& out) const {
+String& Directives::buildString(String& out) const {
if (entries) {
for (auto& entry : *entries) {
out += "?";
out += entry.string;
}
}
+
+ return out;
}
String Directives::toString() const {
- String result;
- buildString(result);
- //if (result.utf8Size() > 1000)
- // Logger::logf(LogLevel::Debug, "Directives: Rebuilt %u character long string", result.utf8Size());
- return result;
+ return string;
}
inline bool Directives::empty() const {
return !entries || entries->empty();
}
+inline Directives::operator bool() const {
+ return !empty();
+}
+
DataStream& operator>>(DataStream& ds, Directives& directives) {
String string;
@@ -103,19 +110,37 @@ DataStream& operator<<(DataStream& ds, Directives const& directives) {
}
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 const& directives) : m_count(0) {
+ if (directives.empty())
+ return;
+
+ Directives parsed(directives);
+ if (parsed) {
+ m_directives.emplace_back(move(parsed));
+ m_count = m_directives.back().entries->size();
+ }
}
-DirectivesGroup::DirectivesGroup(String&& directives) {
- m_directives.emplace_back(move(directives));
- m_count = m_directives.back().entries->size();
+DirectivesGroup::DirectivesGroup(String&& directives) : m_count(0) {
+ if (directives.empty()) {
+ directives.clear();
+ return;
+ }
+
+ Directives parsed(move(directives));
+ if (parsed) {
+ m_directives.emplace_back(move(parsed));
+ m_count = m_directives.back().entries->size();
+ }
}
inline bool DirectivesGroup::empty() const {
return m_count == 0;
}
+inline DirectivesGroup::operator bool() const {
+ return empty();
+}
+
inline bool DirectivesGroup::compare(DirectivesGroup const& other) const {
if (m_count != other.m_count)
return false;
@@ -156,7 +181,7 @@ inline String DirectivesGroup::toString() const {
void DirectivesGroup::addToString(String& string) const {
for (auto& directives : m_directives)
- directives.buildString(string);
+ string += directives.string;
}
void DirectivesGroup::forEach(DirectivesCallback callback) const {
diff --git a/source/core/StarDirectives.hpp b/source/core/StarDirectives.hpp
index ca4364c..440b33a 100644
--- a/source/core/StarDirectives.hpp
+++ b/source/core/StarDirectives.hpp
@@ -12,12 +12,11 @@ STAR_CLASS(DirectivesGroup);
STAR_EXCEPTION(DirectivesException, StarException);
// Kae: My attempt at reducing memory allocation and per-frame string parsing for extremely long directives
-// entries must never be a null ptr!
class Directives {
public:
struct Entry {
ImageOperation operation;
- String string;
+ String string; // One day, we can make this a string_view pointing to Entry::string.
Entry(ImageOperation&& newOperation, String&& newString);
Entry(ImageOperation const& newOperation, String const& newString);
@@ -31,15 +30,17 @@ public:
Directives(List<Entry>&& entries);
void parse(String const& directives);
- void buildString(String& out) const;
+ String& buildString(String& out) const;
String toString() const;
inline bool empty() const;
+ inline operator bool() const;
friend DataStream& operator>>(DataStream& ds, Directives& directives);
friend DataStream& operator<<(DataStream& ds, Directives const& directives);
std::shared_ptr<List<Entry> const> entries;
size_t hash = 0;
+ String string;
};
class DirectivesGroup {
@@ -51,6 +52,7 @@ public:
void parseDirectivesIntoLeaf(String const& directives);
inline bool empty() const;
+ inline operator bool() const;
bool compare(DirectivesGroup const& other) const;
void append(Directives const& other);
void append(List<Directives::Entry>&& entries);
diff --git a/source/core/StarLexicalCast.hpp b/source/core/StarLexicalCast.hpp
index bee2692..6e6c66e 100644
--- a/source/core/StarLexicalCast.hpp
+++ b/source/core/StarLexicalCast.hpp
@@ -47,7 +47,7 @@ Type lexicalCast(std::string const& s, std::ios_base::fmtflags flags = std::ios_
if (m)
return m.take();
else
- throw BadLexicalCast();
+ throw BadLexicalCast(strf("Lexical cast failed on '%s'", s));
}
template <typename Type>