Skip to content

Commit

Permalink
Add optional custom comparator to SetOnce<T, CompareEq>
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Jul 17, 2023
1 parent a87499d commit 036444f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 10 additions & 5 deletions lib/core/include/qx/core/qx-setonce.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
// Standard Library Includes
#include <type_traits>

// Extra-component Includes
#include <qx/utility/qx-concepts.h>

namespace Qx
{

template<typename T>
requires std::is_assignable_v<T&, T>
template<typename T, class CompareEq = std::equal_to<T>>
requires std::is_assignable_v<T&, T> && Qx::defines_call_for_s<CompareEq, bool, T, T>
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)
Expand All @@ -36,9 +41,9 @@ class SetOnce
mValue = mDefaultValue;
}

SetOnce<T>& operator=(const T& value)
SetOnce<T, CompareEq>& operator=(const T& value)
{
if(!mSet && mDefaultValue != value)
if(!mSet && !mComparator(mDefaultValue, value))
{
mValue = value;
mSet = true;
Expand Down
19 changes: 14 additions & 5 deletions lib/core/src/qx-setonce.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>::SetOnce(T initial)
* @fn SetOnce<T, CompareEq>::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<T>::isSet() const
* @fn SetOnce<T, CompareEq>::isSet() const
*
* Returns @c true if the containers value has been set; otherwise returns @c false.
*/

/*!
* @fn const T& SetOnce<T>::value() const
* @fn const T& SetOnce<T, CompareEq>::value() const
*
* Returns the current value of the container.
*/

/*!
* @fn void SetOnce<T>::reset()
* @fn void SetOnce<T, CompareEq>::reset()
*
* Resets the container to its initial state.
*
Expand All @@ -47,7 +56,7 @@ namespace Qx
*/

/*!
* @fn SetOnce<T>& SetOnce<T>::operator=(const T& value)
* @fn SetOnce<T, CompareEq>& SetOnce<T, CompareEq>::operator=(const T& value)
*
* Sets the value of the container to @a value, if it is different from its initial value.
*
Expand Down

0 comments on commit 036444f

Please sign in to comment.