feat(example): text output added to validated_type and ranged_representation

This commit is contained in:
Mateusz Pusz
2023-09-04 11:15:33 +02:00
parent b84c74b330
commit b3fb025053
2 changed files with 31 additions and 0 deletions

View File

@@ -24,6 +24,7 @@
#include "validated_type.h"
#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/fmt.h>
#include <mp-units/customization_points.h>
#include <algorithm>
#include <concepts>
@@ -53,3 +54,12 @@ public:
template<typename T, auto Min, auto Max>
inline constexpr bool mp_units::is_scalar<ranged_representation<T, Min, Max>> = mp_units::is_scalar<T>;
template<typename T, auto Min, auto Max>
struct MP_UNITS_STD_FMT::formatter<ranged_representation<T, Min, Max>> : formatter<T> {
template<typename FormatContext>
auto format(const ranged_representation<T, Min, Max>& v, FormatContext& ctx)
{
return formatter<T>::format(v.value(), ctx);
}
};

View File

@@ -24,6 +24,8 @@
#include <gsl/gsl-lite.hpp>
#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/fmt.h>
#include <ostream>
#include <utility>
inline constexpr struct validated_tag {
@@ -97,3 +99,22 @@ public:
requires std::three_way_comparable<T>
= default;
};
template<typename CharT, typename Traits, typename T, typename Validator>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const validated_type<T, Validator>& v)
requires requires { os << v.value(); }
{
return os << v.value();
}
template<typename T, typename Validator>
struct MP_UNITS_STD_FMT::formatter<validated_type<T, Validator>> : formatter<T> {
template<typename FormatContext>
auto format(const validated_type<T, Validator>& v, FormatContext& ctx)
{
return formatter<T>::format(v.value(), ctx);
}
};