Skip to content

Commit

Permalink
removed bad use of std::string_view, moved comparing objects before t…
Browse files Browse the repository at this point in the history
…emplates
  • Loading branch information
martinbond7 committed Oct 30, 2024
1 parent 1030336 commit 605d9e2
Show file tree
Hide file tree
Showing 31 changed files with 79 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
8 changes: 4 additions & 4 deletions exercises/solutions/02_vocabulary_types/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DataLogger
{
public:
using value_type = Measurement::value_type;
using add_type = std::variant<double, std::string_view>;
using add_type = std::variant<double, std::string>;

Measurements(std::string_view id, MeasurementType type)
: id{id}, type{type}
Expand All @@ -30,7 +30,7 @@ namespace DataLogger
inline bool add(add_type value);

inline std::vector<Measurement> data() const;
inline std::tuple<std::string_view, MeasurementType, size_t> summary() const;
inline std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand All @@ -52,7 +52,7 @@ namespace DataLogger
add(*dp);
return true;
}
else if(auto* sv = std::get_if<std::string_view>(&value); sv != nullptr) {
else if(auto* sv = std::get_if<std::string>(&value); sv != nullptr) {
char* end {};
if (double d = std::strtod( sv->data(), &end ); end != sv->data()) {
add(d);
Expand All @@ -66,7 +66,7 @@ namespace DataLogger
return measures;
}

std::tuple<std::string_view, MeasurementType, size_t> Measurements::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements::summary() const
{
return {id, type, measures.size()};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand All @@ -51,7 +51,7 @@ namespace DataLogger
}

template<typename T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
26 changes: 0 additions & 26 deletions exercises/solutions/03_templates/src/Measurement.h

This file was deleted.

41 changes: 41 additions & 0 deletions exercises/solutions/04_templates/src/Measurement.h
Original file line number Diff line number Diff line change
@@ -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 <cmath>

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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand All @@ -51,7 +51,7 @@ namespace DataLogger
}

template<typename T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/05_requirements/src/MeasurementType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
4 changes: 2 additions & 2 deletions exercises/solutions/05_requirements/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand Down Expand Up @@ -73,7 +73,7 @@ namespace DataLogger
std::is_copy_assignable_v<T> &&
requires (T x) { x.get_value(); } &&
requires (T x, T y) { x < y; }
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
9 changes: 5 additions & 4 deletions exercises/solutions/06_concepts/src/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace DataLogger
{
template<typename T>
concept Measurable = requires {
requires std::semiregular<T> &&
requires (T x) { x.get_value(); } &&
requires (T x, T y) { x < y; };
requires std::semiregular<T>;
requires
requires (T x) { {x.get_value()} -> std::same_as<typename T::value_type>; } &&
requires (T x, T y) { {x < y} -> std::same_as<bool>; };
};


Expand All @@ -27,7 +28,7 @@ namespace DataLogger
{ t.add(typename T::value_type{}) };
{ t.size() } -> std::same_as<size_t>;
{ t.data() } -> std::same_as<std::vector<typename T::measurement_type>>;
{ t.summary() } -> std::same_as<std::tuple<std::string_view, MeasurementType, size_t>>;
{ t.summary() } -> std::same_as<std::tuple<std::string, MeasurementType, size_t>>;
};
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/06_concepts/src/MeasurementType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
4 changes: 2 additions & 2 deletions exercises/solutions/06_concepts/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand All @@ -53,7 +53,7 @@ namespace DataLogger
}

template<Measurable T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/07_ranges_views/src/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace DataLogger
{ t.add(typename T::value_type{}) };
{ t.size() } -> std::same_as<size_t>;
{ t.data() } -> std::same_as<std::vector<typename T::measurement_type>>;
{ t.summary() } -> std::same_as<std::tuple<std::string_view, MeasurementType, size_t>>;
{ t.summary() } -> std::same_as<std::tuple<std::string, MeasurementType, size_t>>;
};
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/07_ranges_views/src/MeasurementType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
4 changes: 2 additions & 2 deletions exercises/solutions/07_ranges_views/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand Down Expand Up @@ -63,7 +63,7 @@ namespace DataLogger
}

template<Measurable T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/08_pmr/src/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace DataLogger
{ t.add(typename T::value_type{}) };
{ t.size() } -> std::same_as<size_t>;
{ t.data() } -> std::same_as<std::vector<typename T::measurement_type>>;
{ t.summary() } -> std::same_as<std::tuple<std::string_view, MeasurementType, size_t>>;
{ t.summary() } -> std::same_as<std::tuple<std::string, MeasurementType, size_t>>;
};
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/08_pmr/src/MeasurementType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
4 changes: 2 additions & 2 deletions exercises/solutions/08_pmr/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand Down Expand Up @@ -63,7 +63,7 @@ namespace DataLogger
}

template<Measurable T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/09_coroutines/src/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace DataLogger
{ t.add(typename T::value_type{}) };
{ t.size() } -> std::same_as<size_t>;
{ t.data() } -> std::same_as<std::vector<typename T::measurement_type>>;
{ t.summary() } -> std::same_as<std::tuple<std::string_view, MeasurementType, size_t>>;
{ t.summary() } -> std::same_as<std::tuple<std::string, MeasurementType, size_t>>;
};
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/09_coroutines/src/MeasurementType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down
4 changes: 2 additions & 2 deletions exercises/solutions/09_coroutines/src/Measurements.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace DataLogger
void add(value_type value);

std::vector<T> data() const;
std::tuple<std::string_view, MeasurementType, size_t> summary() const;
std::tuple<std::string, MeasurementType, size_t> summary() const;

size_t size() const {
return measures.size();
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace DataLogger
}

template<Measurable T>
std::tuple<std::string_view, MeasurementType, size_t> Measurements<T>::summary() const
std::tuple<std::string, MeasurementType, size_t> Measurements<T>::summary() const
{
return {id, type, measures.size()};
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/solutions/10_modules/src/Constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace DataLogger
{ t.add(typename T::value_type{}) };
{ t.size() } -> std::same_as<size_t>;
{ t.data() } -> std::same_as<std::vector<typename T::measurement_type>>;
{ t.summary() } -> std::same_as<std::tuple<std::string_view, MeasurementType, size_t>>;
{ t.summary() } -> std::same_as<std::tuple<std::string, MeasurementType, size_t>>;
};
}

Expand Down
2 changes: 0 additions & 2 deletions exercises/solutions/10_modules/src/MeasurementType.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
// -----------------------------------------------------------------------------
module;
#include <cstddef>
// #include <string_view>
#include <array>
export module MeasurementType;

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"
Expand Down
Loading

0 comments on commit 605d9e2

Please sign in to comment.