From 78e87e9f09c2e82ae9d1b70e45db7b84d48af693 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Tue, 6 Aug 2024 09:43:02 +0100 Subject: [PATCH] clx_sprite: Minor improvements from #7191 --- Source/engine/clx_sprite.hpp | 92 +++++++++++++++-------------- Source/utils/intrusive_optional.hpp | 12 +++- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Source/engine/clx_sprite.hpp b/Source/engine/clx_sprite.hpp index b41e8174c68..e8957ed4eca 100644 --- a/Source/engine/clx_sprite.hpp +++ b/Source/engine/clx_sprite.hpp @@ -89,14 +89,10 @@ class ClxSprite { private: // For OptionalClxSprite. - constexpr ClxSprite() - : data_(nullptr) - , pixel_data_size_(0) - { - } + constexpr ClxSprite() = default; - const uint8_t *data_; - uint32_t pixel_data_size_; + const uint8_t *data_ = nullptr; + uint32_t pixel_data_size_ = 0; friend class OptionalClxSprite; }; @@ -139,7 +135,7 @@ class ClxSpriteList { } /** @brief The offset to the next sprite sheet, or file size if this is the last sprite sheet. */ - [[nodiscard]] constexpr uint32_t nextSpriteSheetOffsetOrFileSize() const + [[nodiscard]] constexpr uint32_t dataSize() const { return LoadLE32(&data_[4 + numSprites() * 4]); } @@ -154,12 +150,9 @@ class ClxSpriteList { private: // For OptionalClxSpriteList. - constexpr ClxSpriteList() - : data_(nullptr) - { - } + constexpr ClxSpriteList() = default; - const uint8_t *data_; + const uint8_t *data_ = nullptr; friend class OptionalClxSpriteList; }; @@ -264,16 +257,17 @@ class ClxSpriteSheet { [[nodiscard]] constexpr ClxSpriteSheetIterator begin() const; [[nodiscard]] constexpr ClxSpriteSheetIterator end() const; -private: - // For OptionalClxSpriteSheet. - constexpr ClxSpriteSheet() - : data_(nullptr) - , num_lists_(0) + [[nodiscard]] size_t dataSize() const { + return static_cast(&data_[sheetOffset(num_lists_ - 1)] + (*this)[num_lists_ - 1].dataSize() - &data_[0]); } - const uint8_t *data_; - uint16_t num_lists_; +private: + // For OptionalClxSpriteSheet. + constexpr ClxSpriteSheet() = default; + + const uint8_t *data_ = nullptr; + uint16_t num_lists_ = 0; friend class OptionalClxSpriteSheet; }; @@ -367,6 +361,11 @@ class OwnedClxSpriteList { return ClxSpriteList { *this }.numSprites(); } + [[nodiscard]] size_t dataSize() const + { + return ClxSpriteList { *this }.dataSize(); + } + private: // For OptionalOwnedClxSpriteList. OwnedClxSpriteList() = default; @@ -385,9 +384,9 @@ inline ClxSpriteList::ClxSpriteList(const OwnedClxSpriteList &owned) inline OwnedClxSpriteList ClxSpriteList::clone() const { - const size_t dataSize = nextSpriteSheetOffsetOrFileSize(); - std::unique_ptr data { new uint8_t[dataSize] }; - memcpy(data.get(), data_, dataSize); + const size_t size = dataSize(); + std::unique_ptr data { new uint8_t[size] }; + memcpy(data.get(), data_, size); return OwnedClxSpriteList { std::move(data) }; } @@ -422,16 +421,17 @@ class OwnedClxSpriteSheet { return ClxSpriteSheet { *this }.end(); } -private: - // For OptionalOwnedClxSpriteList. - OwnedClxSpriteSheet() - : data_(nullptr) - , num_lists_(0) + [[nodiscard]] size_t dataSize() const { + return ClxSpriteSheet { *this }.dataSize(); } +private: + // For OptionalOwnedClxSpriteList. + OwnedClxSpriteSheet() = default; + std::unique_ptr data_; - uint16_t num_lists_; + uint16_t num_lists_ = 0; friend class ClxSpriteSheet; // for implicit conversion. friend class OptionalOwnedClxSpriteSheet; @@ -489,17 +489,18 @@ class ClxSpriteListOrSheet { return num_lists_ != 0; } -private: - const uint8_t *data_; - uint16_t num_lists_; - - // For OptionalClxSpriteListOrSheet. - constexpr ClxSpriteListOrSheet() - : data_(nullptr) - , num_lists_(0) + [[nodiscard]] size_t dataSize() const { + return isSheet() ? sheet().dataSize() : list().dataSize(); } +private: + // For OptionalClxSpriteListOrSheet. + constexpr ClxSpriteListOrSheet() = default; + + const uint8_t *data_ = nullptr; + uint16_t num_lists_ = 0; + friend class OptionalClxSpriteListOrSheet; }; @@ -563,17 +564,20 @@ class OwnedClxSpriteListOrSheet { return num_lists_ != 0; } -private: - std::unique_ptr data_; - uint16_t num_lists_; + [[nodiscard]] uint16_t numLists() const { return num_lists_; } - // For OptionalOwnedClxSpriteListOrSheet. - OwnedClxSpriteListOrSheet() - : data_(nullptr) - , num_lists_(0) + [[nodiscard]] size_t dataSize() const { + return ClxSpriteListOrSheet { *this }.dataSize(); } +private: + // For OptionalOwnedClxSpriteListOrSheet. + OwnedClxSpriteListOrSheet() = default; + + std::unique_ptr data_; + uint16_t num_lists_ = 0; + friend class ClxSpriteListOrSheet; friend class OptionalOwnedClxSpriteListOrSheet; }; diff --git a/Source/utils/intrusive_optional.hpp b/Source/utils/intrusive_optional.hpp index 40523b473a5..8f66f4a8084 100644 --- a/Source/utils/intrusive_optional.hpp +++ b/Source/utils/intrusive_optional.hpp @@ -30,7 +30,10 @@ public: } \ \ template \ - CONSTEXPR OPTIONAL_CLASS &operator=(VALUE_CLASS &&value) \ + CONSTEXPR std::enable_if_t< \ + !std::is_same_v>>, \ + OPTIONAL_CLASS> & \ + operator=(U &&value) noexcept \ { \ value_ = std::forward(value); \ return *this; \ @@ -66,11 +69,16 @@ public: return &value_; \ } \ \ - CONSTEXPR operator bool() const \ + [[nodiscard]] CONSTEXPR bool has_value() const \ { \ return value_.FIELD != NULL_VALUE; \ } \ \ + CONSTEXPR operator bool() const \ + { \ + return has_value(); \ + } \ + \ private: \ VALUE_CLASS value_;