From 937ec7189a578c70ed3ae3f871cb0150fbae8e0a Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 16 Apr 2024 08:04:49 +0100 Subject: [PATCH] Make first() and last() static for template based limits specialisation --- include/etl/cyclic_value.h | 91 +++++++++++++++++++------------------- test/test_cyclic_value.cpp | 9 +++- 2 files changed, 54 insertions(+), 46 deletions(-) diff --git a/include/etl/cyclic_value.h b/include/etl/cyclic_value.h index 03f19325..392c7a14 100644 --- a/include/etl/cyclic_value.h +++ b/include/etl/cyclic_value.h @@ -49,19 +49,19 @@ namespace etl //*************************************************************************** /// Provides a value that cycles between two limits. //*************************************************************************** - template + template class cyclic_value; //*************************************************************************** - /// Provides a value that cycles between two limits. + /// Provides a value that cycles between two compile time limits. /// Supports incrementing and decrementing. ///\tparam T The type of the variable. - ///\tparam FIRST The first value of the range. - ///\tparam LAST The last value of the range. + ///\tparam First The first value of the range. + ///\tparam Last The last value of the range. ///\ingroup cyclic_value //*************************************************************************** - template - class cyclic_value + template + class cyclic_value { public: @@ -70,7 +70,7 @@ namespace etl /// The initial value is set to the first value. //************************************************************************* cyclic_value() - : value(FIRST) + : value(First) { } @@ -87,7 +87,7 @@ namespace etl //************************************************************************* /// Copy constructor. //************************************************************************* - cyclic_value(const cyclic_value& other) + cyclic_value(const cyclic_value& other) : value(other.value) { } @@ -95,7 +95,7 @@ namespace etl //************************************************************************* /// Assignment operator. //************************************************************************* - cyclic_value& operator =(const cyclic_value& other) + cyclic_value& operator =(const cyclic_value& other) { value = other.value; @@ -104,17 +104,18 @@ namespace etl //************************************************************************* /// Sets the value. + /// Truncates to the First/Last range. ///\param value The value. //************************************************************************* void set(T value_) { - if (value_ > LAST) + if (value_ > Last) { - value_ = LAST; + value_ = Last; } - else if (value_ < FIRST) + else if (value_ < First) { - value_ = FIRST; + value_ = First; } value = value_; @@ -125,7 +126,7 @@ namespace etl //************************************************************************* void to_first() { - value = FIRST; + value = First; } //************************************************************************* @@ -133,7 +134,7 @@ namespace etl //************************************************************************* void to_last() { - value = LAST; + value = Last; } //************************************************************************* @@ -181,9 +182,9 @@ namespace etl //************************************************************************* cyclic_value& operator ++() { - if (value >= LAST) ETL_UNLIKELY + if (value >= Last) ETL_UNLIKELY { - value = FIRST; + value = First; } else { @@ -210,9 +211,9 @@ namespace etl //************************************************************************* cyclic_value& operator --() { - if (value <= FIRST) ETL_UNLIKELY + if (value <= First) ETL_UNLIKELY { - value = LAST; + value = Last; } else { @@ -264,23 +265,23 @@ namespace etl //************************************************************************* /// Gets the first value. //************************************************************************* - const T first() const + static ETL_CONSTEXPR T first() { - return FIRST; + return First; } //************************************************************************* /// Gets the last value. //************************************************************************* - const T last() const + static ETL_CONSTEXPR T last() { - return LAST; + return Last; } //************************************************************************* /// Swaps the values. //************************************************************************* - void swap(cyclic_value& other) + void swap(cyclic_value& other) { using ETL_OR_STD::swap; // Allow ADL @@ -290,7 +291,7 @@ namespace etl //************************************************************************* /// Swaps the values. //************************************************************************* - friend void swap(cyclic_value& lhs, cyclic_value& rhs) + friend void swap(cyclic_value& lhs, cyclic_value& rhs) { lhs.swap(rhs); } @@ -298,7 +299,7 @@ namespace etl //************************************************************************* /// Operator ==. //************************************************************************* - friend bool operator == (const cyclic_value& lhs, const cyclic_value& rhs) + friend bool operator == (const cyclic_value& lhs, const cyclic_value& rhs) { return lhs.value == rhs.value; } @@ -306,7 +307,7 @@ namespace etl //************************************************************************* /// Operator !=. //************************************************************************* - friend bool operator != (const cyclic_value& lhs, const cyclic_value& rhs) + friend bool operator != (const cyclic_value& lhs, const cyclic_value& rhs) { return !(lhs == rhs); } @@ -317,15 +318,15 @@ namespace etl }; //*************************************************************************** - /// Provides a value that cycles between two limits. + /// Provides a value that cycles between two run time limits. /// Supports incrementing and decrementing. ///\tparam T The type of the variable. - ///\tparam FIRST The first value of the range. - ///\tparam LAST The last value of the range. + ///\tparam First The first value of the range. + ///\tparam Last The last value of the range. ///\ingroup cyclic_value //*************************************************************************** - template - class cyclic_value + template + class cyclic_value { public: @@ -335,9 +336,9 @@ namespace etl /// The initial value is set to the first value. //************************************************************************* cyclic_value() - : value(FIRST), - first_value(FIRST), - last_value(LAST) + : value(First) + , first_value(First) + , last_value(Last) { } @@ -348,9 +349,9 @@ namespace etl ///\param last The last value in the range. //************************************************************************* cyclic_value(T first_, T last_) - : value(first_), - first_value(first_), - last_value(last_) + : value(first_) + , first_value(first_) + , last_value(last_) { } @@ -372,9 +373,9 @@ namespace etl /// Copy constructor. //************************************************************************* cyclic_value(const cyclic_value& other) - : value(other.value), - first_value(other.first_value), - last_value(other.last_value) + : value(other.value) + , first_value(other.first_value) + , last_value(other.last_value) { } @@ -570,7 +571,7 @@ namespace etl //************************************************************************* /// Swaps the values. //************************************************************************* - void swap(cyclic_value& other) + void swap(cyclic_value& other) { using ETL_OR_STD::swap; // Allow ADL @@ -582,7 +583,7 @@ namespace etl //************************************************************************* /// Swaps the values. //************************************************************************* - friend void swap(cyclic_value& lhs, cyclic_value& rhs) + friend void swap(cyclic_value& lhs, cyclic_value& rhs) { lhs.swap(rhs); } @@ -590,7 +591,7 @@ namespace etl //************************************************************************* /// Operator ==. //************************************************************************* - friend bool operator == (const cyclic_value& lhs, const cyclic_value& rhs) + friend bool operator == (const cyclic_value& lhs, const cyclic_value& rhs) { return (lhs.value == rhs.value) && (lhs.first_value == rhs.first_value) && @@ -600,7 +601,7 @@ namespace etl //************************************************************************* /// Operator !=. //************************************************************************* - friend bool operator != (const cyclic_value& lhs, const cyclic_value& rhs) + friend bool operator != (const cyclic_value& lhs, const cyclic_value& rhs) { return !(lhs == rhs); } diff --git a/test/test_cyclic_value.cpp b/test/test_cyclic_value.cpp index a15d1699..face6086 100644 --- a/test/test_cyclic_value.cpp +++ b/test/test_cyclic_value.cpp @@ -75,7 +75,14 @@ namespace //************************************************************************* TEST(test_set_compile_time) { - etl::cyclic_value value; + using CV = etl::cyclic_value; + + CV value; + + CHECK_EQUAL(2, value.first()); + CHECK_EQUAL(7, value.last()); + CHECK_EQUAL(2, CV::first()); + CHECK_EQUAL(7, CV::last()); value.set(5); CHECK_EQUAL(5, value.get());