diff --git a/lib/core/include/qx/core/qx-setonce.h b/lib/core/include/qx/core/qx-setonce.h index a6f83943..45e51b6c 100644 --- a/lib/core/include/qx/core/qx-setonce.h +++ b/lib/core/include/qx/core/qx-setonce.h @@ -4,22 +4,27 @@ // Standard Library Includes #include +// Extra-component Includes +#include + namespace Qx { -template - requires std::is_assignable_v +template> + requires std::is_assignable_v && Qx::defines_call_for_s class SetOnce { //-Instance Members---------------------------------------------------------------------------------------------------- private: + CompareEq mComparator; bool mSet; T mDefaultValue; T mValue; //-Constructor------------------------------------------------------------------------------------------------------- public: - SetOnce(T initial) : + SetOnce(T initial, const CompareEq& comp = CompareEq()) : + mComparator(comp), mSet(false), mDefaultValue(initial), mValue(initial) @@ -36,9 +41,9 @@ class SetOnce mValue = mDefaultValue; } - SetOnce& operator=(const T& value) + SetOnce& operator=(const T& value) { - if(!mSet && mDefaultValue != value) + if(!mSet && !mComparator(mDefaultValue, value)) { mValue = value; mSet = true; diff --git a/lib/core/src/qx-setonce.dox b/lib/core/src/qx-setonce.dox index cf1000c7..45843e40 100644 --- a/lib/core/src/qx-setonce.dox +++ b/lib/core/src/qx-setonce.dox @@ -9,35 +9,44 @@ namespace Qx * @ingroup qx-core * * @brief The SetOnce template class acts as a container for a value that can only be set once. + * + * The optional @a CompareEq template parameter can be used to provide a custom compare-equal function + * object type. */ //-Constructor---------------------------------------------------------------------------------------------- //Public: /*! - * @fn SetOnce::SetOnce(T initial) + * @fn SetOnce::SetOnce(T initial, const CompareEq& comp) * * Creates a SetOnce container that holds the initial value @a initial. * * The container is initially unset and only holds this value until it is set. + * + * Optionally, a custom compare-equal function can be provided through @a comp, which + * is used to determine whether or not an assigned value is different from the + * container's initial value. + * + * @sa operator=(const T& value). */ //-Instance Functions---------------------------------------------------------------------------------------------- //Public: /*! - * @fn SetOnce::isSet() const + * @fn SetOnce::isSet() const * * Returns @c true if the containers value has been set; otherwise returns @c false. */ /*! - * @fn const T& SetOnce::value() const + * @fn const T& SetOnce::value() const * * Returns the current value of the container. */ /*! - * @fn void SetOnce::reset() + * @fn void SetOnce::reset() * * Resets the container to its initial state. * @@ -47,7 +56,7 @@ namespace Qx */ /*! - * @fn SetOnce& SetOnce::operator=(const T& value) + * @fn SetOnce& SetOnce::operator=(const T& value) * * Sets the value of the container to @a value, if it is different from its initial value. *