clang-tidy/clang fixes and format

This commit is contained in:
Honkazel 2025-02-02 18:48:52 +05:00
parent 6a8bc9d2a4
commit 0ccb43f5cd
37 changed files with 1191 additions and 1210 deletions

View file

@ -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")

View file

@ -9,218 +9,216 @@
#include <functional>
#include <chrono>
namespace Hyprutils {
namespace Animation {
//NOLINTNEXTLINE
namespace Hyprutils::Animation {
/* A base class for animated variables. */
class CBaseAnimatedVariable {
public:
using CallbackFun = std::function<void(Memory::CWeakPointer<CBaseAnimatedVariable> thisptr)>;
/* A base class for animated variables. */
class CBaseAnimatedVariable {
public:
using CallbackFun = std::function<void(Memory::CWeakPointer<CBaseAnimatedVariable> thisptr)>;
CBaseAnimatedVariable() {
; // m_bDummy = true;
};
CBaseAnimatedVariable() {
; // m_bDummy = true;
};
void create(CAnimationManager*, int, Memory::CSharedPointer<CBaseAnimatedVariable>);
void connectToActive();
void disconnectFromActive();
void create(CAnimationManager*, int, Memory::CSharedPointer<CBaseAnimatedVariable>);
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<SAnimationPropertyConfig> pConfig) {
m_pConfig = pConfig;
}
//
void setConfig(Memory::CSharedPointer<SAnimationPropertyConfig> pConfig) {
m_pConfig = pConfig;
}
Memory::CWeakPointer<SAnimationPropertyConfig> getConfig() const {
return m_pConfig;
}
Memory::CWeakPointer<SAnimationPropertyConfig> 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<CBaseAnimatedVariable> m_pSelf;
Memory::CWeakPointer<CBaseAnimatedVariable> m_pSelf;
Memory::CWeakPointer<CAnimationManager::SAnimationManagerSignals> m_pSignals;
Memory::CWeakPointer<CAnimationManager::SAnimationManagerSignals> m_pSignals;
private:
Memory::CWeakPointer<SAnimationPropertyConfig> m_pConfig;
private:
Memory::CWeakPointer<SAnimationPropertyConfig> 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 <class ValueImpl>
concept AnimatedType = requires(ValueImpl val) {
requires std::is_copy_constructible_v<ValueImpl>;
{ val == val } -> std::same_as<bool>; // requires operator==
{ val = val }; // requires operator=
};
/* This concept represents the minimum requirement for a type to be used with CGenericAnimatedVariable */
template <class ValueImpl>
concept AnimatedType = requires(ValueImpl val) {
requires std::is_copy_constructible_v<ValueImpl>;
{ val == val } -> std::same_as<bool>; // 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 <AnimatedType VarType, class AnimationContext>
class CGenericAnimatedVariable : public CBaseAnimatedVariable {
public:
CGenericAnimatedVariable() = default;
template <AnimatedType VarType, class AnimationContext>
class CGenericAnimatedVariable : public CBaseAnimatedVariable {
public:
CGenericAnimatedVariable() = default;
void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf,
const VarType& initialValue) {
m_Begun = initialValue;
m_Value = initialValue;
m_Goal = initialValue;
void create(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CSharedPointer<CGenericAnimatedVariable<VarType, AnimationContext>> 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{};
};
}

View file

@ -6,52 +6,51 @@
#include <string>
#include <unordered_map>
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<SAnimationPropertyConfig> pValues;
Memory::CWeakPointer<SAnimationPropertyConfig> pParentAnimation;
};
Memory::CWeakPointer<SAnimationPropertyConfig> pValues;
Memory::CWeakPointer<SAnimationPropertyConfig> 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<SAnimationPropertyConfig> getConfig(const std::string& name) const;
const std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>>& getFullConfig() const;
Memory::CSharedPointer<SAnimationPropertyConfig> getConfig(const std::string& name) const;
const std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>>& 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<SAnimationPropertyConfig> PANIM);
std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>> m_mAnimationConfig;
};
}
private:
void setAnimForChildren(Memory::CSharedPointer<SAnimationPropertyConfig> PANIM);
std::unordered_map<std::string, Memory::CSharedPointer<SAnimationPropertyConfig>> m_mAnimationConfig;
};
}

View file

