region: introduce foreach and cleanup clangd warnings (#65)

* CRegion: introduce forEachRect

makes us able to directly call a function on each rect instead of using
getRects() allocating a vector every single time, especially when its
used in hot paths like the renderer.

* CRegion: cleanup CRegion of clangd warnings

overload pixman() with const, remove const_cast, mark move as noexcept.
ensure self assignment doesnt occur.
This commit is contained in:
Tom Englund 2025-07-10 11:12:26 +02:00 committed by GitHub
parent a8229739cf
commit e21b18ff8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 11 deletions

View file

@ -21,17 +21,21 @@ namespace Hyprutils {
CRegion(pixman_box32_t* box);
CRegion(const CRegion&);
CRegion(CRegion&&);
CRegion(CRegion&&) noexcept;
~CRegion();
CRegion& operator=(CRegion&& other) {
pixman_region32_copy(&m_rRegion, other.pixman());
CRegion& operator=(CRegion&& other) noexcept {
if (this != &other)
pixman_region32_copy(&m_rRegion, other.pixman());
return *this;
}
CRegion& operator=(CRegion& other) {
pixman_region32_copy(&m_rRegion, other.pixman());
CRegion& operator=(const CRegion& other) {
if (this != &other)
pixman_region32_copy(&m_rRegion, other.pixman());
return *this;
}
@ -58,12 +62,24 @@ namespace Hyprutils {
CRegion copy() const;
std::vector<pixman_box32_t> getRects() const;
template <typename T>
void forEachRect(T&& cb) const {
int rectsNum = 0;
const auto* rects = pixman_region32_rectangles(&m_rRegion, &rectsNum);
for (int i = 0; i < rectsNum; ++i) {
std::forward<T>(cb)(rects[i]);
}
}
//
pixman_region32_t* pixman() {
return &m_rRegion;
}
const pixman_region32_t* pixman() const {
return &m_rRegion;
}
private:
pixman_region32_t m_rRegion;
};

View file

@ -28,10 +28,10 @@ Hyprutils::Math::CRegion::CRegion(pixman_box32_t* box) {
Hyprutils::Math::CRegion::CRegion(const CRegion& other) {
pixman_region32_init(&m_rRegion);
pixman_region32_copy(&m_rRegion, const_cast<CRegion*>(&other)->pixman());
pixman_region32_copy(&m_rRegion, other.pixman());
}
Hyprutils::Math::CRegion::CRegion(CRegion&& other) {
Hyprutils::Math::CRegion::CRegion(CRegion&& other) noexcept {
pixman_region32_init(&m_rRegion);
pixman_region32_copy(&m_rRegion, other.pixman());
}
@ -46,12 +46,12 @@ CRegion& Hyprutils::Math::CRegion::clear() {
}
CRegion& Hyprutils::Math::CRegion::set(const CRegion& other) {
pixman_region32_copy(&m_rRegion, const_cast<CRegion*>(&other)->pixman());
pixman_region32_copy(&m_rRegion, other.pixman());
return *this;
}
CRegion& Hyprutils::Math::CRegion::add(const CRegion& other) {
pixman_region32_union(&m_rRegion, &m_rRegion, const_cast<CRegion*>(&other)->pixman());
pixman_region32_union(&m_rRegion, &m_rRegion, other.pixman());
return *this;
}
@ -66,12 +66,12 @@ CRegion& Hyprutils::Math::CRegion::add(const CBox& other) {
}
CRegion& Hyprutils::Math::CRegion::subtract(const CRegion& other) {
pixman_region32_subtract(&m_rRegion, &m_rRegion, const_cast<CRegion*>(&other)->pixman());
pixman_region32_subtract(&m_rRegion, &m_rRegion, other.pixman());
return *this;
}
CRegion& Hyprutils::Math::CRegion::intersect(const CRegion& other) {
pixman_region32_intersect(&m_rRegion, &m_rRegion, const_cast<CRegion*>(&other)->pixman());
pixman_region32_intersect(&m_rRegion, &m_rRegion, other.pixman());
return *this;
}