From 605d9e23f39ce885094426ed28b31f0cd6f9bfec Mon Sep 17 00:00:00 2001 From: Martin Bond <28291143+martinbond7@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:46:27 +0000 Subject: [PATCH] removed bad use of std::string_view, moved comparing objects before templates --- .../src/MeasurementType.h | 2 +- .../02_vocabulary_types/src/MeasurementType.h | 2 +- .../02_vocabulary_types/src/Measurements.h | 8 ++-- .../include/Generator.h | 0 .../src/Measurement.h | 0 .../src/MeasurementType.h | 2 +- .../src/Measurements.h | 4 +- .../src/main.cpp | 1 + .../solutions/03_templates/src/Measurement.h | 26 ------------ .../include/Generator.h | 0 .../solutions/04_templates/src/Measurement.h | 41 +++++++++++++++++++ .../src/MeasurementType.h | 2 +- .../src/Measurements.h | 4 +- .../src/main.cpp | 0 .../05_requirements/src/MeasurementType.h | 2 +- .../05_requirements/src/Measurements.h | 4 +- .../solutions/06_concepts/src/Constraints.h | 9 ++-- .../06_concepts/src/MeasurementType.h | 2 +- .../solutions/06_concepts/src/Measurements.h | 4 +- .../07_ranges_views/src/Constraints.h | 2 +- .../07_ranges_views/src/MeasurementType.h | 2 +- .../07_ranges_views/src/Measurements.h | 4 +- exercises/solutions/08_pmr/src/Constraints.h | 2 +- .../solutions/08_pmr/src/MeasurementType.h | 2 +- exercises/solutions/08_pmr/src/Measurements.h | 4 +- .../solutions/09_coroutines/src/Constraints.h | 2 +- .../09_coroutines/src/MeasurementType.h | 2 +- .../09_coroutines/src/Measurements.h | 4 +- .../solutions/10_modules/src/Constraints.h | 2 +- .../10_modules/src/MeasurementType.ixx | 2 - .../datalogger/src/MeasurementType.h | 2 +- 31 files changed, 79 insertions(+), 64 deletions(-) rename exercises/solutions/{03_templates => 03_comparing_objects}/include/Generator.h (100%) rename exercises/solutions/{04_comparing_objects => 03_comparing_objects}/src/Measurement.h (100%) rename exercises/solutions/{03_templates => 03_comparing_objects}/src/MeasurementType.h (88%) rename exercises/solutions/{04_comparing_objects => 03_comparing_objects}/src/Measurements.h (86%) rename exercises/solutions/{04_comparing_objects => 03_comparing_objects}/src/main.cpp (99%) delete mode 100644 exercises/solutions/03_templates/src/Measurement.h rename exercises/solutions/{04_comparing_objects => 04_templates}/include/Generator.h (100%) create mode 100644 exercises/solutions/04_templates/src/Measurement.h rename exercises/solutions/{04_comparing_objects => 04_templates}/src/MeasurementType.h (88%) rename exercises/solutions/{03_templates => 04_templates}/src/Measurements.h (86%) rename exercises/solutions/{03_templates => 04_templates}/src/main.cpp (100%) diff --git a/exercises/solutions/01_language_features/src/MeasurementType.h b/exercises/solutions/01_language_features/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/01_language_features/src/MeasurementType.h +++ b/exercises/solutions/01_language_features/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/02_vocabulary_types/src/MeasurementType.h b/exercises/solutions/02_vocabulary_types/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/02_vocabulary_types/src/MeasurementType.h +++ b/exercises/solutions/02_vocabulary_types/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/02_vocabulary_types/src/Measurements.h b/exercises/solutions/02_vocabulary_types/src/Measurements.h index f18e928..03b499f 100644 --- a/exercises/solutions/02_vocabulary_types/src/Measurements.h +++ b/exercises/solutions/02_vocabulary_types/src/Measurements.h @@ -18,7 +18,7 @@ namespace DataLogger { public: using value_type = Measurement::value_type; - using add_type = std::variant; + using add_type = std::variant; Measurements(std::string_view id, MeasurementType type) : id{id}, type{type} @@ -30,7 +30,7 @@ namespace DataLogger inline bool add(add_type value); inline std::vector data() const; - inline std::tuple summary() const; + inline std::tuple summary() const; size_t size() const { return measures.size(); @@ -52,7 +52,7 @@ namespace DataLogger add(*dp); return true; } - else if(auto* sv = std::get_if(&value); sv != nullptr) { + else if(auto* sv = std::get_if(&value); sv != nullptr) { char* end {}; if (double d = std::strtod( sv->data(), &end ); end != sv->data()) { add(d); @@ -66,7 +66,7 @@ namespace DataLogger return measures; } - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/03_templates/include/Generator.h b/exercises/solutions/03_comparing_objects/include/Generator.h similarity index 100% rename from exercises/solutions/03_templates/include/Generator.h rename to exercises/solutions/03_comparing_objects/include/Generator.h diff --git a/exercises/solutions/04_comparing_objects/src/Measurement.h b/exercises/solutions/03_comparing_objects/src/Measurement.h similarity index 100% rename from exercises/solutions/04_comparing_objects/src/Measurement.h rename to exercises/solutions/03_comparing_objects/src/Measurement.h diff --git a/exercises/solutions/03_templates/src/MeasurementType.h b/exercises/solutions/03_comparing_objects/src/MeasurementType.h similarity index 88% rename from exercises/solutions/03_templates/src/MeasurementType.h rename to exercises/solutions/03_comparing_objects/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/03_templates/src/MeasurementType.h +++ b/exercises/solutions/03_comparing_objects/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/04_comparing_objects/src/Measurements.h b/exercises/solutions/03_comparing_objects/src/Measurements.h similarity index 86% rename from exercises/solutions/04_comparing_objects/src/Measurements.h rename to exercises/solutions/03_comparing_objects/src/Measurements.h index 8d58c77..821b718 100644 --- a/exercises/solutions/04_comparing_objects/src/Measurements.h +++ b/exercises/solutions/03_comparing_objects/src/Measurements.h @@ -28,7 +28,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -51,7 +51,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/04_comparing_objects/src/main.cpp b/exercises/solutions/03_comparing_objects/src/main.cpp similarity index 99% rename from exercises/solutions/04_comparing_objects/src/main.cpp rename to exercises/solutions/03_comparing_objects/src/main.cpp index 71f2b74..54b4a65 100644 --- a/exercises/solutions/04_comparing_objects/src/main.cpp +++ b/exercises/solutions/03_comparing_objects/src/main.cpp @@ -27,5 +27,6 @@ int main() assert(Measurement{1.002} > Measurement{1.0}); assert((Measurement{1.0} <=> Measurement{NAN}) == std::partial_ordering::unordered); + return 0; } diff --git a/exercises/solutions/03_templates/src/Measurement.h b/exercises/solutions/03_templates/src/Measurement.h deleted file mode 100644 index 8038362..0000000 --- a/exercises/solutions/03_templates/src/Measurement.h +++ /dev/null @@ -1,26 +0,0 @@ -// Measurement.h -// See project README.md for disclaimer and additional information. -// Feabhas Ltd - -#pragma once -#ifndef MEASUREMENT_H -#define MEASUREMENT_H -#include - -namespace DataLogger -{ - class Measurement - { - public: - using value_type = double; - static constexpr double eps {1e-3}; - - Measurement() = default; - explicit Measurement(double value) : value{value} {} - double get_value() const { return value; } - - private: - double value {}; - }; -} -#endif diff --git a/exercises/solutions/04_comparing_objects/include/Generator.h b/exercises/solutions/04_templates/include/Generator.h similarity index 100% rename from exercises/solutions/04_comparing_objects/include/Generator.h rename to exercises/solutions/04_templates/include/Generator.h diff --git a/exercises/solutions/04_templates/src/Measurement.h b/exercises/solutions/04_templates/src/Measurement.h new file mode 100644 index 0000000..f61af97 --- /dev/null +++ b/exercises/solutions/04_templates/src/Measurement.h @@ -0,0 +1,41 @@ +// Measurement.h +// See project README.md for disclaimer and additional information. +// Feabhas Ltd + +#pragma once +#ifndef MEASUREMENT_H +#define MEASUREMENT_H +#include + +namespace DataLogger +{ + class Measurement + { + public: + using value_type = double; + static constexpr double eps {1e-3}; + + Measurement() = default; + explicit Measurement(double value) : value{value} {} + // Measurement(const Measurement&&) = delete; + double get_value() const { return value; } + + std::partial_ordering operator<=>(const Measurement& rhs) const + { + auto diff = fabs(value - rhs.value); + if (diff <= eps) return std::partial_ordering::equivalent; + else if (value < rhs.value) return std::partial_ordering::less; + else if (value > rhs.value) return std::partial_ordering::greater; + return std::partial_ordering::unordered; + } + + bool operator==(const Measurement& rhs) const + { + return (*this <=> rhs) == std::partial_ordering::equivalent; + } + + private: + double value {}; + }; +} +#endif diff --git a/exercises/solutions/04_comparing_objects/src/MeasurementType.h b/exercises/solutions/04_templates/src/MeasurementType.h similarity index 88% rename from exercises/solutions/04_comparing_objects/src/MeasurementType.h rename to exercises/solutions/04_templates/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/04_comparing_objects/src/MeasurementType.h +++ b/exercises/solutions/04_templates/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/03_templates/src/Measurements.h b/exercises/solutions/04_templates/src/Measurements.h similarity index 86% rename from exercises/solutions/03_templates/src/Measurements.h rename to exercises/solutions/04_templates/src/Measurements.h index 8d58c77..821b718 100644 --- a/exercises/solutions/03_templates/src/Measurements.h +++ b/exercises/solutions/04_templates/src/Measurements.h @@ -28,7 +28,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -51,7 +51,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/03_templates/src/main.cpp b/exercises/solutions/04_templates/src/main.cpp similarity index 100% rename from exercises/solutions/03_templates/src/main.cpp rename to exercises/solutions/04_templates/src/main.cpp diff --git a/exercises/solutions/05_requirements/src/MeasurementType.h b/exercises/solutions/05_requirements/src/MeasurementType.h index 371d2f9..76b7ce9 100644 --- a/exercises/solutions/05_requirements/src/MeasurementType.h +++ b/exercises/solutions/05_requirements/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr std::string to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/05_requirements/src/Measurements.h b/exercises/solutions/05_requirements/src/Measurements.h index 15e0cb9..3bbcccd 100644 --- a/exercises/solutions/05_requirements/src/Measurements.h +++ b/exercises/solutions/05_requirements/src/Measurements.h @@ -35,7 +35,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -73,7 +73,7 @@ namespace DataLogger std::is_copy_assignable_v && requires (T x) { x.get_value(); } && requires (T x, T y) { x < y; } - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/06_concepts/src/Constraints.h b/exercises/solutions/06_concepts/src/Constraints.h index 3c5ccba..8f7a76d 100644 --- a/exercises/solutions/06_concepts/src/Constraints.h +++ b/exercises/solutions/06_concepts/src/Constraints.h @@ -16,9 +16,10 @@ namespace DataLogger { template concept Measurable = requires { - requires std::semiregular && - requires (T x) { x.get_value(); } && - requires (T x, T y) { x < y; }; + requires std::semiregular; + requires + requires (T x) { {x.get_value()} -> std::same_as; } && + requires (T x, T y) { {x < y} -> std::same_as; }; }; @@ -27,7 +28,7 @@ namespace DataLogger { t.add(typename T::value_type{}) }; { t.size() } -> std::same_as; { t.data() } -> std::same_as>; - { t.summary() } -> std::same_as>; + { t.summary() } -> std::same_as>; }; } diff --git a/exercises/solutions/06_concepts/src/MeasurementType.h b/exercises/solutions/06_concepts/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/06_concepts/src/MeasurementType.h +++ b/exercises/solutions/06_concepts/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/06_concepts/src/Measurements.h b/exercises/solutions/06_concepts/src/Measurements.h index e7463c3..c025d5a 100644 --- a/exercises/solutions/06_concepts/src/Measurements.h +++ b/exercises/solutions/06_concepts/src/Measurements.h @@ -30,7 +30,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -53,7 +53,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/07_ranges_views/src/Constraints.h b/exercises/solutions/07_ranges_views/src/Constraints.h index 3c5ccba..e5d91fd 100644 --- a/exercises/solutions/07_ranges_views/src/Constraints.h +++ b/exercises/solutions/07_ranges_views/src/Constraints.h @@ -27,7 +27,7 @@ namespace DataLogger { t.add(typename T::value_type{}) }; { t.size() } -> std::same_as; { t.data() } -> std::same_as>; - { t.summary() } -> std::same_as>; + { t.summary() } -> std::same_as>; }; } diff --git a/exercises/solutions/07_ranges_views/src/MeasurementType.h b/exercises/solutions/07_ranges_views/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/07_ranges_views/src/MeasurementType.h +++ b/exercises/solutions/07_ranges_views/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/07_ranges_views/src/Measurements.h b/exercises/solutions/07_ranges_views/src/Measurements.h index 8acaa75..53a11c7 100644 --- a/exercises/solutions/07_ranges_views/src/Measurements.h +++ b/exercises/solutions/07_ranges_views/src/Measurements.h @@ -31,7 +31,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -63,7 +63,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/08_pmr/src/Constraints.h b/exercises/solutions/08_pmr/src/Constraints.h index f34e0df..d195b1e 100644 --- a/exercises/solutions/08_pmr/src/Constraints.h +++ b/exercises/solutions/08_pmr/src/Constraints.h @@ -27,7 +27,7 @@ namespace DataLogger { t.add(typename T::value_type{}) }; { t.size() } -> std::same_as; { t.data() } -> std::same_as>; - { t.summary() } -> std::same_as>; + { t.summary() } -> std::same_as>; }; } diff --git a/exercises/solutions/08_pmr/src/MeasurementType.h b/exercises/solutions/08_pmr/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/08_pmr/src/MeasurementType.h +++ b/exercises/solutions/08_pmr/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/08_pmr/src/Measurements.h b/exercises/solutions/08_pmr/src/Measurements.h index 8acaa75..53a11c7 100644 --- a/exercises/solutions/08_pmr/src/Measurements.h +++ b/exercises/solutions/08_pmr/src/Measurements.h @@ -31,7 +31,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -63,7 +63,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/09_coroutines/src/Constraints.h b/exercises/solutions/09_coroutines/src/Constraints.h index 3c5ccba..e5d91fd 100644 --- a/exercises/solutions/09_coroutines/src/Constraints.h +++ b/exercises/solutions/09_coroutines/src/Constraints.h @@ -27,7 +27,7 @@ namespace DataLogger { t.add(typename T::value_type{}) }; { t.size() } -> std::same_as; { t.data() } -> std::same_as>; - { t.summary() } -> std::same_as>; + { t.summary() } -> std::same_as>; }; } diff --git a/exercises/solutions/09_coroutines/src/MeasurementType.h b/exercises/solutions/09_coroutines/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/solutions/09_coroutines/src/MeasurementType.h +++ b/exercises/solutions/09_coroutines/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" }; diff --git a/exercises/solutions/09_coroutines/src/Measurements.h b/exercises/solutions/09_coroutines/src/Measurements.h index afa2ef6..3d60a96 100644 --- a/exercises/solutions/09_coroutines/src/Measurements.h +++ b/exercises/solutions/09_coroutines/src/Measurements.h @@ -32,7 +32,7 @@ namespace DataLogger void add(value_type value); std::vector data() const; - std::tuple summary() const; + std::tuple summary() const; size_t size() const { return measures.size(); @@ -66,7 +66,7 @@ namespace DataLogger } template - std::tuple Measurements::summary() const + std::tuple Measurements::summary() const { return {id, type, measures.size()}; } diff --git a/exercises/solutions/10_modules/src/Constraints.h b/exercises/solutions/10_modules/src/Constraints.h index 731e462..50de06e 100644 --- a/exercises/solutions/10_modules/src/Constraints.h +++ b/exercises/solutions/10_modules/src/Constraints.h @@ -30,7 +30,7 @@ namespace DataLogger { t.add(typename T::value_type{}) }; { t.size() } -> std::same_as; { t.data() } -> std::same_as>; - { t.summary() } -> std::same_as>; + { t.summary() } -> std::same_as>; }; } diff --git a/exercises/solutions/10_modules/src/MeasurementType.ixx b/exercises/solutions/10_modules/src/MeasurementType.ixx index c3f02dc..2f4fbc6 100644 --- a/exercises/solutions/10_modules/src/MeasurementType.ixx +++ b/exercises/solutions/10_modules/src/MeasurementType.ixx @@ -15,7 +15,6 @@ // ----------------------------------------------------------------------------- module; #include -// #include #include export module MeasurementType; @@ -23,7 +22,6 @@ export namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - // gcc 13.1 fails to compile if return type is std::string_view constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" diff --git a/exercises/templates/datalogger/src/MeasurementType.h b/exercises/templates/datalogger/src/MeasurementType.h index 371d2f9..9f49e14 100644 --- a/exercises/templates/datalogger/src/MeasurementType.h +++ b/exercises/templates/datalogger/src/MeasurementType.h @@ -14,7 +14,7 @@ namespace DataLogger { enum class MeasurementType {unknown, temperature, pressure, minT, maxT}; - constexpr std::string_view to_string(MeasurementType type) { + constexpr const char* to_string(MeasurementType type) { constexpr std::array names { "unknown", "temperature", "pressure", "minT", "maxT" };