@ -9,55 +9,54 @@
#include <unordered_map>
#include <vector>
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<CBezierCurve> getBezier(const std::string&);
bool bezierExists(const std::string&);
Memory::CSharedPointer<CBezierCurve> getBezier(const std::string&);
const std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>>& getAllBeziers();
const std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>>& getAllBeziers();
struct SAnimationManagerSignals {
Signal::CSignal connect; // WP<CBaseAnimatedVariable>
Signal::CSignal disconnect; // WP<CBaseAnimatedVariable>
};
Memory::CWeakPointer<SAnimationManagerSignals> getSignals() const;
std::vector<Memory::CWeakPointer<CBaseAnimatedVariable>> m_vActiveAnimatedVariables;
private:
std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>> 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<SAnimVarListeners> m_listeners;
Memory::CUniquePointer<SAnimationManagerSignals> m_events;
struct SAnimationManagerSignals {
Signal::CSignal connect; // WP<CBaseAnimatedVariable>
Signal::CSignal disconnect; // WP<CBaseAnimatedVariable>
};
}
Memory::CWeakPointer<SAnimationManagerSignals> getSignals() const;
std::vector<Memory::CWeakPointer<CBaseAnimatedVariable>> m_vActiveAnimatedVariables;
private:
std::unordered_map<std::string, Memory::CSharedPointer<CBezierCurve>> 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<SAnimVarListeners> m_listeners;
Memory::CUniquePointer<SAnimationManagerSignals> m_events;
};
}

View file

@ -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<Hyprutils::Math::Vector2D, 2>& 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<Hyprutils::Math::Vector2D, 2>& 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<Hyprutils::Math::Vector2D> m_vPoints;
private:
/* this INCLUDES the 0,0 and 1,1 points. */
std::vector<Hyprutils::Math::Vector2D> m_vPoints;
std::array<Hyprutils::Math::Vector2D, BAKEDPOINTS> m_aPointsBaked;
};
}
std::array<Hyprutils::Math::Vector2D, BAKEDPOINTS> m_aPointsBaked;
};
}

View file

@ -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;
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
//NOLINTNEXTLINE
namespace Hyprutils::Math {
/**

View file

@ -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<float, 9>);
Mat3x3(std::vector<float>);
//NOLINTNEXTLINE
class Mat3x3 {
public:
Mat3x3();
Mat3x3(std::array<float, 9>);
Mat3x3(std::vector<float>);
/* 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<float, 9> getMatrix() const;
/* get the matrix as an array, in a row-major order. */
std::array<float, 9> 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<float, 9> matrix;
};
}
}
private:
std::array<float, 9> matrix;
};
}

View file

@ -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,
};
}
}
//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,
};
}

View file

@ -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<pixman_box32_t> getRects() const;
std::vector<pixman_box32_t> 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;
};
}

View file

@ -3,102 +3,102 @@
#include <format>
#include <string>
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

View file

@ -2,118 +2,116 @@
#include <memory>
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 <typename T>
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<T> __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 <typename T>
//NOLINTNEXTLINE
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) 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<T> _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();
}
};
}

View file

