diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-25 18:12:54 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-25 18:12:54 +1000 |
commit | 7d205330dbf1c2fd44d9d58393ab46434ac8bb5e (patch) | |
tree | e73db06236120e2399f0b4863257477eec528295 /source/core | |
parent | e2424b7dcf60d18b277b092eb7f2a947fff27415 (diff) |
More directives optimization
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/StarColor.cpp | 54 | ||||
-rw-r--r-- | source/core/StarColor.hpp | 2 | ||||
-rw-r--r-- | source/core/StarDirectives.cpp | 19 | ||||
-rw-r--r-- | source/core/StarDirectives.hpp | 4 | ||||
-rw-r--r-- | source/core/StarException.hpp | 36 | ||||
-rw-r--r-- | source/core/StarException_unix.cpp | 14 | ||||
-rw-r--r-- | source/core/StarException_windows.cpp | 12 | ||||
-rw-r--r-- | source/core/StarImageProcessing.cpp | 4 |
8 files changed, 86 insertions, 59 deletions
diff --git a/source/core/StarColor.cpp b/source/core/StarColor.cpp index 0503f19..02b359a 100644 --- a/source/core/StarColor.cpp +++ b/source/core/StarColor.cpp @@ -221,7 +221,7 @@ Color::Color(const String& name) { if (i != NamedColors.end()) *this = i->second; else - throw ColorException(strf("Named color %s not found", name)); + throw ColorException(strf("Named color %s not found", name), false); } } @@ -303,30 +303,7 @@ uint32_t Color::toUint32() const { } Color Color::fromHex(String const& s) { - uint8_t cbytes[4]; - - if (s.utf8Size() == 3) { - nibbleDecode(s.utf8Ptr(), 3, (char*)cbytes, 4); - cbytes[0] = (cbytes[0] << 4) | cbytes[0]; - cbytes[1] = (cbytes[1] << 4) | cbytes[1]; - cbytes[2] = (cbytes[2] << 4) | cbytes[2]; - cbytes[3] = 255; - } else if (s.utf8Size() == 4) { - nibbleDecode(s.utf8Ptr(), 4, (char*)cbytes, 4); - cbytes[0] = (cbytes[0] << 4) | cbytes[0]; - cbytes[1] = (cbytes[1] << 4) | cbytes[1]; - cbytes[2] = (cbytes[2] << 4) | cbytes[2]; - cbytes[3] = (cbytes[3] << 4) | cbytes[3]; - } else if (s.utf8Size() == 6) { - hexDecode(s.utf8Ptr(), 6, (char*)cbytes, 4); - cbytes[3] = 255; - } else if (s.utf8Size() == 8) { - hexDecode(s.utf8Ptr(), 8, (char*)cbytes, 4); - } else { - throw ColorException(strf("Improper size for hex string '%s' in Color::fromHex", s)); - } - - return Color::rgba(cbytes[0], cbytes[1], cbytes[2], cbytes[3]); + return Color::rgba(hexToVec4B(s)); } Vec4B Color::toRgba() const { @@ -624,4 +601,31 @@ Vec4B Color::hueShiftVec4B(Vec4B color, float hue) { } } +Vec4B Color::hexToVec4B(String const& s) { + Array<uint8_t, 4> cbytes; + + if (s.utf8Size() == 3) { + nibbleDecode(s.utf8Ptr(), 3, (char*)cbytes.data(), 4); + cbytes[0] = (cbytes[0] << 4) | cbytes[0]; + cbytes[1] = (cbytes[1] << 4) | cbytes[1]; + cbytes[2] = (cbytes[2] << 4) | cbytes[2]; + cbytes[3] = 255; + } else if (s.utf8Size() == 4) { + nibbleDecode(s.utf8Ptr(), 4, (char*)cbytes.data(), 4); + cbytes[0] = (cbytes[0] << 4) | cbytes[0]; + cbytes[1] = (cbytes[1] << 4) | cbytes[1]; + cbytes[2] = (cbytes[2] << 4) | cbytes[2]; + cbytes[3] = (cbytes[3] << 4) | cbytes[3]; + } else if (s.utf8Size() == 6) { + hexDecode(s.utf8Ptr(), 6, (char*)cbytes.data(), 4); + cbytes[3] = 255; + } else if (s.utf8Size() == 8) { + hexDecode(s.utf8Ptr(), 8, (char*)cbytes.data(), 4); + } else { + throw ColorException(strf("Improper size for hex string '%s' in Color::hexToVec4B", s), false); + } + + return Vec4B(move(cbytes)); +} + } diff --git a/source/core/StarColor.hpp b/source/core/StarColor.hpp index e2fe2b5..68bcc10 100644 --- a/source/core/StarColor.hpp +++ b/source/core/StarColor.hpp @@ -67,7 +67,7 @@ public: static Color temperature(float temp); static Vec4B hueShiftVec4B(Vec4B color, float hue); - + static Vec4B Color::hexToVec4B(String const& s); // Black Color(); diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp index 1245eae..5dd82cf 100644 --- a/source/core/StarDirectives.cpp +++ b/source/core/StarDirectives.cpp @@ -229,6 +229,10 @@ inline size_t DirectivesGroup::hash() const { return hasher.digest(); } +const List<Directives>& DirectivesGroup::list() const { + return m_directives; +} + bool operator==(DirectivesGroup const& a, DirectivesGroup const& b) { return a.compare(b); } @@ -237,6 +241,21 @@ bool operator!=(DirectivesGroup const& a, DirectivesGroup const& b) { return !a.compare(b); } +DataStream& operator>>(DataStream& ds, DirectivesGroup& directivesGroup) { + String string; + ds.read(string); + + directivesGroup = move(DirectivesGroup(move(string))); + + return ds; +} + +DataStream& operator<<(DataStream& ds, DirectivesGroup const& directivesGroup) { + ds.write(directivesGroup.toString()); + + return ds; +} + size_t hash<DirectivesGroup>::operator()(DirectivesGroup const& s) const { return s.hash(); } diff --git a/source/core/StarDirectives.hpp b/source/core/StarDirectives.hpp index 440b33a..dafa2a0 100644 --- a/source/core/StarDirectives.hpp +++ b/source/core/StarDirectives.hpp @@ -73,9 +73,13 @@ public: void applyExistingImage(Image& image) const; inline size_t hash() const; + const List<Directives>& list() const; friend bool operator==(DirectivesGroup const& a, DirectivesGroup const& b); friend bool operator!=(DirectivesGroup const& a, DirectivesGroup const& b); + + friend DataStream& operator>>(DataStream& ds, DirectivesGroup& directives); + friend DataStream& operator<<(DataStream& ds, DirectivesGroup const& directives); private: void buildString(String& string, const DirectivesGroup& directives) const; diff --git a/source/core/StarException.hpp b/source/core/StarException.hpp index b427511..7e6fe3e 100644 --- a/source/core/StarException.hpp +++ b/source/core/StarException.hpp @@ -13,7 +13,7 @@ public: StarException() noexcept; virtual ~StarException() noexcept; - explicit StarException(std::string message) noexcept; + explicit StarException(std::string message, bool genStackTrace = true) noexcept; explicit StarException(std::exception const& cause) noexcept; StarException(std::string message, std::exception const& cause) noexcept; @@ -26,7 +26,7 @@ public: friend OutputProxy outputException(std::exception const& e, bool fullStacktrace); protected: - StarException(char const* type, std::string message) noexcept; + StarException(char const* type, std::string message, bool genStackTrace = true) noexcept; StarException(char const* type, std::string message, std::exception const& cause) noexcept; private: @@ -68,22 +68,22 @@ void fatalException(std::exception const& e, bool showStackTrace); {} #endif -#define STAR_EXCEPTION(ClassName, BaseName) \ - class ClassName : public BaseName { \ - public: \ - template <typename... Args> \ - static ClassName format(char const* fmt, Args const&... args) { \ - return ClassName(strf(fmt, args...)); \ - } \ - ClassName() : BaseName(#ClassName, std::string()) {} \ - explicit ClassName(std::string message) : BaseName(#ClassName, move(message)) {} \ - explicit ClassName(std::exception const& cause) : BaseName(#ClassName, std::string(), cause) {} \ - ClassName(std::string message, std::exception const& cause) : BaseName(#ClassName, move(message), cause) {} \ - \ - protected: \ - ClassName(char const* type, std::string message) : BaseName(type, move(message)) {} \ - ClassName(char const* type, std::string message, std::exception const& cause) \ - : BaseName(type, move(message), cause) {} \ +#define STAR_EXCEPTION(ClassName, BaseName) \ + class ClassName : public BaseName { \ + public: \ + template <typename... Args> \ + static ClassName format(char const* fmt, Args const&... args) { \ + return ClassName(strf(fmt, args...)); \ + } \ + ClassName() : BaseName(#ClassName, std::string()) {} \ + explicit ClassName(std::string message, bool genStackTrace = true) : BaseName(#ClassName, move(message), genStackTrace) {} \ + explicit ClassName(std::exception const& cause) : BaseName(#ClassName, std::string(), cause) {} \ + ClassName(std::string message, std::exception const& cause) : BaseName(#ClassName, move(message), cause) {} \ + \ + protected: \ + ClassName(char const* type, std::string message, bool genStackTrace = true) : BaseName(type, move(message), genStackTrace) {} \ + ClassName(char const* type, std::string message, std::exception const& cause) \ + : BaseName(type, move(message), cause) {} \ } STAR_EXCEPTION(OutOfRangeException, StarException); diff --git a/source/core/StarException_unix.cpp b/source/core/StarException_unix.cpp index db23c7c..578c0b4 100644 --- a/source/core/StarException_unix.cpp +++ b/source/core/StarException_unix.cpp @@ -38,8 +38,8 @@ StarException::StarException() noexcept StarException::~StarException() noexcept {} -StarException::StarException(std::string message) noexcept - : StarException("StarException", move(message)) {} +StarException::StarException(std::string message, bool genStackTrace) noexcept + : StarException("StarException", move(message), genStackTrace) {} StarException::StarException(std::exception const& cause) noexcept : StarException("StarException", std::string(), cause) {} @@ -56,19 +56,19 @@ const char* StarException::what() const throw() { return m_whatBuffer.c_str(); } -StarException::StarException(char const* type, std::string message) noexcept { - auto printException = [](std::ostream& os, bool fullStacktrace, char const* type, std::string message, StackCapture stack) { +StarException::StarException(char const* type, std::string message, bool genStackTrace) noexcept { + auto printException = [](std::ostream& os, bool fullStacktrace, char const* type, std::string message, Maybe<StackCapture> stack) { os << "(" << type << ")"; if (!message.empty()) os << " " << message; - if (fullStacktrace) { + if (fullStacktrace && stack) { os << std::endl; - os << outputStack(stack); + os << outputStack(*stack); } }; - m_printException = bind(printException, _1, _2, type, move(message), captureStack()); + m_printException = bind(printException, _1, _2, type, move(message), genStackTrace ? captureStack() : Maybe<StackCapture>()); } StarException::StarException(char const* type, std::string message, std::exception const& cause) noexcept diff --git a/source/core/StarException_windows.cpp b/source/core/StarException_windows.cpp index 8892eb2..ca81332 100644 --- a/source/core/StarException_windows.cpp +++ b/source/core/StarException_windows.cpp @@ -145,7 +145,7 @@ StarException::StarException() noexcept : StarException(std::string("StarExcepti StarException::~StarException() noexcept {} -StarException::StarException(std::string message) noexcept : StarException("StarException", move(message)) {} +StarException::StarException(std::string message, bool genStackTrace) noexcept : StarException("StarException", move(message), genStackTrace) {} StarException::StarException(std::exception const& cause) noexcept : StarException("StarException", std::string(), cause) {} @@ -162,20 +162,20 @@ const char* StarException::what() const throw() { return m_whatBuffer.c_str(); } -StarException::StarException(char const* type, std::string message) noexcept { +StarException::StarException(char const* type, std::string message, bool genStackTrace) noexcept { auto printException = []( - std::ostream& os, bool fullStacktrace, char const* type, std::string message, StackCapture stack) { + std::ostream& os, bool fullStacktrace, char const* type, std::string message, Maybe<StackCapture> stack) { os << "(" << type << ")"; if (!message.empty()) os << " " << message; - if (fullStacktrace) { + if (fullStacktrace && stack) { os << std::endl; - os << outputStack(stack); + os << outputStack(*stack); } }; - m_printException = bind(printException, _1, _2, type, move(message), captureStack()); + m_printException = bind(printException, _1, _2, type, move(message), genStackTrace ? captureStack() : Maybe<StackCapture>()); } StarException::StarException(char const* type, std::string message, std::exception const& cause) noexcept diff --git a/source/core/StarImageProcessing.cpp b/source/core/StarImageProcessing.cpp index c2f980e..56ac130 100644 --- a/source/core/StarImageProcessing.cpp +++ b/source/core/StarImageProcessing.cpp @@ -173,7 +173,7 @@ ImageOperation imageOperationFromString(String const& string) { } else if (type == "replace") { ColorReplaceImageOperation operation; for (size_t i = 0; i < (bits.size() - 1) / 2; ++i) - operation.colorReplaceMap[Color::fromHex(bits[i * 2 + 1]).toRgba()] = Color::fromHex(bits[i * 2 + 2]).toRgba(); + operation.colorReplaceMap[Color::hexToVec4B(bits[i * 2 + 1])] = Color::hexToVec4B(bits[i * 2 + 2]); return operation; @@ -259,7 +259,7 @@ ImageOperation imageOperationFromString(String const& string) { return FlipImageOperation{FlipImageOperation::FlipXY}; } else { - throw ImageOperationException(strf("Could not recognize ImageOperation type %s", type)); + throw ImageOperationException(strf("Could not recognize ImageOperation type %s", type), false); } } catch (OutOfRangeException const& e) { throw ImageOperationException("Error reading ImageOperation", e); |