diff --git a/include/hyprutils/math/Edges.hpp b/include/hyprutils/math/Edges.hpp index a1eaef9..304a3d6 100644 --- a/include/hyprutils/math/Edges.hpp +++ b/include/hyprutils/math/Edges.hpp @@ -1,4 +1,6 @@ #pragma once +#include "hyprutils/memory/Casts.hpp" + #include namespace Hyprutils::Math { @@ -18,7 +20,7 @@ namespace Hyprutils::Math { CEdges() = default; CEdges(eEdges edges) : m_edges(edges) {} - CEdges(uint8_t edges) : m_edges(static_cast(edges)) {} + CEdges(uint8_t edges) : m_edges(sc(edges)) {} bool operator==(const CEdges& other) { return m_edges == other.m_edges; @@ -80,28 +82,28 @@ namespace Hyprutils::Math { * @param top The state the top edge should be set to. */ void setTop(bool top) { - m_edges = static_cast((m_edges & ~TOP) | (TOP * top)); + m_edges = sc((m_edges & ~TOP) | (TOP * top)); } /** * @param left The state the left edge should be set to. */ void setLeft(bool left) { - m_edges = static_cast((m_edges & ~LEFT) | (LEFT * left)); + m_edges = sc((m_edges & ~LEFT) | (LEFT * left)); } /** * @param bottom The state the bottom edge should be set to. */ void setBottom(bool bottom) { - m_edges = static_cast((m_edges & ~BOTTOM) | (BOTTOM * bottom)); + m_edges = sc((m_edges & ~BOTTOM) | (BOTTOM * bottom)); } /** * @param right The state the right edge should be set to. */ void setRight(bool right) { - m_edges = static_cast((m_edges & ~RIGHT) | (RIGHT * right)); + m_edges = sc((m_edges & ~RIGHT) | (RIGHT * right)); } eEdges m_edges = NONE; diff --git a/include/hyprutils/memory/Atomic.hpp b/include/hyprutils/memory/Atomic.hpp index 8d6da7d..5388c2b 100644 --- a/include/hyprutils/memory/Atomic.hpp +++ b/include/hyprutils/memory/Atomic.hpp @@ -1,4 +1,5 @@ #pragma once +#pragma once #include "./ImplBase.hpp" #include "./SharedPtr.hpp" @@ -143,7 +144,7 @@ namespace Hyprutils::Memory { // -> must unlock BEFORE reset // not last ref? // -> must unlock AFTER reset - auto& mutex = ((Atomic_::impl*)m_ptr.impl_)->getMutex(); + auto& mutex = sc*>(m_ptr.impl_)->getMutex(); mutex.lock(); if (m_ptr.impl_->ref() > 1) { @@ -208,7 +209,7 @@ namespace Hyprutils::Memory { private: std::lock_guard implLockGuard() const { - return ((Atomic_::impl*)m_ptr.impl_)->lockGuard(); + return sc*>(m_ptr.impl_)->lockGuard(); } CSharedPointer m_ptr; @@ -316,7 +317,7 @@ namespace Hyprutils::Memory { // -> must unlock BEFORE reset // not last ref? // -> must unlock AFTER reset - auto& mutex = ((Atomic_::impl*)m_ptr.impl_)->getMutex(); + auto& mutex = sc*>(m_ptr.impl_)->getMutex(); mutex.lock(); if (m_ptr.impl_->ref() == 0 && m_ptr.impl_->wref() == 1) { mutex.unlock(); @@ -379,7 +380,7 @@ namespace Hyprutils::Memory { private: std::lock_guard implLockGuard() const { - return ((Atomic_::impl*)m_ptr.impl_)->lockGuard(); + return sc*>(m_ptr.impl_)->lockGuard(); } CWeakPointer m_ptr; diff --git a/include/hyprutils/memory/Casts.hpp b/include/hyprutils/memory/Casts.hpp new file mode 100644 index 0000000..04078b7 --- /dev/null +++ b/include/hyprutils/memory/Casts.hpp @@ -0,0 +1,28 @@ +#pragma once +#include +#include + +template +constexpr To sc(From&& from) noexcept { + return static_cast(std::forward(from)); +} + +template +constexpr To cc(From&& from) noexcept { + return const_cast(std::forward(from)); +} + +template +constexpr To rc(From&& from) noexcept { + return reinterpret_cast(std::forward(from)); +} + +template +constexpr To dc(From&& from) { + return dynamic_cast(std::forward(from)); +} + +template +constexpr To bc(const From& from) noexcept { + return std::bit_cast(from); +} \ No newline at end of file diff --git a/include/hyprutils/memory/SharedPtr.hpp b/include/hyprutils/memory/SharedPtr.hpp index b86dfb2..a523def 100644 --- a/include/hyprutils/memory/SharedPtr.hpp +++ b/include/hyprutils/memory/SharedPtr.hpp @@ -2,6 +2,7 @@ #include #include "ImplBase.hpp" +#include "Casts.hpp" /* This is a custom impl of std::shared_ptr. @@ -114,11 +115,11 @@ namespace Hyprutils { } bool operator()(const CSharedPointer& lhs, const CSharedPointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + return rc(lhs.impl_) < rc(rhs.impl_); } bool operator<(const CSharedPointer& rhs) const { - return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); + return rc(impl_) < rc(rhs.impl_); } T* operator->() const { @@ -135,7 +136,7 @@ namespace Hyprutils { } T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; + return impl_ ? sc(impl_->getData()) : nullptr; } unsigned int strongRef() const { diff --git a/include/hyprutils/memory/UniquePtr.hpp b/include/hyprutils/memory/UniquePtr.hpp index 8588560..676b945 100644 --- a/include/hyprutils/memory/UniquePtr.hpp +++ b/include/hyprutils/memory/UniquePtr.hpp @@ -1,6 +1,7 @@ #pragma once #include "ImplBase.hpp" +#include "Casts.hpp" /* This is a custom impl of std::unique_ptr. @@ -74,7 +75,7 @@ namespace Hyprutils { } bool operator()(const CUniquePointer& lhs, const CUniquePointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + return rc(lhs.impl_) < rc(rhs.impl_); } T* operator->() const { @@ -91,7 +92,7 @@ namespace Hyprutils { } T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; + return impl_ ? sc(impl_->getData()) : nullptr; } Impl_::impl_base* impl_ = nullptr; diff --git a/include/hyprutils/memory/WeakPtr.hpp b/include/hyprutils/memory/WeakPtr.hpp index 023bf78..8b734d0 100644 --- a/include/hyprutils/memory/WeakPtr.hpp +++ b/include/hyprutils/memory/WeakPtr.hpp @@ -2,6 +2,7 @@ #include "./SharedPtr.hpp" #include "./UniquePtr.hpp" +#include "./Casts.hpp" /* This is a Hyprland implementation of std::weak_ptr. @@ -91,7 +92,7 @@ namespace Hyprutils { /* create a weak ptr from a shared ptr with assignment */ template validHierarchy&> operator=(const CSharedPointer& rhs) { - if (reinterpret_cast(impl_) == reinterpret_cast(rhs.impl_)) + if (rc(impl_) == rc(rhs.impl_)) return *this; decrementWeak(); @@ -162,15 +163,15 @@ namespace Hyprutils { } bool operator()(const CWeakPointer& lhs, const CWeakPointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + return rc(lhs.impl_) < rc(rhs.impl_); } bool operator<(const CWeakPointer& rhs) const { - return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); + return rc(impl_) < rc(rhs.impl_); } T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; + return impl_ ? sc(impl_->getData()) : nullptr; } T* operator->() const { diff --git a/include/hyprutils/signal/Signal.hpp b/include/hyprutils/signal/Signal.hpp index 37a12ba..9e7885e 100644 --- a/include/hyprutils/signal/Signal.hpp +++ b/include/hyprutils/signal/Signal.hpp @@ -37,7 +37,7 @@ namespace Hyprutils { if constexpr (sizeof...(Args) == 1) // NOLINTNEXTLINE: const is reapplied by handler invocation if required - emitInternal(const_cast(static_cast(&std::get<0>(argsTuple)))); + emitInternal(cc(sc(&std::get<0>(argsTuple)))); else emitInternal(&argsTuple); } @@ -94,9 +94,9 @@ namespace Hyprutils { if constexpr (sizeof...(Args) == 0) handler(); else if constexpr (sizeof...(Args) == 1) - handler(*static_cast...>>>*>(args)); + handler(*sc...>>>*>(args)); else - std::apply(handler, *static_cast...>*>(args)); + std::apply(handler, *sc...>*>(args)); }; } }; diff --git a/src/animation/BezierCurve.cpp b/src/animation/BezierCurve.cpp index 5b05d2f..ca8f21a 100644 --- a/src/animation/BezierCurve.cpp +++ b/src/animation/BezierCurve.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -21,7 +22,7 @@ void CBezierCurve::setup(const std::array& pVec) { // bake BAKEDPOINTS points for faster lookups // T -> X ( / BAKEDPOINTS ) for (int i = 0; i < BAKEDPOINTS; ++i) { - float const t = (i + 1) / (float)BAKEDPOINTS; + float const t = (i + 1) / sc(BAKEDPOINTS); m_aPointsBaked[i] = Vector2D(getXForT(t), getYForT(t)); } diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index a889f3b..e592683 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -98,7 +99,7 @@ Mat3x3& Mat3x3::rotate(float rot) { } Mat3x3& Mat3x3::scale(const Vector2D& scale_) { - multiply(std::array{(float)scale_.x, 0.0f, 0.0f, 0.0f, (float)scale_.y, 0.0f, 0.0f, 0.0f, 1.0f}); + multiply(std::array{sc(scale_.x), 0.0f, 0.0f, 0.0f, sc(scale_.y), 0.0f, 0.0f, 0.0f, 1.0f}); return *this; } @@ -107,7 +108,7 @@ Mat3x3& Mat3x3::scale(const float scale_) { } Mat3x3& Mat3x3::translate(const Vector2D& offset) { - multiply(std::array{1.0f, 0.0f, (float)offset.x, 0.0f, 1.0f, (float)offset.y, 0.0f, 0.0f, 1.0f}); + multiply(std::array{1.0f, 0.0f, sc(offset.x), 0.0f, 1.0f, sc(offset.y), 0.0f, 0.0f, 1.0f}); return *this; } diff --git a/src/math/Region.cpp b/src/math/Region.cpp index f05cfb0..d4ed602 100644 --- a/src/math/Region.cpp +++ b/src/math/Region.cpp @@ -1,3 +1,4 @@ +#include "hyprutils/memory/Casts.hpp" #include #include @@ -86,7 +87,7 @@ CRegion& Hyprutils::Math::CRegion::invert(pixman_box32_t* box) { } CRegion& Hyprutils::Math::CRegion::invert(const CBox& box) { - pixman_box32 pixmanBox = {.x1 = (int32_t)box.x, .y1 = (int32_t)box.y, .x2 = (int32_t)box.w + (int32_t)box.x, .y2 = (int32_t)box.h + (int32_t)box.y}; + pixman_box32 pixmanBox = {.x1 = sc(box.x), .y1 = sc(box.y), .x2 = sc(box.w) + sc(box.x), .y2 = sc(box.h) + sc(box.y)}; return this->invert(&pixmanBox); } @@ -104,7 +105,7 @@ CRegion& Hyprutils::Math::CRegion::transform(const eTransform t, double w, doubl clear(); for (auto& r : rects) { - CBox xfmd{(double)r.x1, (double)r.y1, (double)r.x2 - r.x1, (double)r.y2 - r.y1}; + CBox xfmd{r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1}; xfmd.transform(t, w, h); add(xfmd); } @@ -118,7 +119,7 @@ CRegion& Hyprutils::Math::CRegion::expand(double units) { clear(); for (auto& r : rects) { - CBox b{(double)r.x1 - units, (double)r.y1 - units, (double)r.x2 - r.x1 + (units * 2), (double)r.y2 - r.y1 + (units * 2)}; + CBox b{sc(r.x1) - units, sc(r.y1) - units, sc(r.x2) - r.x1 + (units * 2), sc(r.y2) - r.y1 + (units * 2)}; add(b); } @@ -171,7 +172,7 @@ std::vector Hyprutils::Math::CRegion::getRects() const { CBox Hyprutils::Math::CRegion::getExtents() { pixman_box32_t* box = pixman_region32_extents(&m_rRegion); - return {(double)box->x1, (double)box->y1, (double)box->x2 - box->x1, (double)box->y2 - box->y1}; + return {sc(box->x1), sc(box->y1), sc(box->x2) - box->x1, sc(box->y2) - box->y1}; } bool Hyprutils::Math::CRegion::containsPoint(const Vector2D& vec) const { diff --git a/src/math/Vector2D.cpp b/src/math/Vector2D.cpp index 6957154..053379f 100644 --- a/src/math/Vector2D.cpp +++ b/src/math/Vector2D.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -9,7 +10,7 @@ Hyprutils::Math::Vector2D::Vector2D(double xx, double yy) : x(xx), y(yy) { ; } -Hyprutils::Math::Vector2D::Vector2D(int xx, int yy) : x((double)xx), y((double)yy) { +Hyprutils::Math::Vector2D::Vector2D(int xx, int yy) : x(sc(xx)), y(sc(yy)) { ; } @@ -59,23 +60,14 @@ Vector2D Hyprutils::Math::Vector2D::getComponentMax(const Vector2D& other) const Vector2D Hyprutils::Math::Vector2D::transform(eTransform transform, const Vector2D& monitorSize) const { switch (transform) { - case HYPRUTILS_TRANSFORM_NORMAL: - return *this; - case HYPRUTILS_TRANSFORM_90: - return Vector2D(y, monitorSize.y - x); - case HYPRUTILS_TRANSFORM_180: - return Vector2D(monitorSize.x - x, monitorSize.y - y); - case HYPRUTILS_TRANSFORM_270: - return Vector2D(monitorSize.x - y, x); - case HYPRUTILS_TRANSFORM_FLIPPED: - return Vector2D(monitorSize.x - x, y); - case HYPRUTILS_TRANSFORM_FLIPPED_90: - return Vector2D(y, x); - case HYPRUTILS_TRANSFORM_FLIPPED_180: - return Vector2D(x, monitorSize.y - y); - case HYPRUTILS_TRANSFORM_FLIPPED_270: - return Vector2D(monitorSize.x - y, monitorSize.y - x); - default: - return *this; + case HYPRUTILS_TRANSFORM_NORMAL: return *this; + case HYPRUTILS_TRANSFORM_90: return Vector2D(y, monitorSize.y - x); + case HYPRUTILS_TRANSFORM_180: return Vector2D(monitorSize.x - x, monitorSize.y - y); + case HYPRUTILS_TRANSFORM_270: return Vector2D(monitorSize.x - y, x); + case HYPRUTILS_TRANSFORM_FLIPPED: return Vector2D(monitorSize.x - x, y); + case HYPRUTILS_TRANSFORM_FLIPPED_90: return Vector2D(y, x); + case HYPRUTILS_TRANSFORM_FLIPPED_180: return Vector2D(x, monitorSize.y - y); + case HYPRUTILS_TRANSFORM_FLIPPED_270: return Vector2D(monitorSize.x - y, monitorSize.y - x); + default: return *this; } } diff --git a/src/os/Process.cpp b/src/os/Process.cpp index cd6a758..a2442b4 100644 --- a/src/os/Process.cpp +++ b/src/os/Process.cpp @@ -1,4 +1,5 @@ #include +#include using namespace Hyprutils::OS; #include @@ -61,7 +62,7 @@ bool Hyprutils::OS::CProcess::runSync() { dup2(errPipe[1], 2 /* stderr */); // build argv - std::vector argsC; + std::vector argsC; argsC.emplace_back(strdup(m_impl->binary.c_str())); for (auto& arg : m_impl->args) { // TODO: does this leak? Can we just pipe c_str() as the strings won't be realloc'd? @@ -75,7 +76,7 @@ bool Hyprutils::OS::CProcess::runSync() { setenv(n.c_str(), v.c_str(), 1); } - execvp(m_impl->binary.c_str(), (char* const*)argsC.data()); + execvp(m_impl->binary.c_str(), argsC.data()); exit(1); } else { // parent @@ -129,7 +130,7 @@ bool Hyprutils::OS::CProcess::runSync() { if (pollfds[0].revents & POLLIN) { while ((ret = read(outPipe[0], buf.data(), 1023)) > 0) { - m_impl->out += std::string_view{(char*)buf.data(), (size_t)ret}; + m_impl->out += std::string_view{buf.data(), sc(ret)}; } buf.fill(0); @@ -137,7 +138,7 @@ bool Hyprutils::OS::CProcess::runSync() { if (pollfds[1].revents & POLLIN) { while ((ret = read(errPipe[0], buf.data(), 1023)) > 0) { - m_impl->err += std::string_view{(char*)buf.data(), (size_t)ret}; + m_impl->err += std::string_view{buf.data(), sc(ret)}; } buf.fill(0); @@ -146,13 +147,13 @@ bool Hyprutils::OS::CProcess::runSync() { // Final reads. Nonblock, so its ok. while ((ret = read(outPipe[0], buf.data(), 1023)) > 0) { - m_impl->out += std::string_view{(char*)buf.data(), (size_t)ret}; + m_impl->out += std::string_view{buf.data(), sc(ret)}; } buf.fill(0); while ((ret = read(errPipe[0], buf.data(), 1023)) > 0) { - m_impl->err += std::string_view{(char*)buf.data(), (size_t)ret}; + m_impl->err += std::string_view{buf.data(), sc(ret)}; } buf.fill(0); @@ -198,7 +199,7 @@ bool Hyprutils::OS::CProcess::runAsync() { close(socket[0]); close(socket[1]); // build argv - std::vector argsC; + std::vector argsC; argsC.emplace_back(strdup(m_impl->binary.c_str())); for (auto& arg : m_impl->args) { argsC.emplace_back(strdup(arg.c_str())); @@ -216,7 +217,7 @@ bool Hyprutils::OS::CProcess::runAsync() { if (m_impl->stderrFD != -1) dup2(m_impl->stderrFD, 2); - execvp(m_impl->binary.c_str(), (char* const*)argsC.data()); + execvp(m_impl->binary.c_str(), argsC.data()); _exit(0); } close(socket[0]); diff --git a/src/signal/Listener.cpp b/src/signal/Listener.cpp index da49943..6d1746d 100644 --- a/src/signal/Listener.cpp +++ b/src/signal/Listener.cpp @@ -16,5 +16,5 @@ void Hyprutils::Signal::CSignalListener::emitInternal(void* data) { void Hyprutils::Signal::CSignalListener::emit(std::any data) { auto dataTuple = std::tuple(data); - emitInternal(static_cast(&dataTuple)); + emitInternal(&dataTuple); } diff --git a/tests/animation.cpp b/tests/animation.cpp index 112f379..9dbcf84 100644 --- a/tests/animation.cpp +++ b/tests/animation.cpp @@ -62,7 +62,7 @@ class CMyAnimationManager : public CAnimationManager { switch (PAV->m_Type) { case eAVTypes::INT: { - auto avInt = dynamic_cast*>(PAV.get()); + auto avInt = dc*>(PAV.get()); if (!avInt) std::cout << Colors::RED << "Dynamic cast upcast failed" << Colors::RESET; @@ -70,7 +70,7 @@ class CMyAnimationManager : public CAnimationManager { avInt->value() = avInt->begun() + (DELTA * POINTY); } break; case eAVTypes::TEST: { - auto avCustom = dynamic_cast*>(PAV.get()); + auto avCustom = dc*>(PAV.get()); if (!avCustom) std::cout << Colors::RED << "Dynamic cast upcast failed" << Colors::RESET; @@ -93,7 +93,7 @@ class CMyAnimationManager : public CAnimationManager { constexpr const eAVTypes EAVTYPE = std::is_same_v ? eAVTypes::INT : eAVTypes::TEST; const auto PAV = makeShared>(); - PAV->create(EAVTYPE, static_cast(this), PAV, v); + PAV->create(EAVTYPE, sc(this), PAV, v); PAV->setConfig(animationTree.getConfig(animationConfigName)); av = std::move(PAV); } @@ -348,7 +348,7 @@ int main(int argc, char** argv, char** envp) { *s.m_iA = 5; s.m_iA->setCallbackOnEnd([&endCallbackRan](WP v) { endCallbackRan++; - const auto PAV = dynamic_cast*>(v.lock().get()); + const auto PAV = dc*>(v.lock().get()); *PAV = 10; PAV->setCallbackOnEnd([&endCallbackRan](WP v) { endCallbackRan++; });