@ -13,178 +13,174 @@
or deref an existing one inside the destructor.
*/
namespace Hyprutils {
namespace Memory {
namespace Hyprutils::Memory {
template <typename T>
class CSharedPointer {
public:
template <typename X>
using validHierarchy = std::enable_if_t<std::is_assignable_v<CSharedPointer<T>&, X>, CSharedPointer&>;
template <typename X>
using isConstructible = std::enable_if_t<std::is_constructible_v<T&, X&>>;
template <typename T>
class CSharedPointer {
public:
template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<CSharedPointer<T>&, X>::value, CSharedPointer&>::type;
template <typename X>
using isConstructible = typename std::enable_if<std::is_constructible<T&, X&>::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<T>(object);
increment();
}
explicit CSharedPointer(T* object) noexcept : impl_(new Impl_::impl<T>(object)) {
increment();
}
/* creates a shared pointer from a reference */
template <typename U, typename = isConstructible<U>>
CSharedPointer(const CSharedPointer<U>& ref) noexcept {
impl_ = ref.impl_;
increment();
}
/* creates a shared pointer from a reference */
template <typename U, typename = isConstructible<U>>
CSharedPointer(const CSharedPointer<U>& 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 <typename U, typename = isConstructible<U>>
CSharedPointer(CSharedPointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_);
}
template <typename U, typename = isConstructible<U>>
// reason - ref param not moved. But is that correct?
CSharedPointer(CSharedPointer<U>& 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 <typename U>
validHierarchy<const CSharedPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if (impl_ == rhs.impl_)
return *this;
decrement();
impl_ = rhs.impl_;
increment();
template <typename U>
// Same. And also what should i do with the warning of unconventional assign operators?
validHierarchy<const CSharedPointer<U>&> operator=(const CSharedPointer<U>& 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 <typename U>
validHierarchy<const CSharedPointer<U>&> operator=(CSharedPointer<U>&& 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 <typename U>
// Same
validHierarchy<const CSharedPointer<U>&> operator=(CSharedPointer<U>& 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<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
bool operator==(const CSharedPointer& rhs) const {
return impl_ == rhs.impl_;
}
bool operator<(const CSharedPointer& rhs) const {
return reinterpret_cast<uintptr_t>(impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
bool operator()(const CSharedPointer& lhs, const CSharedPointer& rhs) const {
return reinterpret_cast<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
T* operator->() const {
return get();
}
bool operator<(const CSharedPointer& rhs) const {
return reinterpret_cast<uintptr_t>(impl_) < reinterpret_cast<uintptr_t>(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<T*>(impl_->getData()) : nullptr;
}
void reset() {
decrement();
impl_ = nullptr;
}
unsigned int strongRef() const {
return impl_ ? impl_->ref() : 0;
}
T* get() const {
return impl_ ? static_cast<T*>(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 <typename U, typename... Args>
static CSharedPointer<U> makeShared(Args&&... args) {
return CSharedPointer<U>(new U(std::forward<Args>(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 <typename U, typename... Args>
static CSharedPointer<U> makeShared(Args&&... args) {
return CSharedPointer<U>(new U(std::forward<Args>(args)...));
}
}
@ -193,4 +189,4 @@ struct std::hash<Hyprutils::Memory::CSharedPointer<T>> {
std::size_t operator()(const Hyprutils::Memory::CSharedPointer<T>& p) const noexcept {
return std::hash<void*>{}(p.impl_);
}
};
};

View file

@ -9,135 +9,134 @@
to be locked.
*/
namespace Hyprutils {
namespace Memory {
template <typename T>
class CUniquePointer {
public:
template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<CUniquePointer<T>&, X>::value, CUniquePointer&>::type;
template <typename X>
using isConstructible = typename std::enable_if<std::is_constructible<T&, X&>::value>::type;
namespace Hyprutils::Memory {
template <typename T>
class CUniquePointer {
public:
template <typename X>
using validHierarchy = std::enable_if_t<std::is_assignable_v<CUniquePointer<T>&, X>, CUniquePointer&>;
template <typename X>
using isConstructible = std::enable_if_t<std::is_constructible_v<T&, X&>>;
/* 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<T>(object, false);
increment();
}
explicit CUniquePointer(T* object) noexcept : impl_(new Impl_::impl<T>(object, false)) {
increment();
}
/* creates a shared pointer from a reference */
template <typename U, typename = isConstructible<U>>
CUniquePointer(const CUniquePointer<U>& ref) = delete;
CUniquePointer(const CUniquePointer& ref) = delete;
/* creates a shared pointer from a reference */
template <typename U, typename = isConstructible<U>>
CUniquePointer(const CUniquePointer<U>& ref) = delete;
CUniquePointer(const CUniquePointer& ref) = delete;
template <typename U, typename = isConstructible<U>>
CUniquePointer(CUniquePointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_);
}
template <typename U, typename = isConstructible<U>>
// Same
CUniquePointer(CUniquePointer<U>& 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 <typename U>
validHierarchy<const CUniquePointer<U>&> operator=(const CUniquePointer<U>& rhs) = delete;
CUniquePointer& operator=(const CUniquePointer& rhs) = delete;
template <typename U>
validHierarchy<const CUniquePointer<U>&> operator=(const CUniquePointer<U>& rhs) = delete;
CUniquePointer& operator=(const CUniquePointer& rhs) = delete;
template <typename U>
validHierarchy<const CUniquePointer<U>&> operator=(CUniquePointer<U>&& rhs) {
std::swap(impl_, rhs.impl_);
return *this;
}
template <typename U>
// Same with both
validHierarchy<const CUniquePointer<U>&> operator=(CUniquePointer<U>& 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<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
bool operator()(const CUniquePointer& lhs, const CUniquePointer& rhs) const {
return reinterpret_cast<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(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<T*>(impl_->getData()) : nullptr;
}
T* get() const {
return impl_ ? static_cast<T*>(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 <typename U, typename... Args>
static CUniquePointer<U> makeUnique(Args&&... args) {
return CUniquePointer<U>(new U(std::forward<Args>(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 <typename U, typename... Args>
static CUniquePointer<U> makeUnique(Args&&... args) {
return CUniquePointer<U>(new U(std::forward<Args>(args)...));
}
}
@ -146,4 +145,4 @@ struct std::hash<Hyprutils::Memory::CUniquePointer<T>> {
std::size_t operator()(const Hyprutils::Memory::CUniquePointer<T>& p) const noexcept {
return std::hash<void*>{}(p.impl_);
}
};
};

View file

@ -9,206 +9,208 @@
See SharedPtr.hpp for more info on how it's different.
*/
namespace Hyprutils {
namespace Memory {
template <typename T>
class CWeakPointer {
public:
template <typename X>
using validHierarchy = typename std::enable_if<std::is_assignable<CWeakPointer<T>&, X>::value, CWeakPointer&>::type;
template <typename X>
using isConstructible = typename std::enable_if<std::is_constructible<T&, X&>::value>::type;
//NOLINTNEXTLINE
namespace Hyprutils::Memory {
template <typename T>
class CWeakPointer {
public:
template <typename X>
using validHierarchy = std::enable_if_t<std::is_assignable_v<CWeakPointer<T>&, X>, CWeakPointer&>;
template <typename X>
using isConstructible = std::enable_if_t<std::is_constructible_v<T&, X&>>;
/* create a weak ptr from a reference */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CSharedPointer<U>& ref) noexcept {
if (!ref.impl_)
return;
/* create a weak ptr from a reference */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CSharedPointer<U>& ref) noexcept {
if (!ref.impl_)
return;
impl_ = ref.impl_;
incrementWeak();
}
impl_ = ref.impl_;
incrementWeak();
}
/* create a weak ptr from a reference */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CUniquePointer<U>& ref) noexcept {
if (!ref.impl_)
return;
/* create a weak ptr from a reference */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CUniquePointer<U>& ref) noexcept {
if (!ref.impl_)
return;
impl_ = ref.impl_;
incrementWeak();
}
impl_ = ref.impl_;
incrementWeak();
}
/* create a weak ptr from another weak ptr */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CWeakPointer<U>& ref) noexcept {
if (!ref.impl_)
return;
/* create a weak ptr from another weak ptr */
template <typename U, typename = isConstructible<U>>
CWeakPointer(const CWeakPointer<U>& 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 <typename U, typename = isConstructible<U>>
CWeakPointer(CWeakPointer<U>&& ref) noexcept {
std::swap(impl_, ref.impl_);
}
template <typename U, typename = isConstructible<U>>
// Same
CWeakPointer(CWeakPointer<U>& 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 <typename U>
validHierarchy<const CWeakPointer<U>&> operator=(const CWeakPointer<U>& rhs) {
if (impl_ == rhs.impl_)
return *this;
decrementWeak();
impl_ = rhs.impl_;
incrementWeak();
/* create a weak ptr from another weak ptr with assignment */
template <typename U>
// Same
validHierarchy<const CWeakPointer<U>&> operator=(const CWeakPointer<U>& rhs) {
if (impl_ == rhs.impl_)
return *this;
}
CWeakPointer<T>& operator=(const CWeakPointer& rhs) {
if (impl_ == rhs.impl_)
return *this;
decrementWeak();
impl_ = rhs.impl_;
incrementWeak();
decrementWeak();
impl_ = rhs.impl_;
incrementWeak();
return *this;
}
// Same
CWeakPointer<T>& operator=(const CWeakPointer& rhs) {
if (impl_ == rhs.impl_)
return *this;
}
/* create a weak ptr from a shared ptr with assignment */
template <typename U>
validHierarchy<const CWeakPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if (reinterpret_cast<uintptr_t>(impl_) == reinterpret_cast<uintptr_t>(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 <typename U>
// Same...
validHierarchy<const CWeakPointer<U>&> operator=(const CSharedPointer<U>& rhs) {
if (reinterpret_cast<uintptr_t>(impl_) == reinterpret_cast<uintptr_t>(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<T> lock() const {
if (!impl_ || !impl_->dataNonNull() || impl_->destroying() || !impl_->lockable())
return {};
return CSharedPointer<T>(impl_);
}
/* this returns valid() */
operator bool() const {
return valid();
}
bool operator==(const CWeakPointer<T>& rhs) const {
return impl_ == rhs.impl_;
}
bool operator==(const CSharedPointer<T>& rhs) const {
return impl_ == rhs.impl_;
}
bool operator==(const CUniquePointer<T>& 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<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
bool operator<(const CWeakPointer& rhs) const {
return reinterpret_cast<uintptr_t>(impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
T* get() const {
return impl_ ? static_cast<T*>(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<T> lock() const {
if (!impl_ || !impl_->dataNonNull() || impl_->destroying() || !impl_->lockable())
return {};
return CSharedPointer<T>(impl_);
}
/* this returns valid() */
operator bool() const {
return valid();
}
bool operator==(const CWeakPointer<T>& rhs) const {
return impl_ == rhs.impl_;
}
bool operator==(const CSharedPointer<T>& rhs) const {
return impl_ == rhs.impl_;
}
bool operator==(const CUniquePointer<T>& 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<uintptr_t>(lhs.impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
bool operator<(const CWeakPointer& rhs) const {
return reinterpret_cast<uintptr_t>(impl_) < reinterpret_cast<uintptr_t>(rhs.impl_);
}
T* get() const {
return impl_ ? static_cast<T*>(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 <typename T>

View file

@ -1,39 +1,39 @@
#pragma once
#include <fcntl.h>
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;
};
};

View file

@ -5,33 +5,32 @@
#include <utility>
#include <sys/types.h>
namespace Hyprutils {
namespace OS {
class CProcess {
public:
/* Creates a process object, doesn't run yet */
CProcess(const std::string& binary_, const std::vector<std::string>& args_);
//NOLINTNEXTLINE
namespace Hyprutils::OS {
class CProcess {
public:
/* Creates a process object, doesn't run yet */
CProcess(const std::string& binary_, const std::vector<std::string>& 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<std::string> args;
std::vector<std::pair<std::string, std::string>> env;
pid_t grandchildPid = 0;
};
}
}
private:
std::string binary, out, err;
std::vector<std::string> args;
std::vector<std::pair<std::string, std::string>> env;
pid_t grandchildPid = 0;
};
}

View file

@ -4,39 +4,38 @@
#include <optional>
#include <utility>
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<std::string> getHome();
std::optional<std::string> 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<String::CVarList> getXdgConfigDirs();
std::optional<String::CVarList> 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<std::string> getXdgConfigHome();
std::optional<std::string> 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::string>;
std::pair<T, T> findConfig(const std::string programName);
}
using T = std::optional<std::string>;
std::pair<T, T> findConfig(const std::string programName);
}

View file

@ -4,41 +4,40 @@
#include <functional>
#include <hyprutils/memory/SharedPtr.hpp>
namespace Hyprutils {
namespace Signal {
class CSignal;
//NOLINTNEXTLINE
namespace Hyprutils::Signal {
class CSignal;
class CSignalListener {
public:
CSignalListener(std::function<void(std::any)> handler);
class CSignalListener {
public:
CSignalListener(std::function<void(std::any)> 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<void(std::any)> m_fHandler;
};
private:
std::function<void(std::any)> m_fHandler;
};
typedef Hyprutils::Memory::CSharedPointer<CSignalListener> CHyprSignalListener;
typedef Hyprutils::Memory::CSharedPointer<CSignalListener> CHyprSignalListener;
class CStaticSignalListener {
public:
CStaticSignalListener(std::function<void(void*, std::any)> handler, void* owner);
class CStaticSignalListener {
public:
CStaticSignalListener(std::function<void(void*, std::any)> 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<void(void*, std::any)> m_fHandler;
};
}
private:
void* m_pOwner = nullptr;
std::function<void(void*, std::any)> m_fHandler;
};
}

View file

@ -7,22 +7,21 @@
#include <hyprutils/memory/WeakPtr.hpp>
#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<void(std::any)> handler);
//
[[nodiscard("Listener is unregistered when the ptr is lost")]] CHyprSignalListener registerListener(std::function<void(std::any)> 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<void(void*, std::any)> 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<void(void*, std::any)> handler, void* owner);
private:
std::vector<Hyprutils::Memory::CWeakPointer<CSignalListener>> m_vListeners;
std::vector<std::unique_ptr<CStaticSignalListener>> m_vStaticListeners;
};
}
}
private:
std::vector<Hyprutils::Memory::CWeakPointer<CSignalListener>> m_vListeners;
std::vector<std::unique_ptr<CStaticSignalListener>> m_vStaticListeners;
};
}

View file

@ -1,11 +1,10 @@
#pragma once
#include <string>
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);
};
};
//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);
};

View file

@ -3,65 +3,64 @@
#include <vector>
#include <string>
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<void(std::string&)> 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<std::string>::iterator begin() {
return m_vArgs.begin();
}
std::vector<std::string>::const_iterator begin() const {
return m_vArgs.begin();
}
std::vector<std::string>::iterator end() {
return m_vArgs.end();
}
std::vector<std::string>::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<void(std::string&)> 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<std::string>::iterator begin() {
return m_vArgs.begin();
}
std::vector<std::string>::const_iterator begin() const {
return m_vArgs.begin();
}
std::vector<std::string>::iterator end() {
return m_vArgs.end();
}
std::vector<std::string>::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<std::string> m_vArgs;
};
}
private:
std::vector<std::string> m_vArgs;
};
}

View file

@ -2,16 +2,15 @@
#include <functional>
namespace Hyprutils {
namespace Utils {
// calls a function when it goes out of scope
class CScopeGuard {
public:
CScopeGuard(const std::function<void()>& fn_);
~CScopeGuard();
//NOLINTNEXTLINE
namespace Hyprutils::Utils {
// calls a function when it goes out of scope
class CScopeGuard {
public:
CScopeGuard(const std::function<void()>& fn_);
~CScopeGuard();
private:
std::function<void()> fn;
};
private:
std::function<void()> fn;
};
};

View file

@ -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() {

View file

@ -99,7 +99,7 @@ bool CAnimationManager::bezierExists(const std::string& bezier) {
}
SP<CBezierCurve> 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;
}

View file

@ -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);
}

View file

@ -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)}};
}

View file

@ -93,6 +93,7 @@ Mat3x3& Mat3x3::transform(eTransform transform) {
}
Mat3x3& Mat3x3::rotate(float rot) {
//no lint or maybe do something?
multiply(std::array<float, 9>{(float)cos(rot), (float)-sin(rot), 0.0f, (float)sin(rot), (float)cos(rot), 0.0f, 0.0f, 0.0f, 1.0f});
return *this;
}

View file

@ -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<CRegion*>(&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);
}

View file

@ -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 {

View file

@ -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() {

View file

@ -6,7 +6,6 @@ using namespace Hyprutils::OS;
#include <unistd.h>
#include <cstring>
#include <array>
#include <thread>
#include <sys/fcntl.h>
#include <sys/wait.h>
@ -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;
}

View file

@ -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);
}

View file

@ -1,6 +1,5 @@
#include <hyprutils/signal/Signal.hpp>
#include <hyprutils/memory/WeakPtr.hpp>
#include <algorithm>
using namespace Hyprutils::Signal;
using namespace Hyprutils::Memory;
@ -18,6 +17,7 @@ void Hyprutils::Signal::CSignal::emit(std::any data) {
}
std::vector<CStaticSignalListener*> 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<v
void Hyprutils::Signal::CSignal::registerStaticListener(std::function<void(void*, std::any)> handler, void* owner) {
m_vStaticListeners.emplace_back(std::make_unique<CStaticSignalListener>(handler, owner));
}
}

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -4,6 +4,7 @@
#include <hyprutils/memory/WeakPtr.hpp>
#include <hyprutils/memory/UniquePtr.hpp>
#include "shared.hpp"
#include <cstdint>
#define SP CSharedPointer
#define WP CWeakPointer
@ -24,7 +25,7 @@ using PANIMVAR = SP<CAnimatedVariable<VarType>>;
template <typename VarType>
using PANIMVARREF = WP<CAnimatedVariable<VarType>>;
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<CMyAnimationManager> pAnimationManager;
class Subject {
@ -120,7 +122,7 @@ class Subject {
PANIMVAR<int> m_iB;
PANIMVAR<SomeTestType> m_iC;
};
// Same
int config() {
pAnimationManager = makeUnique<CMyAnimationManager>();
@ -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();