From 0ccb43f5cdd84d1cc1436e78f901ba49550190ce Mon Sep 17 00:00:00 2001 From: Honkazel <169346573+Honkazel@users.noreply.github.com> Date: Sun, 2 Feb 2025 18:48:52 +0500 Subject: [PATCH] clang-tidy/clang fixes and format --- CMakeLists.txt | 8 + .../hyprutils/animation/AnimatedVariable.hpp | 302 ++++++++------- .../hyprutils/animation/AnimationConfig.hpp | 67 ++-- .../hyprutils/animation/AnimationManager.hpp | 85 +++-- include/hyprutils/animation/BezierCurve.hpp | 35 +- include/hyprutils/math/Box.hpp | 17 +- include/hyprutils/math/Edges.hpp | 1 + include/hyprutils/math/Mat3x3.hpp | 80 ++-- include/hyprutils/math/Misc.hpp | 28 +- include/hyprutils/math/Region.hpp | 110 +++--- include/hyprutils/math/Vector2D.hpp | 168 ++++----- include/hyprutils/memory/ImplBase.hpp | 220 ++++++----- include/hyprutils/memory/SharedPtr.hpp | 274 +++++++------- include/hyprutils/memory/UniquePtr.hpp | 207 ++++++----- include/hyprutils/memory/WeakPtr.hpp | 344 +++++++++--------- include/hyprutils/os/FileDescriptor.hpp | 56 +-- include/hyprutils/os/Process.hpp | 47 ++- include/hyprutils/path/Path.hpp | 31 +- include/hyprutils/signal/Listener.hpp | 55 ++- include/hyprutils/signal/Signal.hpp | 31 +- include/hyprutils/string/String.hpp | 15 +- include/hyprutils/string/VarList.hpp | 103 +++--- include/hyprutils/utils/ScopeGuard.hpp | 19 +- src/animation/AnimatedVariable.cpp | 4 +- src/animation/AnimationManager.cpp | 2 +- src/animation/BezierCurve.cpp | 6 +- src/math/Box.cpp | 4 +- src/math/Mat3x3.cpp | 1 + src/math/Region.cpp | 7 +- src/math/Vector2D.cpp | 19 +- src/os/FileDescriptor.cpp | 9 +- src/os/Process.cpp | 7 +- src/path/Path.cpp | 3 +- src/signal/Signal.cpp | 4 +- src/string/String.cpp | 13 +- src/string/VarList.cpp | 5 +- tests/animation.cpp | 14 +- 37 files changed, 1191 insertions(+), 1210 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bb43aa..ba1e39f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,14 @@ set(LIBDIR ${CMAKE_INSTALL_FULL_LIBDIR}) configure_file(hyprutils.pc.in hyprutils.pc @ONLY) set(CMAKE_CXX_STANDARD 23) +add_compile_options( + -Wall + -Wextra + -Wpedantic + -Wno-unused-parameter + -Wno-unused-value + -Wno-missing-field-initializers) +set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES DEBUG) message(STATUS "Configuring hyprutils in Debug") diff --git a/include/hyprutils/animation/AnimatedVariable.hpp b/include/hyprutils/animation/AnimatedVariable.hpp index cc56434..a703836 100644 --- a/include/hyprutils/animation/AnimatedVariable.hpp +++ b/include/hyprutils/animation/AnimatedVariable.hpp @@ -9,218 +9,216 @@ #include #include -namespace Hyprutils { - namespace Animation { +//NOLINTNEXTLINE +namespace Hyprutils::Animation { + /* A base class for animated variables. */ + class CBaseAnimatedVariable { + public: + using CallbackFun = std::function thisptr)>; - /* A base class for animated variables. */ - class CBaseAnimatedVariable { - public: - using CallbackFun = std::function thisptr)>; + CBaseAnimatedVariable() { + ; // m_bDummy = true; + }; - CBaseAnimatedVariable() { - ; // m_bDummy = true; - }; + void create(CAnimationManager*, int, Memory::CSharedPointer); + void connectToActive(); + void disconnectFromActive(); - void create(CAnimationManager*, int, Memory::CSharedPointer); - void connectToActive(); - void disconnectFromActive(); + /* Needs to call disconnectFromActive to remove `m_pSelf` from the active animation list */ + virtual ~CBaseAnimatedVariable() { + disconnectFromActive(); + }; - /* Needs to call disconnectFromActive to remove `m_pSelf` from the active animation list */ - virtual ~CBaseAnimatedVariable() { - disconnectFromActive(); - }; + virtual void warp(bool endCallback = true, bool forceDisconnect = true) = 0; - virtual void warp(bool endCallback = true, bool forceDisconnect = true) = 0; + CBaseAnimatedVariable(const CBaseAnimatedVariable&) = delete; + CBaseAnimatedVariable(CBaseAnimatedVariable&&) = delete; + CBaseAnimatedVariable& operator=(const CBaseAnimatedVariable&) = delete; + CBaseAnimatedVariable& operator=(CBaseAnimatedVariable&&) = delete; - CBaseAnimatedVariable(const CBaseAnimatedVariable&) = delete; - CBaseAnimatedVariable(CBaseAnimatedVariable&&) = delete; - CBaseAnimatedVariable& operator=(const CBaseAnimatedVariable&) = delete; - CBaseAnimatedVariable& operator=(CBaseAnimatedVariable&&) = delete; + // + void setConfig(Memory::CSharedPointer pConfig) { + m_pConfig = pConfig; + } - // - void setConfig(Memory::CSharedPointer pConfig) { - m_pConfig = pConfig; - } + Memory::CWeakPointer getConfig() const { + return m_pConfig; + } - Memory::CWeakPointer getConfig() const { - return m_pConfig; - } + bool enabled() const; + const std::string& getBezierName() const; + const std::string& getStyle() const; - bool enabled() const; - const std::string& getBezierName() const; - const std::string& getStyle() const; + /* returns the spent (completion) % */ + float getPercent() const; - /* returns the spent (completion) % */ - float getPercent() const; + /* returns the current curve value. */ + float getCurveValue() const; - /* returns the current curve value. */ - float getCurveValue() const; + /* checks if an animation is in progress */ + bool isBeingAnimated() const { + return m_bIsBeingAnimated; + } - /* checks if an animation is in progress */ - bool isBeingAnimated() const { - return m_bIsBeingAnimated; - } + /* checks m_bDummy and m_pAnimationManager */ + bool ok() const; - /* checks m_bDummy and m_pAnimationManager */ - bool ok() const; + /* calls the update callback */ + void onUpdate(); - /* calls the update callback */ - void onUpdate(); - - /* sets a function to be ran when an animation ended. + /* sets a function to be ran when an animation ended. if "remove" is set to true, it will remove the callback when ran. */ - void setCallbackOnEnd(CallbackFun func, bool remove = true); + void setCallbackOnEnd(CallbackFun func, bool remove = true); - /* sets a function to be ran when an animation is started. + /* sets a function to be ran when an animation is started. if "remove" is set to true, it will remove the callback when ran. */ - void setCallbackOnBegin(CallbackFun func, bool remove = true); + void setCallbackOnBegin(CallbackFun func, bool remove = true); - /* sets the update callback, called every time the value is animated and a step is done + /* sets the update callback, called every time the value is animated and a step is done Warning: calling unregisterVar/registerVar in this handler will cause UB */ - void setUpdateCallback(CallbackFun func); + void setUpdateCallback(CallbackFun func); - /* resets all callbacks. Does not call any. */ - void resetAllCallbacks(); + /* resets all callbacks. Does not call any. */ + void resetAllCallbacks(); - void onAnimationEnd(); - void onAnimationBegin(); + void onAnimationEnd(); + void onAnimationBegin(); - /* returns whether the parent CAnimationManager is dead */ - bool isAnimationManagerDead() const; + /* returns whether the parent CAnimationManager is dead */ + bool isAnimationManagerDead() const; - int m_Type = -1; + int m_Type = -1; - protected: - friend class CAnimationManager; + protected: + friend class CAnimationManager; - CAnimationManager* m_pAnimationManager = nullptr; + CAnimationManager* m_pAnimationManager = nullptr; - bool m_bIsConnectedToActive = false; - bool m_bIsBeingAnimated = false; + bool m_bIsConnectedToActive = false; + bool m_bIsBeingAnimated = false; - Memory::CWeakPointer m_pSelf; + Memory::CWeakPointer m_pSelf; - Memory::CWeakPointer m_pSignals; + Memory::CWeakPointer m_pSignals; - private: - Memory::CWeakPointer m_pConfig; + private: + Memory::CWeakPointer m_pConfig; - std::chrono::steady_clock::time_point animationBegin; + std::chrono::steady_clock::time_point animationBegin; - bool m_bDummy = true; + bool m_bDummy = true; - bool m_bRemoveEndAfterRan = true; - bool m_bRemoveBeginAfterRan = true; + bool m_bRemoveEndAfterRan = true; + bool m_bRemoveBeginAfterRan = true; - CallbackFun m_fEndCallback; - CallbackFun m_fBeginCallback; - CallbackFun m_fUpdateCallback; - }; + CallbackFun m_fEndCallback; + CallbackFun m_fBeginCallback; + CallbackFun m_fUpdateCallback; + }; - /* This concept represents the minimum requirement for a type to be used with CGenericAnimatedVariable */ - template - concept AnimatedType = requires(ValueImpl val) { - requires std::is_copy_constructible_v; - { val == val } -> std::same_as; // requires operator== - { val = val }; // requires operator= - }; + /* This concept represents the minimum requirement for a type to be used with CGenericAnimatedVariable */ + template + concept AnimatedType = requires(ValueImpl val) { + requires std::is_copy_constructible_v; + { val == val } -> std::same_as; // requires operator== + { val = val }; // requires operator= + }; - /* + /* A generic class for variables. VarType is the type of the variable to be animated. AnimationContext is there to attach additional data to the animation. In Hyprland that struct would contain a reference to window, workspace or layer for example. */ - template - class CGenericAnimatedVariable : public CBaseAnimatedVariable { - public: - CGenericAnimatedVariable() = default; + template + class CGenericAnimatedVariable : public CBaseAnimatedVariable { + public: + CGenericAnimatedVariable() = default; - void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer> pSelf, - const VarType& initialValue) { - m_Begun = initialValue; - m_Value = initialValue; - m_Goal = initialValue; + void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer> pSelf, + const VarType& initialValue) { + m_Begun = initialValue; + m_Value = initialValue; + m_Goal = initialValue; - CBaseAnimatedVariable::create(pAnimationManager, typeInfo, pSelf); - } + CBaseAnimatedVariable::create(pAnimationManager, typeInfo, pSelf); + } - CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete; - CGenericAnimatedVariable(CGenericAnimatedVariable&&) = delete; - CGenericAnimatedVariable& operator=(const CGenericAnimatedVariable&) = delete; - CGenericAnimatedVariable& operator=(CGenericAnimatedVariable&&) = delete; + CGenericAnimatedVariable(const CGenericAnimatedVariable&) = delete; + CGenericAnimatedVariable(CGenericAnimatedVariable&&) = delete; + CGenericAnimatedVariable& operator=(const CGenericAnimatedVariable&) = delete; + CGenericAnimatedVariable& operator=(CGenericAnimatedVariable&&) = delete; - virtual void warp(bool endCallback = true, bool forceDisconnect = true) { - if (!m_bIsBeingAnimated) - return; + virtual void warp(bool endCallback = true, bool forceDisconnect = true) { + if (!m_bIsBeingAnimated) + return; - m_Value = m_Goal; + m_Value = m_Goal; - onUpdate(); + onUpdate(); - m_bIsBeingAnimated = false; + m_bIsBeingAnimated = false; - if (endCallback) - onAnimationEnd(); + if (endCallback) + onAnimationEnd(); - if (forceDisconnect) - disconnectFromActive(); - } + if (forceDisconnect) + disconnectFromActive(); + } - const VarType& value() const { - return m_Value; - } + const VarType& value() const { + return m_Value; + } - /* used to update the value each tick via the AnimationManager */ - VarType& value() { - return m_Value; - } + /* used to update the value each tick via the AnimationManager */ + VarType& value() { + return m_Value; + } - const VarType& goal() const { - return m_Goal; - } + const VarType& goal() const { + return m_Goal; + } - const VarType& begun() const { - return m_Begun; - } - - CGenericAnimatedVariable& operator=(const VarType& v) { - if (v == m_Goal) - return *this; - - m_Goal = v; - m_Begun = m_Value; - - onAnimationBegin(); + const VarType& begun() const { + return m_Begun; + } + CGenericAnimatedVariable& operator=(const VarType& v) { + if (v == m_Goal) return *this; - } - /* Sets the actual stored value, without affecting the goal, but resets the timer*/ - void setValue(const VarType& v) { - if (v == m_Value) - return; + m_Goal = v; + m_Begun = m_Value; - m_Value = v; - m_Begun = m_Value; + onAnimationBegin(); - onAnimationBegin(); - } + return *this; + } - /* Sets the actual value and goal*/ - void setValueAndWarp(const VarType& v) { - m_Goal = v; - m_bIsBeingAnimated = true; + /* Sets the actual stored value, without affecting the goal, but resets the timer*/ + void setValue(const VarType& v) { + if (v == m_Value) + return; - warp(); - } + m_Value = v; + m_Begun = m_Value; - AnimationContext m_Context; + onAnimationBegin(); + } - private: - VarType m_Value{}; - VarType m_Goal{}; - VarType m_Begun{}; - }; - } + /* Sets the actual value and goal*/ + void setValueAndWarp(const VarType& v) { + m_Goal = v; + m_bIsBeingAnimated = true; + + warp(); + } + + AnimationContext m_Context; + + private: + VarType m_Value{}; + VarType m_Goal{}; + VarType m_Begun{}; + }; } diff --git a/include/hyprutils/animation/AnimationConfig.hpp b/include/hyprutils/animation/AnimationConfig.hpp index 3399afc..03a60d8 100644 --- a/include/hyprutils/animation/AnimationConfig.hpp +++ b/include/hyprutils/animation/AnimationConfig.hpp @@ -6,52 +6,51 @@ #include #include -namespace Hyprutils { - namespace Animation { - /* +//NOLINTNEXTLINE +namespace Hyprutils::Animation { + /* Structure for animation properties. Config properties need to have a static lifetime to allow for config reload. */ - struct SAnimationPropertyConfig { - bool overridden = false; + struct SAnimationPropertyConfig { + bool overridden = false; - std::string internalBezier = ""; - std::string internalStyle = ""; - float internalSpeed = 0.f; - int internalEnabled = -1; + std::string internalBezier = ""; + std::string internalStyle = ""; + float internalSpeed = 0.f; + int internalEnabled = -1; - Memory::CWeakPointer pValues; - Memory::CWeakPointer pParentAnimation; - }; + Memory::CWeakPointer pValues; + Memory::CWeakPointer pParentAnimation; + }; - /* A class to manage SAnimationPropertyConfig objects in a tree structure */ - class CAnimationConfigTree { - public: - CAnimationConfigTree() = default; - ~CAnimationConfigTree() = default; + /* A class to manage SAnimationPropertyConfig objects in a tree structure */ + class CAnimationConfigTree { + public: + CAnimationConfigTree() = default; + ~CAnimationConfigTree() = default; - /* Add a new animation node inheriting from a parent. + /* Add a new animation node inheriting from a parent. If parent is empty, a root node will be created that references it's own values. Make sure the parent node has already been created through this interface. */ - void createNode(const std::string& nodeName, const std::string& parent = ""); + void createNode(const std::string& nodeName, const std::string& parent = ""); - /* check if a node name has been created using createNode */ - bool nodeExists(const std::string& nodeName) const; + /* check if a node name has been created using createNode */ + bool nodeExists(const std::string& nodeName) const; - /* Override the values of a node. The root node can also be overriden. */ - void setConfigForNode(const std::string& nodeName, int enabled, float speed, const std::string& bezier, const std::string& style = ""); + /* Override the values of a node. The root node can also be overriden. */ + void setConfigForNode(const std::string& nodeName, int enabled, float speed, const std::string& bezier, const std::string& style = ""); - Memory::CSharedPointer getConfig(const std::string& name) const; - const std::unordered_map>& getFullConfig() const; + Memory::CSharedPointer getConfig(const std::string& name) const; + const std::unordered_map>& getFullConfig() const; - CAnimationConfigTree(const CAnimationConfigTree&) = delete; - CAnimationConfigTree(CAnimationConfigTree&&) = delete; - CAnimationConfigTree& operator=(const CAnimationConfigTree&) = delete; - CAnimationConfigTree& operator=(CAnimationConfigTree&&) = delete; + CAnimationConfigTree(const CAnimationConfigTree&) = delete; + CAnimationConfigTree(CAnimationConfigTree&&) = delete; + CAnimationConfigTree& operator=(const CAnimationConfigTree&) = delete; + CAnimationConfigTree& operator=(CAnimationConfigTree&&) = delete; - private: - void setAnimForChildren(Memory::CSharedPointer PANIM); - std::unordered_map> m_mAnimationConfig; - }; - } + private: + void setAnimForChildren(Memory::CSharedPointer PANIM); + std::unordered_map> m_mAnimationConfig; + }; } diff --git a/include/hyprutils/animation/AnimationManager.hpp b/include/hyprutils/animation/AnimationManager.hpp index 1266e2d..89bb63f 100644 --- a/include/hyprutils/animation/AnimationManager.hpp +++ b/include/hyprutils/animation/AnimationManager.hpp @@ -9,55 +9,54 @@ #include #include -namespace Hyprutils { - namespace Animation { - class CBaseAnimatedVariable; +//NOLINTNEXTLINE +namespace Hyprutils::Animation { + class CBaseAnimatedVariable; - /* A class for managing bezier curves and variables that are being animated. */ - class CAnimationManager { - public: - CAnimationManager(); - virtual ~CAnimationManager() = default; + /* A class for managing bezier curves and variables that are being animated. */ + class CAnimationManager { + public: + CAnimationManager(); + virtual ~CAnimationManager() = default; - void tickDone(); - void rotateActive(); - bool shouldTickForNext(); + void tickDone(); + void rotateActive(); + bool shouldTickForNext(); - virtual void scheduleTick() = 0; - virtual void onTicked() = 0; + virtual void scheduleTick() = 0; + virtual void onTicked() = 0; - void addBezierWithName(std::string, const Math::Vector2D&, const Math::Vector2D&); - void removeAllBeziers(); + void addBezierWithName(std::string, const Math::Vector2D&, const Math::Vector2D&); + void removeAllBeziers(); - bool bezierExists(const std::string&); - Memory::CSharedPointer getBezier(const std::string&); + bool bezierExists(const std::string&); + Memory::CSharedPointer getBezier(const std::string&); - const std::unordered_map>& getAllBeziers(); + const std::unordered_map>& getAllBeziers(); - struct SAnimationManagerSignals { - Signal::CSignal connect; // WP - Signal::CSignal disconnect; // WP - }; - - Memory::CWeakPointer getSignals() const; - - std::vector> m_vActiveAnimatedVariables; - - private: - std::unordered_map> m_mBezierCurves; - - bool m_bTickScheduled = false; - - void onConnect(std::any data); - void onDisconnect(std::any data); - - struct SAnimVarListeners { - Signal::CHyprSignalListener connect; - Signal::CHyprSignalListener disconnect; - }; - - Memory::CUniquePointer m_listeners; - Memory::CUniquePointer m_events; + struct SAnimationManagerSignals { + Signal::CSignal connect; // WP + Signal::CSignal disconnect; // WP }; - } + + Memory::CWeakPointer getSignals() const; + + std::vector> m_vActiveAnimatedVariables; + + private: + std::unordered_map> m_mBezierCurves; + + bool m_bTickScheduled = false; + + void onConnect(std::any data); + void onDisconnect(std::any data); + + struct SAnimVarListeners { + Signal::CHyprSignalListener connect; + Signal::CHyprSignalListener disconnect; + }; + + Memory::CUniquePointer m_listeners; + Memory::CUniquePointer m_events; + }; } diff --git a/include/hyprutils/animation/BezierCurve.hpp b/include/hyprutils/animation/BezierCurve.hpp index b1647df..cfec62a 100644 --- a/include/hyprutils/animation/BezierCurve.hpp +++ b/include/hyprutils/animation/BezierCurve.hpp @@ -5,26 +5,25 @@ #include "../math/Vector2D.hpp" -namespace Hyprutils { - namespace Animation { - constexpr int BAKEDPOINTS = 255; - constexpr float INVBAKEDPOINTS = 1.f / BAKEDPOINTS; +//NOLINTNEXTLINE +namespace Hyprutils::Animation { + constexpr int BAKEDPOINTS = 255; + constexpr float INVBAKEDPOINTS = 1.f / BAKEDPOINTS; - /* An implementation of a cubic bezier curve. */ - class CBezierCurve { - public: - /* Calculates a cubic bezier curve based on 2 control points (EXCLUDES the 0,0 and 1,1 points). */ - void setup(const std::array& points); + /* An implementation of a cubic bezier curve. */ + class CBezierCurve { + public: + /* Calculates a cubic bezier curve based on 2 control points (EXCLUDES the 0,0 and 1,1 points). */ + void setup(const std::array& points); - float getYForT(float const& t) const; - float getXForT(float const& t) const; - float getYForPoint(float const& x) const; + float getYForT(float const& t) const; + float getXForT(float const& t) const; + float getYForPoint(float const& x) const; - private: - /* this INCLUDES the 0,0 and 1,1 points. */ - std::vector m_vPoints; + private: + /* this INCLUDES the 0,0 and 1,1 points. */ + std::vector m_vPoints; - std::array m_aPointsBaked; - }; - } + std::array m_aPointsBaked; + }; } diff --git a/include/hyprutils/math/Box.hpp b/include/hyprutils/math/Box.hpp index b59c312..93beffe 100644 --- a/include/hyprutils/math/Box.hpp +++ b/include/hyprutils/math/Box.hpp @@ -18,14 +18,14 @@ namespace Hyprutils::Math { * @return Scaled SBoxExtents. */ SBoxExtents operator*(const double& scale) const { - return SBoxExtents{topLeft * scale, bottomRight * scale}; + return SBoxExtents{.topLeft = topLeft * scale, .bottomRight = bottomRight * scale}; } /** * @brief Rounds the coordinates of the extents. * @return Rounded SBoxExtents. */ SBoxExtents round() { - return {topLeft.round(), bottomRight.round()}; + return {.topLeft = topLeft.round(), .bottomRight = bottomRight.round()}; } /** * @brief Checks equality between two SBoxExtents objects. @@ -58,9 +58,7 @@ namespace Hyprutils::Math { * @param w_ Width of the box. * @param h_ Height of the box. */ - CBox(double x_, double y_, double w_, double h_) { - x = x_; - y = y_; + CBox(double x_, double y_, double w_, double h_) : x(x_), y(y_) { w = w_; h = h_; } @@ -75,9 +73,8 @@ namespace Hyprutils::Math { * @brief Constructs a CBox with uniform dimensions. * @param d Dimensions to apply uniformly (x, y, width, height). */ - CBox(const double d) { - x = d; - y = d; + // XD. This comment will be deleted before MR, but it'll be saved in the history. Sometimes it's nice to be an idiot. + CBox(const double d) : x(d), y(d) { w = d; h = d; } @@ -86,9 +83,7 @@ namespace Hyprutils::Math { * @param pos Position vector representing the top-left corner. * @param size Size vector representing width and height. */ - CBox(const Vector2D& pos, const Vector2D& size) { - x = pos.x; - y = pos.y; + CBox(const Vector2D& pos, const Vector2D& size) : x(pos.x), y(pos.y) { w = size.x; h = size.y; } diff --git a/include/hyprutils/math/Edges.hpp b/include/hyprutils/math/Edges.hpp index a1eaef9..8a6a543 100644 --- a/include/hyprutils/math/Edges.hpp +++ b/include/hyprutils/math/Edges.hpp @@ -1,6 +1,7 @@ #pragma once #include +//NOLINTNEXTLINE namespace Hyprutils::Math { /** diff --git a/include/hyprutils/math/Mat3x3.hpp b/include/hyprutils/math/Mat3x3.hpp index 040ee8e..39b4e8a 100644 --- a/include/hyprutils/math/Mat3x3.hpp +++ b/include/hyprutils/math/Mat3x3.hpp @@ -7,53 +7,53 @@ #include "./Misc.hpp" -namespace Hyprutils { - namespace Math { - class CBox; - class Vector2D; +//NOLINTNEXTLINE +namespace Hyprutils::Math { + class CBox; + class Vector2D; - class Mat3x3 { - public: - Mat3x3(); - Mat3x3(std::array); - Mat3x3(std::vector); + //NOLINTNEXTLINE + class Mat3x3 { + public: + Mat3x3(); + Mat3x3(std::array); + Mat3x3(std::vector); - /* create an identity 3x3 matrix */ - static Mat3x3 identity(); + /* create an identity 3x3 matrix */ + static Mat3x3 identity(); - /* create an output projection matrix */ - static Mat3x3 outputProjection(const Vector2D& size, eTransform transform); + /* create an output projection matrix */ + static Mat3x3 outputProjection(const Vector2D& size, eTransform transform); - /* get the matrix as an array, in a row-major order. */ - std::array getMatrix() const; + /* get the matrix as an array, in a row-major order. */ + std::array getMatrix() const; - /* create a box projection matrix */ - Mat3x3 projectBox(const CBox& box, eTransform transform, float rot = 0.F /* rad, CCW */) const; + /* create a box projection matrix */ + Mat3x3 projectBox(const CBox& box, eTransform transform, float rot = 0.F /* rad, CCW */) const; - /* in-place functions */ - Mat3x3& transform(eTransform transform); - Mat3x3& rotate(float rot /* rad, CCW */); - Mat3x3& scale(const Vector2D& scale); - Mat3x3& scale(const float scale); - Mat3x3& translate(const Vector2D& offset); - Mat3x3& transpose(); - Mat3x3& multiply(const Mat3x3& other); + /* in-place functions */ + Mat3x3& transform(eTransform transform); + Mat3x3& rotate(float rot /* rad, CCW */); + Mat3x3& scale(const Vector2D& scale); + Mat3x3& scale(const float scale); + Mat3x3& translate(const Vector2D& offset); + Mat3x3& transpose(); + Mat3x3& multiply(const Mat3x3& other); - /* misc utils */ - Mat3x3 copy() const; - std::string toString() const; + /* misc utils */ + Mat3x3 copy() const; + std::string toString() const; - bool operator==(const Mat3x3& other) const { - return other.matrix == matrix; - } + bool operator==(const Mat3x3& other) const { + return other.matrix == matrix; + } - friend std::ostream& operator<<(std::ostream& os, const Mat3x3& mat) { - os << mat.toString(); - return os; - } + friend std::ostream& operator<<(std::ostream& os, const Mat3x3& mat) { + os << mat.toString(); + return os; + } - private: - std::array matrix; - }; - } -} \ No newline at end of file + private: + std::array matrix; + }; +} diff --git a/include/hyprutils/math/Misc.hpp b/include/hyprutils/math/Misc.hpp index 33f446f..9fff632 100644 --- a/include/hyprutils/math/Misc.hpp +++ b/include/hyprutils/math/Misc.hpp @@ -1,16 +1,16 @@ #pragma once -namespace Hyprutils { - namespace Math { - enum eTransform { - HYPRUTILS_TRANSFORM_NORMAL = 0, - HYPRUTILS_TRANSFORM_90 = 1, - HYPRUTILS_TRANSFORM_180 = 2, - HYPRUTILS_TRANSFORM_270 = 3, - HYPRUTILS_TRANSFORM_FLIPPED = 4, - HYPRUTILS_TRANSFORM_FLIPPED_90 = 5, - HYPRUTILS_TRANSFORM_FLIPPED_180 = 6, - HYPRUTILS_TRANSFORM_FLIPPED_270 = 7, - }; - } -} \ No newline at end of file +//NOLINTNEXTLINE +namespace Hyprutils::Math { + // Welcome nahui + enum eTransform : unsigned char { + HYPRUTILS_TRANSFORM_NORMAL = 0, + HYPRUTILS_TRANSFORM_90 = 1, + HYPRUTILS_TRANSFORM_180 = 2, + HYPRUTILS_TRANSFORM_270 = 3, + HYPRUTILS_TRANSFORM_FLIPPED = 4, + HYPRUTILS_TRANSFORM_FLIPPED_90 = 5, + HYPRUTILS_TRANSFORM_FLIPPED_180 = 6, + HYPRUTILS_TRANSFORM_FLIPPED_270 = 7, + }; +} diff --git a/include/hyprutils/math/Region.hpp b/include/hyprutils/math/Region.hpp index f7589f2..466f1bf 100644 --- a/include/hyprutils/math/Region.hpp +++ b/include/hyprutils/math/Region.hpp @@ -5,67 +5,67 @@ #include "Vector2D.hpp" #include "Box.hpp" -namespace Hyprutils { - namespace Math { - class CRegion { - public: - /* Create an empty region */ - CRegion(); - /* Create from a reference. Copies, does not own. */ - CRegion(const pixman_region32_t* const ref); - /* Create from a box */ - CRegion(double x, double y, double w, double h); - /* Create from a CBox */ - CRegion(const CBox& box); - /* Create from a pixman_box32_t */ - CRegion(pixman_box32_t* box); +//NOLINTNEXTLINE +namespace Hyprutils::Math { + class CRegion { + public: + /* Create an empty region */ + CRegion(); + /* Create from a reference. Copies, does not own. */ + CRegion(const pixman_region32_t* const ref); + /* Create from a box */ + CRegion(double x, double y, double w, double h); + /* Create from a CBox */ + CRegion(const CBox& box); + /* Create from a pixman_box32_t */ + CRegion(pixman_box32_t* box); - CRegion(const CRegion&); - CRegion(CRegion&&); + CRegion(const CRegion&); + CRegion(CRegion&&) noexcept; - ~CRegion(); + ~CRegion(); - CRegion& operator=(CRegion&& other) { - pixman_region32_copy(&m_rRegion, other.pixman()); - return *this; - } + CRegion& operator=(CRegion&& other) noexcept { + pixman_region32_copy(&m_rRegion, other.pixman()); + return *this; + } - CRegion& operator=(CRegion& other) { - pixman_region32_copy(&m_rRegion, other.pixman()); - return *this; - } + // CRegion&& will shit-talkin you about being non-redeclarable and CRegion const&... pixman moment. + CRegion& operator=(CRegion other) { + pixman_region32_copy(&m_rRegion, other.pixman()); + return *this; + } - CRegion& clear(); - CRegion& set(const CRegion& other); - CRegion& add(const CRegion& other); - CRegion& add(double x, double y, double w, double h); - CRegion& add(const CBox& other); - CRegion& subtract(const CRegion& other); - CRegion& intersect(const CRegion& other); - CRegion& intersect(double x, double y, double w, double h); - CRegion& translate(const Vector2D& vec); - CRegion& transform(const eTransform t, double w, double h); - CRegion& invert(pixman_box32_t* box); - CRegion& invert(const CBox& box); - CRegion& scale(float scale); - CRegion& scale(const Vector2D& scale); - CRegion& expand(double units); - CRegion& rationalize(); - CBox getExtents(); - bool containsPoint(const Vector2D& vec) const; - bool empty() const; - Vector2D closestPoint(const Vector2D& vec) const; - CRegion copy() const; + CRegion& clear(); + CRegion& set(const CRegion& other); + CRegion& add(const CRegion& other); + CRegion& add(double x, double y, double w, double h); + CRegion& add(const CBox& other); + CRegion& subtract(const CRegion& other); + CRegion& intersect(const CRegion& other); + CRegion& intersect(double x, double y, double w, double h); + CRegion& translate(const Vector2D& vec); + CRegion& transform(const eTransform t, double w, double h); + CRegion& invert(pixman_box32_t* box); + CRegion& invert(const CBox& box); + CRegion& scale(float scale); + CRegion& scale(const Vector2D& scale); + CRegion& expand(double units); + CRegion& rationalize(); + CBox getExtents(); + bool containsPoint(const Vector2D& vec) const; + bool empty() const; + Vector2D closestPoint(const Vector2D& vec) const; + CRegion copy() const; - std::vector getRects() const; + std::vector getRects() const; - // - pixman_region32_t* pixman() { - return &m_rRegion; - } + // + pixman_region32_t* pixman() { + return &m_rRegion; + } - private: - pixman_region32_t m_rRegion; - }; - } + private: + pixman_region32_t m_rRegion; + }; } diff --git a/include/hyprutils/math/Vector2D.hpp b/include/hyprutils/math/Vector2D.hpp index f59e1e6..26e1ddd 100644 --- a/include/hyprutils/math/Vector2D.hpp +++ b/include/hyprutils/math/Vector2D.hpp @@ -3,102 +3,102 @@ #include #include -namespace Hyprutils { - namespace Math { - class Vector2D { - public: - Vector2D(double, double); - Vector2D(int, int); - Vector2D(); - ~Vector2D(); +//NOLINTNEXTLINE +namespace Hyprutils::Math { + //NOLINTNEXTLINE + class Vector2D { + public: + Vector2D(double, double); + Vector2D(int, int); + Vector2D(); + ~Vector2D(); - double x = 0; - double y = 0; + double x = 0; + double y = 0; - // returns the scale - double normalize(); + // returns the scale + double normalize(); - Vector2D operator+(const Vector2D& a) const { - return Vector2D(this->x + a.x, this->y + a.y); - } - Vector2D operator-(const Vector2D& a) const { - return Vector2D(this->x - a.x, this->y - a.y); - } - Vector2D operator-() const { - return Vector2D(-this->x, -this->y); - } - Vector2D operator*(const double& a) const { - return Vector2D(this->x * a, this->y * a); - } - Vector2D operator/(const double& a) const { - return Vector2D(this->x / a, this->y / a); - } + Vector2D operator+(const Vector2D& a) const { + return Vector2D(this->x + a.x, this->y + a.y); + } + Vector2D operator-(const Vector2D& a) const { + return Vector2D(this->x - a.x, this->y - a.y); + } + Vector2D operator-() const { + return Vector2D(-this->x, -this->y); + } + Vector2D operator*(const double& a) const { + return Vector2D(this->x * a, this->y * a); + } + Vector2D operator/(const double& a) const { + return Vector2D(this->x / a, this->y / a); + } - bool operator==(const Vector2D& a) const { - return a.x == x && a.y == y; - } + bool operator==(const Vector2D& a) const { + return a.x == x && a.y == y; + } - bool operator!=(const Vector2D& a) const { - return a.x != x || a.y != y; - } + bool operator!=(const Vector2D& a) const { + return a.x != x || a.y != y; + } - Vector2D operator*(const Vector2D& a) const { - return Vector2D(this->x * a.x, this->y * a.y); - } + Vector2D operator*(const Vector2D& a) const { + return Vector2D(this->x * a.x, this->y * a.y); + } - Vector2D operator/(const Vector2D& a) const { - return Vector2D(this->x / a.x, this->y / a.y); - } + Vector2D operator/(const Vector2D& a) const { + return Vector2D(this->x / a.x, this->y / a.y); + } - bool operator>(const Vector2D& a) const { - return this->x > a.x && this->y > a.y; - } + bool operator>(const Vector2D& a) const { + return this->x > a.x && this->y > a.y; + } - bool operator<(const Vector2D& a) const { - return this->x < a.x && this->y < a.y; - } - Vector2D& operator+=(const Vector2D& a) { - this->x += a.x; - this->y += a.y; - return *this; - } - Vector2D& operator-=(const Vector2D& a) { - this->x -= a.x; - this->y -= a.y; - return *this; - } - Vector2D& operator*=(const Vector2D& a) { - this->x *= a.x; - this->y *= a.y; - return *this; - } - Vector2D& operator/=(const Vector2D& a) { - this->x /= a.x; - this->y /= a.y; - return *this; - } - Vector2D& operator*=(const double& a) { - this->x *= a; - this->y *= a; - return *this; - } - Vector2D& operator/=(const double& a) { - this->x /= a; - this->y /= a; - return *this; - } + bool operator<(const Vector2D& a) const { + return this->x < a.x && this->y < a.y; + } + Vector2D& operator+=(const Vector2D& a) { + this->x += a.x; + this->y += a.y; + return *this; + } + Vector2D& operator-=(const Vector2D& a) { + this->x -= a.x; + this->y -= a.y; + return *this; + } + Vector2D& operator*=(const Vector2D& a) { + this->x *= a.x; + this->y *= a.y; + return *this; + } + Vector2D& operator/=(const Vector2D& a) { + this->x /= a.x; + this->y /= a.y; + return *this; + } + Vector2D& operator*=(const double& a) { + this->x *= a; + this->y *= a; + return *this; + } + Vector2D& operator/=(const double& a) { + this->x /= a; + this->y /= a; + return *this; + } - double distance(const Vector2D& other) const; - double distanceSq(const Vector2D& other) const; - double size() const; - Vector2D clamp(const Vector2D& min, const Vector2D& max = Vector2D{-1, -1}) const; + double distance(const Vector2D& other) const; + double distanceSq(const Vector2D& other) const; + double size() const; + Vector2D clamp(const Vector2D& min, const Vector2D& max = Vector2D{-1, -1}) const; - Vector2D floor() const; - Vector2D round() const; + Vector2D floor() const; + Vector2D round() const; - Vector2D getComponentMax(const Vector2D& other) const; - }; - } + Vector2D getComponentMax(const Vector2D& other) const; + }; } // absolutely ridiculous formatter spec parsing diff --git a/include/hyprutils/memory/ImplBase.hpp b/include/hyprutils/memory/ImplBase.hpp index 9b9de54..1a9021c 100644 --- a/include/hyprutils/memory/ImplBase.hpp +++ b/include/hyprutils/memory/ImplBase.hpp @@ -2,118 +2,116 @@ #include -namespace Hyprutils { - namespace Memory { - namespace Impl_ { - class impl_base { - public: - virtual ~impl_base() {}; +//NOLINTNEXTLINE +namespace Hyprutils::Memory::Impl_ { + //NOLINTNEXTLINE + class impl_base { + public: + virtual ~impl_base() = default; - virtual void inc() noexcept = 0; - virtual void dec() noexcept = 0; - virtual void incWeak() noexcept = 0; - virtual void decWeak() noexcept = 0; - virtual unsigned int ref() noexcept = 0; - virtual unsigned int wref() noexcept = 0; - virtual void destroy() noexcept = 0; - virtual bool destroying() noexcept = 0; - virtual bool dataNonNull() noexcept = 0; - virtual bool lockable() noexcept = 0; - virtual void* getData() noexcept = 0; - }; + virtual void inc() noexcept = 0; + virtual void dec() noexcept = 0; + virtual void incWeak() noexcept = 0; + virtual void decWeak() noexcept = 0; + virtual unsigned int ref() noexcept = 0; + virtual unsigned int wref() noexcept = 0; + virtual void destroy() noexcept = 0; + virtual bool destroying() noexcept = 0; + virtual bool dataNonNull() noexcept = 0; + virtual bool lockable() noexcept = 0; + virtual void* getData() noexcept = 0; + }; - template - class impl : public impl_base { - public: - impl(T* data, bool lock = true) noexcept : _data(data), _lockable(lock) { - ; - } - - /* strong refcount */ - unsigned int _ref = 0; - /* weak refcount */ - unsigned int _weak = 0; - /* if this is lockable (shared) */ - bool _lockable = true; - - T* _data = nullptr; - - friend void swap(impl*& a, impl*& b) { - impl* tmp = a; - a = b; - b = tmp; - } - - /* if the destructor was called, - creating shared_ptrs is no longer valid */ - bool _destroying = false; - - void _destroy() { - if (!_data || _destroying) - return; - - // first, we destroy the data, but keep the pointer. - // this way, weak pointers will still be able to - // reference and use, but no longer create shared ones. - _destroying = true; - __deleter(_data); - // now, we can reset the data and call it a day. - _data = nullptr; - _destroying = false; - } - - std::default_delete __deleter{}; - - // - virtual void inc() noexcept { - _ref++; - } - - virtual void dec() noexcept { - _ref--; - } - - virtual void incWeak() noexcept { - _weak++; - } - - virtual void decWeak() noexcept { - _weak--; - } - - virtual unsigned int ref() noexcept { - return _ref; - } - - virtual unsigned int wref() noexcept { - return _weak; - } - - virtual void destroy() noexcept { - _destroy(); - } - - virtual bool destroying() noexcept { - return _destroying; - } - - virtual bool lockable() noexcept { - return _lockable; - } - - virtual bool dataNonNull() noexcept { - return _data != nullptr; - } - - virtual void* getData() noexcept { - return _data; - } - - virtual ~impl() { - destroy(); - } - }; + template + //NOLINTNEXTLINE + class impl : public impl_base { + public: + impl(T* data, bool lock = true) noexcept : _data(data), _lockable(lock) { + ; } - } -} \ No newline at end of file + /* strong refcount */ + unsigned int _ref = 0; + /* weak refcount */ + unsigned int _weak = 0; + /* if this is lockable (shared) */ + bool _lockable = true; + + T* _data = nullptr; + + friend void swap(impl*& a, impl*& b) noexcept { + impl* tmp = a; + a = b; + b = tmp; + } + + /* if the destructor was called, + creating shared_ptrs is no longer valid */ + bool _destroying = false; + + void _destroy() { + if (!_data || _destroying) + return; + + // first, we destroy the data, but keep the pointer. + // this way, weak pointers will still be able to + // reference and use, but no longer create shared ones. + _destroying = true; + _deleter(_data); + // now, we can reset the data and call it a day. + _data = nullptr; + _destroying = false; + } + + std::default_delete _deleter{}; + + // + virtual void inc() noexcept { + _ref++; + } + + virtual void dec() noexcept { + _ref--; + } + + virtual void incWeak() noexcept { + _weak++; + } + + virtual void decWeak() noexcept { + _weak--; + } + + virtual unsigned int ref() noexcept { + return _ref; + } + + virtual unsigned int wref() noexcept { + return _weak; + } + + virtual void destroy() noexcept { + _destroy(); + } + + virtual bool destroying() noexcept { + return _destroying; + } + + virtual bool lockable() noexcept { + return _lockable; + } + + virtual bool dataNonNull() noexcept { + return _data != nullptr; + } + + virtual void* getData() noexcept { + return _data; + } + + virtual ~impl() { + destroy(); + } + }; +} diff --git a/include/hyprutils/memory/SharedPtr.hpp b/include/hyprutils/memory/SharedPtr.hpp index d2c5995..5f0d2ae 100644 --- a/include/hyprutils/memory/SharedPtr.hpp +++ b/include/hyprutils/memory/SharedPtr.hpp @@ -13,178 +13,174 @@ or deref an existing one inside the destructor. */ -namespace Hyprutils { - namespace Memory { +namespace Hyprutils::Memory { + template + class CSharedPointer { + public: + template + using validHierarchy = std::enable_if_t&, X>, CSharedPointer&>; + template + using isConstructible = std::enable_if_t>; - template - class CSharedPointer { - public: - template - using validHierarchy = typename std::enable_if&, X>::value, CSharedPointer&>::type; - template - using isConstructible = typename std::enable_if::value>::type; - - /* creates a new shared pointer managing a resource + /* creates a new shared pointer managing a resource avoid calling. Could duplicate ownership. Prefer makeShared */ - explicit CSharedPointer(T* object) noexcept { - impl_ = new Impl_::impl(object); - increment(); - } + explicit CSharedPointer(T* object) noexcept : impl_(new Impl_::impl(object)) { + increment(); + } - /* creates a shared pointer from a reference */ - template > - CSharedPointer(const CSharedPointer& ref) noexcept { - impl_ = ref.impl_; - increment(); - } + /* creates a shared pointer from a reference */ + template > + CSharedPointer(const CSharedPointer& ref) noexcept : impl_(ref.impl_) { + increment(); + } - CSharedPointer(const CSharedPointer& ref) noexcept { - impl_ = ref.impl_; - increment(); - } + CSharedPointer(const CSharedPointer& ref) noexcept : impl_(ref.impl_) { + increment(); + } - template > - CSharedPointer(CSharedPointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + template > + // reason - ref param not moved. But is that correct? + CSharedPointer(CSharedPointer& ref) noexcept { + std::swap(impl_, ref.impl_); + } - CSharedPointer(CSharedPointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + CSharedPointer(CSharedPointer&& ref) noexcept { + std::swap(impl_, ref.impl_); + } - /* allows weakPointer to create from an impl */ - CSharedPointer(Impl_::impl_base* implementation) noexcept { - impl_ = implementation; - increment(); - } + /* allows weakPointer to create from an impl */ + CSharedPointer(Impl_::impl_base* implementation) noexcept : impl_(implementation) { + increment(); + } - /* creates an empty shared pointer with no implementation */ - CSharedPointer() noexcept { - ; // empty - } + /* creates an empty shared pointer with no implementation */ + CSharedPointer() noexcept { + ; // empty + } - /* creates an empty shared pointer with no implementation */ - CSharedPointer(std::nullptr_t) noexcept { - ; // empty - } + /* creates an empty shared pointer with no implementation */ + CSharedPointer(std::nullptr_t) noexcept { + ; // empty + } - ~CSharedPointer() { - decrement(); - } + ~CSharedPointer() { + decrement(); + } - template - validHierarchy&> operator=(const CSharedPointer& rhs) { - if (impl_ == rhs.impl_) - return *this; - - decrement(); - impl_ = rhs.impl_; - increment(); + template + // Same. And also what should i do with the warning of unconventional assign operators? + validHierarchy&> operator=(const CSharedPointer& rhs) { + if (impl_ == rhs.impl_) return *this; - } - CSharedPointer& operator=(const CSharedPointer& rhs) { - if (impl_ == rhs.impl_) - return *this; - - decrement(); - impl_ = rhs.impl_; - increment(); + decrement(); + impl_ = rhs.impl_; + increment(); + return *this; + } + //Self assignment warning. What to do? + CSharedPointer& operator=(const CSharedPointer& rhs) { + if (impl_ == rhs.impl_) return *this; - } - template - validHierarchy&> operator=(CSharedPointer&& rhs) { - std::swap(impl_, rhs.impl_); - return *this; - } + decrement(); + impl_ = rhs.impl_; + increment(); + return *this; + } - CSharedPointer& operator=(CSharedPointer&& rhs) { - std::swap(impl_, rhs.impl_); - return *this; - } + template + // Same + validHierarchy&> operator=(CSharedPointer& rhs) { + std::swap(impl_, rhs.impl_); + return *this; + } - operator bool() const { - return impl_ && impl_->dataNonNull(); - } + CSharedPointer& operator=(CSharedPointer&& rhs) noexcept { + std::swap(impl_, rhs.impl_); + return *this; + } - bool operator==(const CSharedPointer& rhs) const { - return impl_ == rhs.impl_; - } + operator bool() const { + return impl_ && impl_->dataNonNull(); + } - bool operator()(const CSharedPointer& lhs, const CSharedPointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); - } + bool operator==(const CSharedPointer& rhs) const { + return impl_ == rhs.impl_; + } - bool operator<(const CSharedPointer& rhs) const { - return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); - } + bool operator()(const CSharedPointer& lhs, const CSharedPointer& rhs) const { + return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + } - T* operator->() const { - return get(); - } + bool operator<(const CSharedPointer& rhs) const { + return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); + } - T& operator*() const { - return *get(); - } + T* operator->() const { + return get(); + } - void reset() { - decrement(); - impl_ = nullptr; - } + T& operator*() const { + return *get(); + } - T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; - } + void reset() { + decrement(); + impl_ = nullptr; + } - unsigned int strongRef() const { - return impl_ ? impl_->ref() : 0; - } + T* get() const { + return impl_ ? static_cast(impl_->getData()) : nullptr; + } - Impl_::impl_base* impl_ = nullptr; + unsigned int strongRef() const { + return impl_ ? impl_->ref() : 0; + } - private: - /* + Impl_::impl_base* impl_ = nullptr; + + private: + /* no-op if there is no impl_ may delete the stored object if ref == 0 may delete and reset impl_ if ref == 0 and weak == 0 */ - void decrement() { - if (!impl_) - return; + void decrement() { + if (!impl_) + return; - impl_->dec(); + impl_->dec(); - // if ref == 0, we can destroy impl - if (impl_->ref() == 0) - destroyImpl(); - } - /* no-op if there is no impl_ */ - void increment() { - if (!impl_) - return; - - impl_->inc(); - } - - /* destroy the pointed-to object - if able, will also destroy impl */ - void destroyImpl() { - // destroy the impl contents - impl_->destroy(); - - // check for weak refs, if zero, we can also delete impl_ - if (impl_->wref() == 0) { - delete impl_; - impl_ = nullptr; - } - } - }; - - template - static CSharedPointer makeShared(Args&&... args) { - return CSharedPointer(new U(std::forward(args)...)); + // if ref == 0, we can destroy impl + if (impl_->ref() == 0) + destroyImpl(); } + /* no-op if there is no impl_ */ + void increment() { + if (!impl_) + return; + + impl_->inc(); + } + + /* destroy the pointed-to object + if able, will also destroy impl */ + void destroyImpl() { + // destroy the impl contents + impl_->destroy(); + + // check for weak refs, if zero, we can also delete impl_ + if (impl_->wref() == 0) { + delete impl_; + impl_ = nullptr; + } + } + }; + + template + static CSharedPointer makeShared(Args&&... args) { + return CSharedPointer(new U(std::forward(args)...)); } } @@ -193,4 +189,4 @@ struct std::hash> { std::size_t operator()(const Hyprutils::Memory::CSharedPointer& p) const noexcept { return std::hash{}(p.impl_); } -}; \ No newline at end of file +}; diff --git a/include/hyprutils/memory/UniquePtr.hpp b/include/hyprutils/memory/UniquePtr.hpp index 8588560..19d368c 100644 --- a/include/hyprutils/memory/UniquePtr.hpp +++ b/include/hyprutils/memory/UniquePtr.hpp @@ -9,135 +9,134 @@ to be locked. */ -namespace Hyprutils { - namespace Memory { - template - class CUniquePointer { - public: - template - using validHierarchy = typename std::enable_if&, X>::value, CUniquePointer&>::type; - template - using isConstructible = typename std::enable_if::value>::type; +namespace Hyprutils::Memory { + template + class CUniquePointer { + public: + template + using validHierarchy = std::enable_if_t&, X>, CUniquePointer&>; + template + using isConstructible = std::enable_if_t>; - /* creates a new unique pointer managing a resource + /* creates a new unique pointer managing a resource avoid calling. Could duplicate ownership. Prefer makeUnique */ - explicit CUniquePointer(T* object) noexcept { - impl_ = new Impl_::impl(object, false); - increment(); - } + explicit CUniquePointer(T* object) noexcept : impl_(new Impl_::impl(object, false)) { + increment(); + } - /* creates a shared pointer from a reference */ - template > - CUniquePointer(const CUniquePointer& ref) = delete; - CUniquePointer(const CUniquePointer& ref) = delete; + /* creates a shared pointer from a reference */ + template > + CUniquePointer(const CUniquePointer& ref) = delete; + CUniquePointer(const CUniquePointer& ref) = delete; - template > - CUniquePointer(CUniquePointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + template > + // Same + CUniquePointer(CUniquePointer& ref) noexcept { + std::swap(impl_, ref.impl_); + } - CUniquePointer(CUniquePointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + CUniquePointer(CUniquePointer&& ref) noexcept { + std::swap(impl_, ref.impl_); + } - /* creates an empty unique pointer with no implementation */ - CUniquePointer() noexcept { - ; // empty - } + /* creates an empty unique pointer with no implementation */ + CUniquePointer() noexcept { + ; // empty + } - /* creates an empty unique pointer with no implementation */ - CUniquePointer(std::nullptr_t) noexcept { - ; // empty - } + /* creates an empty unique pointer with no implementation */ + CUniquePointer(std::nullptr_t) noexcept { + ; // empty + } - ~CUniquePointer() { - decrement(); - } + ~CUniquePointer() { + decrement(); + } - template - validHierarchy&> operator=(const CUniquePointer& rhs) = delete; - CUniquePointer& operator=(const CUniquePointer& rhs) = delete; + template + validHierarchy&> operator=(const CUniquePointer& rhs) = delete; + CUniquePointer& operator=(const CUniquePointer& rhs) = delete; - template - validHierarchy&> operator=(CUniquePointer&& rhs) { - std::swap(impl_, rhs.impl_); - return *this; - } + template + // Same with both + validHierarchy&> operator=(CUniquePointer& rhs) { + std::swap(impl_, rhs.impl_); + return *this; + } - CUniquePointer& operator=(CUniquePointer&& rhs) { - std::swap(impl_, rhs.impl_); - return *this; - } + CUniquePointer& operator=(CUniquePointer&& rhs) noexcept { + std::swap(impl_, rhs.impl_); + return *this; + } - operator bool() const { - return impl_; - } + operator bool() const { + return impl_; + } - bool operator()(const CUniquePointer& lhs, const CUniquePointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); - } + bool operator()(const CUniquePointer& lhs, const CUniquePointer& rhs) const { + return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + } - T* operator->() const { - return get(); - } + T* operator->() const { + return get(); + } - T& operator*() const { - return *get(); - } + T& operator*() const { + return *get(); + } - void reset() { - decrement(); - impl_ = nullptr; - } + void reset() { + decrement(); + impl_ = nullptr; + } - T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; - } + T* get() const { + return impl_ ? static_cast(impl_->getData()) : nullptr; + } - Impl_::impl_base* impl_ = nullptr; + Impl_::impl_base* impl_ = nullptr; - private: - /* + private: + /* no-op if there is no impl_ may delete the stored object if ref == 0 may delete and reset impl_ if ref == 0 and weak == 0 */ - void decrement() { - if (!impl_) - return; + void decrement() { + if (!impl_) + return; - impl_->dec(); + impl_->dec(); - // if ref == 0, we can destroy impl - if (impl_->ref() == 0) - destroyImpl(); - } - /* no-op if there is no impl_ */ - void increment() { - if (!impl_) - return; - - impl_->inc(); - } - - /* destroy the pointed-to object - if able, will also destroy impl */ - void destroyImpl() { - // destroy the impl contents - impl_->destroy(); - - // check for weak refs, if zero, we can also delete impl_ - if (impl_->wref() == 0) { - delete impl_; - impl_ = nullptr; - } - } - }; - - template - static CUniquePointer makeUnique(Args&&... args) { - return CUniquePointer(new U(std::forward(args)...)); + // if ref == 0, we can destroy impl + if (impl_->ref() == 0) + destroyImpl(); } + /* no-op if there is no impl_ */ + void increment() { + if (!impl_) + return; + + impl_->inc(); + } + + /* destroy the pointed-to object + if able, will also destroy impl */ + void destroyImpl() { + // destroy the impl contents + impl_->destroy(); + + // check for weak refs, if zero, we can also delete impl_ + if (impl_->wref() == 0) { + delete impl_; + impl_ = nullptr; + } + } + }; + + template + static CUniquePointer makeUnique(Args&&... args) { + return CUniquePointer(new U(std::forward(args)...)); } } @@ -146,4 +145,4 @@ struct std::hash> { std::size_t operator()(const Hyprutils::Memory::CUniquePointer& p) const noexcept { return std::hash{}(p.impl_); } -}; \ No newline at end of file +}; diff --git a/include/hyprutils/memory/WeakPtr.hpp b/include/hyprutils/memory/WeakPtr.hpp index 023bf78..9b4c449 100644 --- a/include/hyprutils/memory/WeakPtr.hpp +++ b/include/hyprutils/memory/WeakPtr.hpp @@ -9,206 +9,208 @@ See SharedPtr.hpp for more info on how it's different. */ -namespace Hyprutils { - namespace Memory { - template - class CWeakPointer { - public: - template - using validHierarchy = typename std::enable_if&, X>::value, CWeakPointer&>::type; - template - using isConstructible = typename std::enable_if::value>::type; +//NOLINTNEXTLINE +namespace Hyprutils::Memory { + template + class CWeakPointer { + public: + template + using validHierarchy = std::enable_if_t&, X>, CWeakPointer&>; + template + using isConstructible = std::enable_if_t>; - /* create a weak ptr from a reference */ - template > - CWeakPointer(const CSharedPointer& ref) noexcept { - if (!ref.impl_) - return; + /* create a weak ptr from a reference */ + template > + CWeakPointer(const CSharedPointer& ref) noexcept { + if (!ref.impl_) + return; - impl_ = ref.impl_; - incrementWeak(); - } + impl_ = ref.impl_; + incrementWeak(); + } - /* create a weak ptr from a reference */ - template > - CWeakPointer(const CUniquePointer& ref) noexcept { - if (!ref.impl_) - return; + /* create a weak ptr from a reference */ + template > + CWeakPointer(const CUniquePointer& ref) noexcept { + if (!ref.impl_) + return; - impl_ = ref.impl_; - incrementWeak(); - } + impl_ = ref.impl_; + incrementWeak(); + } - /* create a weak ptr from another weak ptr */ - template > - CWeakPointer(const CWeakPointer& ref) noexcept { - if (!ref.impl_) - return; + /* create a weak ptr from another weak ptr */ + template > + CWeakPointer(const CWeakPointer& ref) noexcept { + if (!ref.impl_) + return; - impl_ = ref.impl_; - incrementWeak(); - } + impl_ = ref.impl_; + incrementWeak(); + } - CWeakPointer(const CWeakPointer& ref) noexcept { - if (!ref.impl_) - return; + CWeakPointer(const CWeakPointer& ref) noexcept { + if (!ref.impl_) + return; - impl_ = ref.impl_; - incrementWeak(); - } + impl_ = ref.impl_; + incrementWeak(); + } - template > - CWeakPointer(CWeakPointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + template > + // Same + CWeakPointer(CWeakPointer& ref) noexcept { + std::swap(impl_, ref.impl_); + } - CWeakPointer(CWeakPointer&& ref) noexcept { - std::swap(impl_, ref.impl_); - } + CWeakPointer(CWeakPointer&& ref) noexcept { + std::swap(impl_, ref.impl_); + } - /* create a weak ptr from another weak ptr with assignment */ - template - validHierarchy&> operator=(const CWeakPointer& rhs) { - if (impl_ == rhs.impl_) - return *this; - - decrementWeak(); - impl_ = rhs.impl_; - incrementWeak(); + /* create a weak ptr from another weak ptr with assignment */ + template + // Same + validHierarchy&> operator=(const CWeakPointer& rhs) { + if (impl_ == rhs.impl_) return *this; - } - CWeakPointer& operator=(const CWeakPointer& rhs) { - if (impl_ == rhs.impl_) - return *this; - - decrementWeak(); - impl_ = rhs.impl_; - incrementWeak(); + decrementWeak(); + impl_ = rhs.impl_; + incrementWeak(); + return *this; + } + // Same + CWeakPointer& operator=(const CWeakPointer& rhs) { + if (impl_ == rhs.impl_) return *this; - } - /* create a weak ptr from a shared ptr with assignment */ - template - validHierarchy&> operator=(const CSharedPointer& rhs) { - if (reinterpret_cast(impl_) == reinterpret_cast(rhs.impl_)) - return *this; + decrementWeak(); + impl_ = rhs.impl_; + incrementWeak(); + return *this; + } - decrementWeak(); - impl_ = rhs.impl_; - incrementWeak(); + /* create a weak ptr from a shared ptr with assignment */ + template + // Same... + validHierarchy&> operator=(const CSharedPointer& rhs) { + if (reinterpret_cast(impl_) == reinterpret_cast(rhs.impl_)) return *this; - } - /* create an empty weak ptr */ - CWeakPointer() { - ; - } + decrementWeak(); + impl_ = rhs.impl_; + incrementWeak(); + return *this; + } - ~CWeakPointer() { - decrementWeak(); - } + /* create an empty weak ptr */ + CWeakPointer() { + ; + } - /* expired MAY return true even if the pointer is still stored. + ~CWeakPointer() { + decrementWeak(); + } + + /* expired MAY return true even if the pointer is still stored. the situation would be e.g. self-weak pointer in a destructor. for pointer validity, use valid() */ - bool expired() const { - return !impl_ || !impl_->dataNonNull() || impl_->destroying(); - } + bool expired() const { + return !impl_ || !impl_->dataNonNull() || impl_->destroying(); + } - /* this means the pointed-to object is not yet deleted and can still be - referenced, but it might be in the process of being deleted. + /* this means the pointed-to object is not yet deleted and can still be + referenced, but it might be in the process of being deleted. check !expired() if you want to check whether it's valid and assignable to a SP. */ - bool valid() const { - return impl_ && impl_->dataNonNull(); - } + bool valid() const { + return impl_ && impl_->dataNonNull(); + } - void reset() { - decrementWeak(); + void reset() { + decrementWeak(); + impl_ = nullptr; + } + + CSharedPointer lock() const { + if (!impl_ || !impl_->dataNonNull() || impl_->destroying() || !impl_->lockable()) + return {}; + + return CSharedPointer(impl_); + } + + /* this returns valid() */ + operator bool() const { + return valid(); + } + + bool operator==(const CWeakPointer& rhs) const { + return impl_ == rhs.impl_; + } + + bool operator==(const CSharedPointer& rhs) const { + return impl_ == rhs.impl_; + } + + bool operator==(const CUniquePointer& rhs) const { + return impl_ == rhs.impl_; + } + + bool operator==(std::nullptr_t) const { + return !valid(); + } + + bool operator!=(std::nullptr_t) const { + return valid(); + } + + bool operator()(const CWeakPointer& lhs, const CWeakPointer& rhs) const { + return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); + } + + bool operator<(const CWeakPointer& rhs) const { + return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); + } + + T* get() const { + return impl_ ? static_cast(impl_->getData()) : nullptr; + } + + T* operator->() const { + return get(); + } + + T& operator*() const { + return *get(); + } + + Impl_::impl_base* impl_ = nullptr; + + private: + /* no-op if there is no impl_ */ + void decrementWeak() { + if (!impl_) + return; + + impl_->decWeak(); + + // we need to check for ->destroying, + // because otherwise we could destroy here + // and have a shared_ptr destroy the same thing + // later (in situations where we have a weak_ptr to self) + if (impl_->wref() == 0 && impl_->ref() == 0 && !impl_->destroying()) { + delete impl_; impl_ = nullptr; } + } + /* no-op if there is no impl_ */ + void incrementWeak() { + if (!impl_) + return; - CSharedPointer lock() const { - if (!impl_ || !impl_->dataNonNull() || impl_->destroying() || !impl_->lockable()) - return {}; - - return CSharedPointer(impl_); - } - - /* this returns valid() */ - operator bool() const { - return valid(); - } - - bool operator==(const CWeakPointer& rhs) const { - return impl_ == rhs.impl_; - } - - bool operator==(const CSharedPointer& rhs) const { - return impl_ == rhs.impl_; - } - - bool operator==(const CUniquePointer& rhs) const { - return impl_ == rhs.impl_; - } - - bool operator==(std::nullptr_t) const { - return !valid(); - } - - bool operator!=(std::nullptr_t) const { - return valid(); - } - - bool operator()(const CWeakPointer& lhs, const CWeakPointer& rhs) const { - return reinterpret_cast(lhs.impl_) < reinterpret_cast(rhs.impl_); - } - - bool operator<(const CWeakPointer& rhs) const { - return reinterpret_cast(impl_) < reinterpret_cast(rhs.impl_); - } - - T* get() const { - return impl_ ? static_cast(impl_->getData()) : nullptr; - } - - T* operator->() const { - return get(); - } - - T& operator*() const { - return *get(); - } - - Impl_::impl_base* impl_ = nullptr; - - private: - /* no-op if there is no impl_ */ - void decrementWeak() { - if (!impl_) - return; - - impl_->decWeak(); - - // we need to check for ->destroying, - // because otherwise we could destroy here - // and have a shared_ptr destroy the same thing - // later (in situations where we have a weak_ptr to self) - if (impl_->wref() == 0 && impl_->ref() == 0 && !impl_->destroying()) { - delete impl_; - impl_ = nullptr; - } - } - /* no-op if there is no impl_ */ - void incrementWeak() { - if (!impl_) - return; - - impl_->incWeak(); - } - }; - } + impl_->incWeak(); + } + }; } template diff --git a/include/hyprutils/os/FileDescriptor.hpp b/include/hyprutils/os/FileDescriptor.hpp index 96761d0..facbc4e 100644 --- a/include/hyprutils/os/FileDescriptor.hpp +++ b/include/hyprutils/os/FileDescriptor.hpp @@ -1,39 +1,39 @@ #pragma once #include -namespace Hyprutils { - namespace OS { - class CFileDescriptor { - public: - CFileDescriptor() = default; - explicit CFileDescriptor(int const fd); - CFileDescriptor(CFileDescriptor&&); - CFileDescriptor& operator=(CFileDescriptor&&); - ~CFileDescriptor(); - CFileDescriptor(const CFileDescriptor&) = delete; - CFileDescriptor& operator=(const CFileDescriptor&) = delete; +//NOLINTNEXTLINE +namespace Hyprutils::OS { + class CFileDescriptor { + public: + CFileDescriptor() = default; + explicit CFileDescriptor(int const fd); + CFileDescriptor(CFileDescriptor&&) noexcept; + CFileDescriptor& operator=(CFileDescriptor&&) noexcept; + ~CFileDescriptor(); - bool operator==(const CFileDescriptor& rhs) const { - return m_fd == rhs.m_fd; - } + CFileDescriptor(const CFileDescriptor&) = delete; + CFileDescriptor& operator=(const CFileDescriptor&) = delete; - bool isValid() const; - int get() const; - int getFlags() const; - bool setFlags(int flags); - int take(); - void reset(); - CFileDescriptor duplicate(int flags = F_DUPFD_CLOEXEC) const; + bool operator==(const CFileDescriptor& rhs) const { + return m_fd == rhs.m_fd; + } - bool isReadable() const; - bool isClosed() const; + bool isValid() const; + int get() const; + int getFlags() const; + bool setFlags(int flags); + int take(); + void reset(); + CFileDescriptor duplicate(int flags = F_DUPFD_CLOEXEC) const; - static bool isReadable(int fd); - static bool isClosed(int fd); + bool isReadable() const; + bool isClosed() const; - private: - int m_fd = -1; - }; + static bool isReadable(int fd); + static bool isClosed(int fd); + + private: + int m_fd = -1; }; }; diff --git a/include/hyprutils/os/Process.hpp b/include/hyprutils/os/Process.hpp index 73c8450..3dee621 100644 --- a/include/hyprutils/os/Process.hpp +++ b/include/hyprutils/os/Process.hpp @@ -5,33 +5,32 @@ #include #include -namespace Hyprutils { - namespace OS { - class CProcess { - public: - /* Creates a process object, doesn't run yet */ - CProcess(const std::string& binary_, const std::vector& args_); +//NOLINTNEXTLINE +namespace Hyprutils::OS { + class CProcess { + public: + /* Creates a process object, doesn't run yet */ + CProcess(const std::string& binary_, const std::vector& args_); - void addEnv(const std::string& name, const std::string& value); + void addEnv(const std::string& name, const std::string& value); - /* Run the process, synchronously, get the stdout and stderr. False on fail */ - bool runSync(); + /* Run the process, synchronously, get the stdout and stderr. False on fail */ + bool runSync(); - /* Run the process, asynchronously. This will detach the process from this object (and process) and let it live a happy life. False on fail. */ - bool runAsync(); + /* Run the process, asynchronously. This will detach the process from this object (and process) and let it live a happy life. False on fail. */ + bool runAsync(); - // only populated when ran sync - const std::string& stdOut(); - const std::string& stdErr(); + // only populated when ran sync + const std::string& stdOut(); + const std::string& stdErr(); - // only populated when ran async - const pid_t pid(); + // only populated when ran async + pid_t pid(); - private: - std::string binary, out, err; - std::vector args; - std::vector> env; - pid_t grandchildPid = 0; - }; - } -} \ No newline at end of file + private: + std::string binary, out, err; + std::vector args; + std::vector> env; + pid_t grandchildPid = 0; + }; +} diff --git a/include/hyprutils/path/Path.hpp b/include/hyprutils/path/Path.hpp index 8634f20..dccddef 100644 --- a/include/hyprutils/path/Path.hpp +++ b/include/hyprutils/path/Path.hpp @@ -4,39 +4,38 @@ #include #include -namespace Hyprutils { - namespace Path { - /** Check whether a config in the form basePath/hypr/programName.conf exists. +//NOLINTNEXTLINE +namespace Hyprutils::Path { + /** Check whether a config in the form basePath/hypr/programName.conf exists. @param basePath the path where the config will be searched @param programName name of the program (and config file) to search for */ - bool checkConfigExists(const std::string basePath, const std::string programName); + bool checkConfigExists(const std::string basePath, const std::string programName); - /** Constructs a full config path given the basePath and programName. + /** Constructs a full config path given the basePath and programName. @param basePath the path where the config hypr/programName.conf is located @param programName name of the program (and config file) */ - std::string fullConfigPath(const std::string basePath, const std::string programName); + std::string fullConfigPath(const std::string basePath, const std::string programName); - /** Retrieves the absolute path of the $HOME env variable. + /** Retrieves the absolute path of the $HOME env variable. */ - std::optional getHome(); + std::optional getHome(); - /** Retrieves a CVarList of paths from the $XDG_CONFIG_DIRS env variable. + /** Retrieves a CVarList of paths from the $XDG_CONFIG_DIRS env variable. */ - std::optional getXdgConfigDirs(); + std::optional getXdgConfigDirs(); - /** Retrieves the absolute path of the $XDG_CONFIG_HOME env variable. + /** Retrieves the absolute path of the $XDG_CONFIG_HOME env variable. */ - std::optional getXdgConfigHome(); + std::optional getXdgConfigHome(); - /** Searches for a config according to the XDG Base Directory specification. + /** Searches for a config according to the XDG Base Directory specification. Returns a pair of the full path to a config and the base path. Returns std::nullopt in case of a non-existent value. @param programName name of the program (and config file) */ - using T = std::optional; - std::pair findConfig(const std::string programName); - } + using T = std::optional; + std::pair findConfig(const std::string programName); } diff --git a/include/hyprutils/signal/Listener.hpp b/include/hyprutils/signal/Listener.hpp index a78ffc7..6908a95 100644 --- a/include/hyprutils/signal/Listener.hpp +++ b/include/hyprutils/signal/Listener.hpp @@ -4,41 +4,40 @@ #include #include -namespace Hyprutils { - namespace Signal { - class CSignal; +//NOLINTNEXTLINE +namespace Hyprutils::Signal { + class CSignal; - class CSignalListener { - public: - CSignalListener(std::function handler); + class CSignalListener { + public: + CSignalListener(std::function handler); - CSignalListener(CSignalListener&&) = delete; - CSignalListener(CSignalListener&) = delete; - CSignalListener(const CSignalListener&) = delete; - CSignalListener(const CSignalListener&&) = delete; + CSignalListener(CSignalListener&&) = delete; + CSignalListener(CSignalListener&) = delete; + CSignalListener(const CSignalListener&) = delete; + CSignalListener(const CSignalListener&&) = delete; - void emit(std::any data); + void emit(std::any data); - private: - std::function m_fHandler; - }; + private: + std::function m_fHandler; + }; - typedef Hyprutils::Memory::CSharedPointer CHyprSignalListener; + typedef Hyprutils::Memory::CSharedPointer CHyprSignalListener; - class CStaticSignalListener { - public: - CStaticSignalListener(std::function handler, void* owner); + class CStaticSignalListener { + public: + CStaticSignalListener(std::function handler, void* owner); - CStaticSignalListener(CStaticSignalListener&&) = delete; - CStaticSignalListener(CStaticSignalListener&) = delete; - CStaticSignalListener(const CStaticSignalListener&) = delete; - CStaticSignalListener(const CStaticSignalListener&&) = delete; + CStaticSignalListener(CStaticSignalListener&&) = delete; + CStaticSignalListener(CStaticSignalListener&) = delete; + CStaticSignalListener(const CStaticSignalListener&) = delete; + CStaticSignalListener(const CStaticSignalListener&&) = delete; - void emit(std::any data); + void emit(std::any data); - private: - void* m_pOwner = nullptr; - std::function m_fHandler; - }; - } + private: + void* m_pOwner = nullptr; + std::function m_fHandler; + }; } diff --git a/include/hyprutils/signal/Signal.hpp b/include/hyprutils/signal/Signal.hpp index f59b9df..3f51e4f 100644 --- a/include/hyprutils/signal/Signal.hpp +++ b/include/hyprutils/signal/Signal.hpp @@ -7,22 +7,21 @@ #include #include "./Listener.hpp" -namespace Hyprutils { - namespace Signal { - class CSignal { - public: - void emit(std::any data = {}); +//NOLINTNEXTLINE +namespace Hyprutils::Signal { + class CSignal { + public: + void emit(std::any data = {}); - // - [[nodiscard("Listener is unregistered when the ptr is lost")]] CHyprSignalListener registerListener(std::function handler); + // + [[nodiscard("Listener is unregistered when the ptr is lost")]] CHyprSignalListener registerListener(std::function handler); - // this is for static listeners. They die with this signal. - // TODO: can we somehow rid of the void* data and make it a custom this? - void registerStaticListener(std::function handler, void* owner); + // this is for static listeners. They die with this signal. + // TODO: can we somehow rid of the void* data and make it a custom this? + void registerStaticListener(std::function handler, void* owner); - private: - std::vector> m_vListeners; - std::vector> m_vStaticListeners; - }; - } -} \ No newline at end of file + private: + std::vector> m_vListeners; + std::vector> m_vStaticListeners; + }; +} diff --git a/include/hyprutils/string/String.hpp b/include/hyprutils/string/String.hpp index cdc5c39..2ef11de 100644 --- a/include/hyprutils/string/String.hpp +++ b/include/hyprutils/string/String.hpp @@ -1,11 +1,10 @@ #pragma once #include -namespace Hyprutils { - namespace String { - // trims beginning and end of whitespace characters - std::string trim(const std::string& in); - bool isNumber(const std::string& str, bool allowfloat = false); - void replaceInString(std::string& string, const std::string& what, const std::string& to); - }; -}; \ No newline at end of file +//NOLINTNEXTLINE +namespace Hyprutils::String { + // trims beginning and end of whitespace characters + std::string trim(const std::string& in); + bool isNumber(const std::string& str, bool allowfloat = false); + void replaceInString(std::string& string, const std::string& what, const std::string& to); +}; diff --git a/include/hyprutils/string/VarList.hpp b/include/hyprutils/string/VarList.hpp index 697b51e..903d68f 100644 --- a/include/hyprutils/string/VarList.hpp +++ b/include/hyprutils/string/VarList.hpp @@ -3,65 +3,64 @@ #include #include -namespace Hyprutils { - namespace String { - class CVarList { - public: - /** Split string into arg list +//NOLINTNEXTLINE +namespace Hyprutils::String { + class CVarList { + public: + /** Split string into arg list @param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args @param delim if delimiter is 's', use std::isspace @param removeEmpty remove empty args from argv */ - CVarList(const std::string& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false); + CVarList(const std::string& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false); - ~CVarList() = default; + ~CVarList() = default; - size_t size() const { - return m_vArgs.size(); + size_t size() const { + return m_vArgs.size(); + } + + std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const; + + void map(std::function func) { + for (auto& s : m_vArgs) + func(s); + } + + void append(const std::string arg) { + m_vArgs.emplace_back(arg); + } + + std::string operator[](const size_t& idx) const { + if (idx >= m_vArgs.size()) + return ""; + return m_vArgs[idx]; + } + + // for range-based loops + std::vector::iterator begin() { + return m_vArgs.begin(); + } + std::vector::const_iterator begin() const { + return m_vArgs.begin(); + } + std::vector::iterator end() { + return m_vArgs.end(); + } + std::vector::const_iterator end() const { + return m_vArgs.end(); + } + + bool contains(const std::string& el) { + for (auto& a : m_vArgs) { + if (a == el) + return true; } - std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const; + return false; + } - void map(std::function func) { - for (auto& s : m_vArgs) - func(s); - } - - void append(const std::string arg) { - m_vArgs.emplace_back(arg); - } - - std::string operator[](const size_t& idx) const { - if (idx >= m_vArgs.size()) - return ""; - return m_vArgs[idx]; - } - - // for range-based loops - std::vector::iterator begin() { - return m_vArgs.begin(); - } - std::vector::const_iterator begin() const { - return m_vArgs.begin(); - } - std::vector::iterator end() { - return m_vArgs.end(); - } - std::vector::const_iterator end() const { - return m_vArgs.end(); - } - - bool contains(const std::string& el) { - for (auto& a : m_vArgs) { - if (a == el) - return true; - } - - return false; - } - - private: - std::vector m_vArgs; - }; - } + private: + std::vector m_vArgs; + }; } diff --git a/include/hyprutils/utils/ScopeGuard.hpp b/include/hyprutils/utils/ScopeGuard.hpp index af4feb7..d2776d4 100644 --- a/include/hyprutils/utils/ScopeGuard.hpp +++ b/include/hyprutils/utils/ScopeGuard.hpp @@ -2,16 +2,15 @@ #include -namespace Hyprutils { - namespace Utils { - // calls a function when it goes out of scope - class CScopeGuard { - public: - CScopeGuard(const std::function& fn_); - ~CScopeGuard(); +//NOLINTNEXTLINE +namespace Hyprutils::Utils { + // calls a function when it goes out of scope + class CScopeGuard { + public: + CScopeGuard(const std::function& fn_); + ~CScopeGuard(); - private: - std::function fn; - }; + private: + std::function fn; }; }; diff --git a/src/animation/AnimatedVariable.cpp b/src/animation/AnimatedVariable.cpp index 69c66af..dd648a9 100644 --- a/src/animation/AnimatedVariable.cpp +++ b/src/animation/AnimatedVariable.cpp @@ -13,8 +13,8 @@ void CBaseAnimatedVariable::create(CAnimationManager* pManager, int typeInfo, SP m_pSelf = pSelf; m_pAnimationManager = pManager; - m_pSignals = pManager->getSignals(); - m_bDummy = false; + m_pSignals = pManager->getSignals(); + m_bDummy = false; } void CBaseAnimatedVariable::connectToActive() { diff --git a/src/animation/AnimationManager.cpp b/src/animation/AnimationManager.cpp index 0089e35..0a47fe4 100644 --- a/src/animation/AnimationManager.cpp +++ b/src/animation/AnimationManager.cpp @@ -99,7 +99,7 @@ bool CAnimationManager::bezierExists(const std::string& bezier) { } SP CAnimationManager::getBezier(const std::string& name) { - const auto BEZIER = std::find_if(m_mBezierCurves.begin(), m_mBezierCurves.end(), [&](const auto& other) { return other.first == name; }); + const auto BEZIER = std::ranges::find_if(m_mBezierCurves, [&](const auto& other) { return other.first == name; }); return BEZIER == m_mBezierCurves.end() ? m_mBezierCurves["default"] : BEZIER->second; } diff --git a/src/animation/BezierCurve.cpp b/src/animation/BezierCurve.cpp index ac37a01..850cf3a 100644 --- a/src/animation/BezierCurve.cpp +++ b/src/animation/BezierCurve.cpp @@ -35,14 +35,14 @@ float CBezierCurve::getXForT(float const& t) const { float t2 = t * t; float t3 = t2 * t; - return 3 * t * (1 - t) * (1 - t) * m_vPoints[1].x + 3 * t2 * (1 - t) * m_vPoints[2].x + t3 * m_vPoints[3].x; + return (3 * t * (1 - t) * (1 - t) * m_vPoints[1].x) + (3 * t2 * (1 - t) * m_vPoints[2].x) + (t3 * m_vPoints[3].x); } float CBezierCurve::getYForT(float const& t) const { float t2 = t * t; float t3 = t2 * t; - return 3 * t * (1 - t) * (1 - t) * m_vPoints[1].y + 3 * t2 * (1 - t) * m_vPoints[2].y + t3 * m_vPoints[3].y; + return (3 * t * (1 - t) * (1 - t) * m_vPoints[1].y) + (3 * t2 * (1 - t) * m_vPoints[2].y) + (t3 * m_vPoints[3].y); } // Todo: this probably can be done better and faster @@ -74,5 +74,5 @@ float CBezierCurve::getYForPoint(float const& x) const { if (std::isnan(PERCINDELTA) || std::isinf(PERCINDELTA)) // can sometimes happen for VERY small x return 0.f; - return LOWERPOINT->y + (UPPERPOINT->y - LOWERPOINT->y) * PERCINDELTA; + return LOWERPOINT->y + ((UPPERPOINT->y - LOWERPOINT->y) * PERCINDELTA); } diff --git a/src/math/Box.cpp b/src/math/Box.cpp index 3cf503c..16fadfb 100644 --- a/src/math/Box.cpp +++ b/src/math/Box.cpp @@ -37,7 +37,7 @@ CBox& Hyprutils::Math::CBox::translate(const Vector2D& vec) { } Vector2D Hyprutils::Math::CBox::middle() const { - return Vector2D{x + w * HALF, y + h * HALF}; + return Vector2D{x + (w * HALF), y + (h * HALF)}; } bool Hyprutils::Math::CBox::containsPoint(const Vector2D& vec) const { @@ -233,5 +233,5 @@ Vector2D Hyprutils::Math::CBox::closestPoint(const Vector2D& vec) const { } SBoxExtents Hyprutils::Math::CBox::extentsFrom(const CBox& small) { - return {{small.x - x, small.y - y}, {w - small.w - (small.x - x), h - small.h - (small.y - y)}}; + return {.topLeft = {small.x - x, small.y - y}, .bottomRight = {w - small.w - (small.x - x), h - small.h - (small.y - y)}}; } diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index 58e2167..2417cba 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -93,6 +93,7 @@ Mat3x3& Mat3x3::transform(eTransform transform) { } Mat3x3& Mat3x3::rotate(float rot) { + //no lint or maybe do something? multiply(std::array{(float)cos(rot), (float)-sin(rot), 0.0f, (float)sin(rot), (float)cos(rot), 0.0f, 0.0f, 0.0f, 1.0f}); return *this; } diff --git a/src/math/Region.cpp b/src/math/Region.cpp index 0a74f87..f00391d 100644 --- a/src/math/Region.cpp +++ b/src/math/Region.cpp @@ -26,12 +26,13 @@ Hyprutils::Math::CRegion::CRegion(pixman_box32_t* box) { pixman_region32_init_rect(&m_rRegion, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1); } +// What to do with const_cast? Hyprutils::Math::CRegion::CRegion(const CRegion& other) { pixman_region32_init(&m_rRegion); pixman_region32_copy(&m_rRegion, const_cast(&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()); } @@ -86,7 +87,7 @@ CRegion& Hyprutils::Math::CRegion::invert(pixman_box32_t* box) { } CRegion& Hyprutils::Math::CRegion::invert(const CBox& box) { - pixman_box32 pixmanBox = {(int32_t)box.x, (int32_t)box.y, (int32_t)box.w + (int32_t)box.x, (int32_t)box.h + (int32_t)box.y}; + 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}; return this->invert(&pixmanBox); } @@ -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{(double)r.x1 - units, (double)r.y1 - units, (double)r.x2 - r.x1 + (units * 2), (double)r.y2 - r.y1 + (units * 2)}; add(b); } diff --git a/src/math/Vector2D.cpp b/src/math/Vector2D.cpp index a7f709a..19ef243 100644 --- a/src/math/Vector2D.cpp +++ b/src/math/Vector2D.cpp @@ -4,23 +4,18 @@ using namespace Hyprutils::Math; -Hyprutils::Math::Vector2D::Vector2D(double xx, double yy) { - x = xx; - y = yy; +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((double)xx), y((double)yy) { + ; } Hyprutils::Math::Vector2D::Vector2D() { - x = 0; - y = 0; + ; } -Hyprutils::Math::Vector2D::~Vector2D() {} - double Hyprutils::Math::Vector2D::normalize() { // get max abs const auto max = std::abs(x) > std::abs(y) ? std::abs(x) : std::abs(y); @@ -48,11 +43,11 @@ double Hyprutils::Math::Vector2D::distance(const Vector2D& other) const { } double Hyprutils::Math::Vector2D::distanceSq(const Vector2D& other) const { - return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y); + return ((x - other.x) * (x - other.x)) + ((y - other.y) * (y - other.y)); } double Hyprutils::Math::Vector2D::size() const { - return std::sqrt(x * x + y * y); + return std::sqrt((x * x) + (y * y)); } Vector2D Hyprutils::Math::Vector2D::getComponentMax(const Vector2D& other) const { diff --git a/src/os/FileDescriptor.cpp b/src/os/FileDescriptor.cpp index 8592f42..19226d4 100644 --- a/src/os/FileDescriptor.cpp +++ b/src/os/FileDescriptor.cpp @@ -9,9 +9,9 @@ using namespace Hyprutils::OS; CFileDescriptor::CFileDescriptor(int const fd) : m_fd(fd) {} -CFileDescriptor::CFileDescriptor(CFileDescriptor&& other) : m_fd(std::exchange(other.m_fd, -1)) {} +CFileDescriptor::CFileDescriptor(CFileDescriptor&& other) noexcept : m_fd(std::exchange(other.m_fd, -1)) {} -CFileDescriptor& CFileDescriptor::operator=(CFileDescriptor&& other) { +CFileDescriptor& CFileDescriptor::operator=(CFileDescriptor&& other) noexcept { if (this == &other) // Shit will go haywire if there is duplicate ownership abort(); @@ -37,10 +37,7 @@ int CFileDescriptor::getFlags() const { } bool CFileDescriptor::setFlags(int flags) { - if (fcntl(m_fd, F_SETFD, flags) == -1) - return false; - - return true; + return fcntl(m_fd, F_SETFD, flags) != -1; } int CFileDescriptor::take() { diff --git a/src/os/Process.cpp b/src/os/Process.cpp index 7eebbba..0fe6755 100644 --- a/src/os/Process.cpp +++ b/src/os/Process.cpp @@ -6,7 +6,6 @@ using namespace Hyprutils::OS; #include #include #include -#include #include #include @@ -89,7 +88,7 @@ bool Hyprutils::OS::CProcess::runSync() { {.fd = outPipe[0], .events = POLLIN, .revents = 0}, {.fd = errPipe[0], .events = POLLIN, .revents = 0}, }; - + // Maybe bool or just leave it as it is? while (1337) { int ret = poll(pollfds, 2, 5000); @@ -171,7 +170,7 @@ bool Hyprutils::OS::CProcess::runAsync() { // run in child sigset_t set; sigemptyset(&set); - sigprocmask(SIG_SETMASK, &set, NULL); + sigprocmask(SIG_SETMASK, &set, nullptr); grandchild = fork(); if (grandchild == 0) { @@ -225,6 +224,6 @@ const std::string& Hyprutils::OS::CProcess::stdErr() { return err; } -const pid_t Hyprutils::OS::CProcess::pid() { +pid_t Hyprutils::OS::CProcess::pid() { return grandchildPid; } diff --git a/src/path/Path.cpp b/src/path/Path.cpp index b6f9997..47b0bcb 100644 --- a/src/path/Path.cpp +++ b/src/path/Path.cpp @@ -6,6 +6,7 @@ using namespace Hyprutils; namespace Hyprutils::Path { std::string fullConfigPath(std::string basePath, std::string programName) { + //no lint or...? return basePath + "/hypr/" + programName + ".conf"; } @@ -62,7 +63,7 @@ namespace Hyprutils::Path { static const auto xdgConfigDirs = getXdgConfigDirs(); if (xdgConfigDirs.has_value()) { - for (auto dir : xdgConfigDirs.value()) { + for (auto& dir : xdgConfigDirs.value()) { if (checkConfigExists(dir, programName)) return std::make_pair(fullConfigPath(dir, programName), std::nullopt); } diff --git a/src/signal/Signal.cpp b/src/signal/Signal.cpp index e4771f9..b3cb852 100644 --- a/src/signal/Signal.cpp +++ b/src/signal/Signal.cpp @@ -1,6 +1,5 @@ #include #include -#include using namespace Hyprutils::Signal; using namespace Hyprutils::Memory; @@ -18,6 +17,7 @@ void Hyprutils::Signal::CSignal::emit(std::any data) { } std::vector statics; + statics.reserve(m_vStaticListeners.size()); for (auto& l : m_vStaticListeners) { statics.emplace_back(l.get()); } @@ -54,4 +54,4 @@ CHyprSignalListener Hyprutils::Signal::CSignal::registerListener(std::function handler, void* owner) { m_vStaticListeners.emplace_back(std::make_unique(handler, owner)); -} \ No newline at end of file +} diff --git a/src/string/String.cpp b/src/string/String.cpp index 2ff2dac..f27293f 100644 --- a/src/string/String.cpp +++ b/src/string/String.cpp @@ -6,13 +6,13 @@ using namespace Hyprutils::String; std::string Hyprutils::String::trim(const std::string& in) { if (in.empty()) return in; - - int countBefore = 0; + //Wsign-compare + size_t countBefore = 0; while (countBefore < in.length() && std::isspace(in.at(countBefore))) { countBefore++; } - - int countAfter = 0; + //Wsign-compare + size_t countAfter = 0; while (countAfter < in.length() - countBefore && std::isspace(in.at(in.length() - countAfter - 1))) { countAfter++; } @@ -55,10 +55,7 @@ bool Hyprutils::String::isNumber(const std::string& str, bool allowfloat) { } } - if (!isdigit(str.back())) - return false; - - return true; + return isdigit(str.back()) != 0; } void Hyprutils::String::replaceInString(std::string& string, const std::string& what, const std::string& to) { diff --git a/src/string/VarList.cpp b/src/string/VarList.cpp index 1e2743a..0fe44f2 100644 --- a/src/string/VarList.cpp +++ b/src/string/VarList.cpp @@ -22,7 +22,8 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr break; } pos += s.size() + 1; - m_vArgs.emplace_back(trim(std::string_view{s}.data())); + // Is that correct? + m_vArgs.emplace_back(trim(s.data())); } } @@ -35,4 +36,4 @@ std::string Hyprutils::String::CVarList::join(const std::string& joiner, size_t } return rolling; -} \ No newline at end of file +} diff --git a/tests/animation.cpp b/tests/animation.cpp index 971afac..644bf55 100644 --- a/tests/animation.cpp +++ b/tests/animation.cpp @@ -4,6 +4,7 @@ #include #include #include "shared.hpp" +#include #define SP CSharedPointer #define WP CWeakPointer @@ -24,7 +25,7 @@ using PANIMVAR = SP>; template using PANIMVARREF = WP>; -enum eAVTypes { +enum eAVTypes : uint8_t { INT = 1, TEST, }; @@ -34,12 +35,13 @@ struct SomeTestType { bool operator==(const SomeTestType& other) const { return done == other.done; } + // Trivial copy assignment? SomeTestType& operator=(const SomeTestType& other) { done = other.done; return *this; } }; - +// Do this static? CAnimationConfigTree animationTree; class CMyAnimationManager : public CAnimationManager { @@ -106,7 +108,7 @@ class CMyAnimationManager : public CAnimationManager { ; } }; - +// Same UP pAnimationManager; class Subject { @@ -120,7 +122,7 @@ class Subject { PANIMVAR m_iB; PANIMVAR m_iC; }; - +// Same int config() { pAnimationManager = makeUnique(); @@ -299,7 +301,7 @@ int main(int argc, char** argv, char** envp) { if (v.lock() != vars.back()) vars.back()->warp(); }); - s.m_iA->setCallbackOnEnd([&s, &vars](auto) { + s.m_iA->setCallbackOnEnd([&vars](auto) { vars.resize(vars.size() + 1); pAnimationManager->createAnimation(1, vars.back(), "default"); *vars.back() = 1337; @@ -313,7 +315,7 @@ int main(int argc, char** argv, char** envp) { EXPECT(s.m_iA->value(), 1000000); // all vars should be set to 1337 - EXPECT(std::find_if(vars.begin(), vars.end(), [](const auto& v) { return v->value() != 1337; }) == vars.end(), true); + EXPECT(std::ranges::find_if(vars, [](const auto& v) { return v->value() != 1337; }) == vars.end(), true); // test one-time callbacks s.m_iA->resetAllCallbacks();