Skip to content

Commit

Permalink
refactor: basic_symbol_text renamed to symbol_text
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Mar 18, 2024
1 parent 7a0baae commit fb97c2e
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 113 deletions.
4 changes: 2 additions & 2 deletions docs/users_guide/framework_basics/text_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ and units of derived quantities.

!!! tip

For older compilers, it might be required to specify a `basic_symbol_text` class explicitly
For older compilers, it might be required to specify a `symbol_text` class explicitly
template name to initialize it with two symbols:

```cpp
inline constexpr struct ohm : named_unit<basic_symbol_text{u8"Ω", "ohm"}, volt / ampere> {} ohm;
inline constexpr struct ohm : named_unit<symbol_text{u8"Ω", "ohm"}, volt / ampere> {} ohm;
```


Expand Down
6 changes: 3 additions & 3 deletions src/core/include/mp-units/bits/dimension_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@

namespace mp_units {

MP_UNITS_EXPORT template<basic_symbol_text Symbol>
MP_UNITS_EXPORT template<symbol_text Symbol>
struct base_dimension;

namespace detail {

template<basic_symbol_text Symbol>
template<symbol_text Symbol>
void to_base_specialization_of_base_dimension(const volatile base_dimension<Symbol>*);

template<typename T>
Expand All @@ -44,7 +44,7 @@ inline constexpr bool is_derived_from_specialization_of_base_dimension =
template<typename T>
inline constexpr bool is_specialization_of_base_dimension = false;

template<basic_symbol_text Symbol>
template<symbol_text Symbol>
inline constexpr bool is_specialization_of_base_dimension<base_dimension<Symbol>> = true;

/**
Expand Down
16 changes: 8 additions & 8 deletions src/core/include/mp-units/bits/magnitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ template<typename T, auto... Ms>
return integer_part((detail::abs(power_of_2) < detail::abs(power_of_5)) ? power_of_2 : power_of_5);
}

inline constexpr basic_symbol_text base_multiplier(u8"× 10", "x 10");
inline constexpr symbol_text base_multiplier(u8"× 10", "x 10");

template<Magnitude auto M>
[[nodiscard]] consteval auto magnitude_text()
Expand All @@ -880,23 +880,23 @@ template<Magnitude auto M>
if constexpr (num_value == 1 && den_value == 1 && exp10 != 0) {
return base_multiplier + superscript<exp10>();
} else if constexpr (num_value != 1 || den_value != 1 || exp10 != 0) {
auto txt = basic_symbol_text("[") + regular<num_value>();
auto txt = symbol_text("[") + regular<num_value>();
if constexpr (den_value == 1) {
if constexpr (exp10 == 0) {
return txt + basic_symbol_text("]");
return txt + symbol_text("]");
} else {
return txt + basic_symbol_text(" ") + base_multiplier + superscript<exp10>() + basic_symbol_text("]");
return txt + symbol_text(" ") + base_multiplier + superscript<exp10>() + symbol_text("]");
}
} else {
if constexpr (exp10 == 0) {
return txt + basic_symbol_text("/") + regular<den_value>() + basic_symbol_text("]");
return txt + symbol_text("/") + regular<den_value>() + symbol_text("]");
} else {
return txt + basic_symbol_text("/") + regular<den_value>() + basic_symbol_text(" ") + base_multiplier +
superscript<exp10>() + basic_symbol_text("]");
return txt + symbol_text("/") + regular<den_value>() + symbol_text(" ") + base_multiplier +
superscript<exp10>() + symbol_text("]");
}
}
} else {
return basic_symbol_text("");
return symbol_text("");
}
}

Expand Down
35 changes: 16 additions & 19 deletions src/core/include/mp-units/bits/symbol_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,35 @@ constexpr fixed_u8string<N> to_u8string(fixed_string<N> txt)
* @tparam M The size of the ASCII-only symbol
*/
MP_UNITS_EXPORT template<std::size_t N, std::size_t M>
struct basic_symbol_text {
struct symbol_text {
fixed_u8string<N> unicode_;
fixed_string<M> ascii_;

constexpr explicit(false) basic_symbol_text(char ch) : unicode_(static_cast<char8_t>(ch)), ascii_(ch)
constexpr explicit(false) symbol_text(char ch) : unicode_(static_cast<char8_t>(ch)), ascii_(ch)
{
gsl_Expects(detail::is_basic_literal_character_set_char(ch));
}

constexpr explicit(false) basic_symbol_text(const char (&txt)[N + 1]) :
constexpr explicit(false) symbol_text(const char (&txt)[N + 1]) :
unicode_(detail::to_u8string(basic_fixed_string{txt})), ascii_(txt)
{
gsl_ExpectsAudit(txt[N] == char{});
gsl_Expects(detail::is_basic_literal_character_set(txt));
}

constexpr explicit(false) basic_symbol_text(const fixed_string<N>& txt) :
unicode_(detail::to_u8string(txt)), ascii_(txt)
constexpr explicit(false) symbol_text(const fixed_string<N>& txt) : unicode_(detail::to_u8string(txt)), ascii_(txt)
{
gsl_Expects(detail::is_basic_literal_character_set(txt.data_));
}

constexpr basic_symbol_text(const char8_t (&u)[N + 1], const char (&a)[M + 1]) : unicode_(u), ascii_(a)
constexpr symbol_text(const char8_t (&u)[N + 1], const char (&a)[M + 1]) : unicode_(u), ascii_(a)
{
gsl_ExpectsAudit(u[N] == char8_t{});
gsl_ExpectsAudit(a[M] == char{});
gsl_Expects(detail::is_basic_literal_character_set(a));
}

constexpr basic_symbol_text(const fixed_u8string<N>& u, const fixed_string<M>& a) : unicode_(u), ascii_(a)
constexpr symbol_text(const fixed_u8string<N>& u, const fixed_string<M>& a) : unicode_(u), ascii_(a)
{
gsl_Expects(detail::is_basic_literal_character_set(a.data_));
}
Expand All @@ -123,15 +122,14 @@ struct basic_symbol_text {
}

template<std::size_t N2, std::size_t M2>
[[nodiscard]] constexpr friend basic_symbol_text<N + N2, M + M2> operator+(const basic_symbol_text& lhs,
const basic_symbol_text<N2, M2>& rhs)
[[nodiscard]] constexpr friend symbol_text<N + N2, M + M2> operator+(const symbol_text& lhs,
const symbol_text<N2, M2>& rhs)
{
return basic_symbol_text<N + N2, M + M2>(lhs.unicode() + rhs.unicode(), lhs.ascii() + rhs.ascii());
return symbol_text<N + N2, M + M2>(lhs.unicode() + rhs.unicode(), lhs.ascii() + rhs.ascii());
}

template<std::size_t N2, std::size_t M2>
[[nodiscard]] friend constexpr auto operator<=>(const basic_symbol_text& lhs,
const basic_symbol_text<N2, M2>& rhs) noexcept
[[nodiscard]] friend constexpr auto operator<=>(const symbol_text& lhs, const symbol_text<N2, M2>& rhs) noexcept
{
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_ZERO_AS_NULLPOINTER_CONSTANT
Expand All @@ -141,25 +139,24 @@ struct basic_symbol_text {
}

template<std::size_t N2, std::size_t M2>
[[nodiscard]] friend constexpr bool operator==(const basic_symbol_text& lhs,
const basic_symbol_text<N2, M2>& rhs) noexcept
[[nodiscard]] friend constexpr bool operator==(const symbol_text& lhs, const symbol_text<N2, M2>& rhs) noexcept
{
return lhs.unicode() == rhs.unicode() && lhs.ascii() == rhs.ascii();
}
};

basic_symbol_text(char) -> basic_symbol_text<1, 1>;
symbol_text(char) -> symbol_text<1, 1>;

template<std::size_t N>
basic_symbol_text(const char (&)[N]) -> basic_symbol_text<N - 1, N - 1>;
symbol_text(const char (&)[N]) -> symbol_text<N - 1, N - 1>;

template<std::size_t N>
basic_symbol_text(const fixed_string<N>&) -> basic_symbol_text<N, N>;
symbol_text(const fixed_string<N>&) -> symbol_text<N, N>;

template<std::size_t N, std::size_t M>
basic_symbol_text(const char8_t (&)[N], const char (&)[M]) -> basic_symbol_text<N - 1, M - 1>;
symbol_text(const char8_t (&)[N], const char (&)[M]) -> symbol_text<N - 1, M - 1>;

template<std::size_t N, std::size_t M>
basic_symbol_text(const fixed_u8string<N>&, const fixed_string<M>&) -> basic_symbol_text<N, M>;
symbol_text(const fixed_u8string<N>&, const fixed_string<M>&) -> symbol_text<N, M>;

} // namespace mp_units
20 changes: 10 additions & 10 deletions src/core/include/mp-units/bits/text_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ inline constexpr basic_fixed_string superscript_number<8> = u8"\u2078";
template<>
inline constexpr basic_fixed_string superscript_number<9> = u8"\u2079";

inline constexpr basic_symbol_text superscript_minus(u8"\u207b", "-");
inline constexpr symbol_text superscript_minus(u8"\u207b", "-");

inline constexpr basic_symbol_text superscript_prefix(u8"", "^");
inline constexpr symbol_text superscript_prefix(u8"", "^");

template<std::intmax_t Value>
[[nodiscard]] consteval auto superscript_helper()
{
if constexpr (Value < 0)
return superscript_minus + superscript_helper<-Value>();
else if constexpr (Value < 10)
return basic_symbol_text(superscript_number<Value>, basic_fixed_string(static_cast<char>('0' + Value)));
return symbol_text(superscript_number<Value>, basic_fixed_string(static_cast<char>('0' + Value)));
else
return superscript_helper<Value / 10>() + superscript_helper<Value % 10>();
}
Expand All @@ -81,9 +81,9 @@ template<std::intmax_t Value>
[[nodiscard]] consteval auto regular()
{
if constexpr (Value < 0)
return basic_symbol_text("-") + superscript_helper<-Value>();
return symbol_text("-") + superscript_helper<-Value>();
else if constexpr (Value < 10)
return basic_symbol_text(static_cast<char>('0' + Value));
return symbol_text(static_cast<char>('0' + Value));
else
return regular<Value / 10>() + regular<Value % 10>();
}
Expand All @@ -99,7 +99,7 @@ MP_UNITS_EXPORT enum class text_encoding : std::int8_t {
namespace detail {

template<typename CharT, std::size_t N, std::size_t M, std::output_iterator<CharT> Out>
constexpr Out copy(const basic_symbol_text<N, M>& txt, text_encoding encoding, Out out)
constexpr Out copy(const symbol_text<N, M>& txt, text_encoding encoding, Out out)
{
if (encoding == text_encoding::unicode) {
if constexpr (is_same_v<CharT, char8_t>)
Expand All @@ -118,7 +118,7 @@ constexpr Out copy(const basic_symbol_text<N, M>& txt, text_encoding encoding, O
}

template<typename CharT, std::size_t N, std::size_t M, std::output_iterator<CharT> Out>
constexpr Out copy_symbol(const basic_symbol_text<N, M>& txt, text_encoding encoding, bool negative_power, Out out)
constexpr Out copy_symbol(const symbol_text<N, M>& txt, text_encoding encoding, bool negative_power, Out out)
{
out = copy<CharT>(txt, encoding, out);
if (negative_power) {
Expand All @@ -135,12 +135,12 @@ constexpr Out copy_symbol_exponent(text_encoding encoding, bool negative_power,
if constexpr (r.den != 1) {
// add root part
if (negative_power) {
constexpr auto txt = basic_symbol_text("^-(") + regular<r.num>() + basic_symbol_text("/") + regular<r.den>() +
basic_symbol_text(")");
constexpr auto txt =
symbol_text("^-(") + regular<r.num>() + symbol_text("/") + regular<r.den>() + symbol_text(")");
return copy<CharT>(txt, encoding, out);
} else {
constexpr auto txt =
basic_symbol_text("^(") + regular<r.num>() + basic_symbol_text("/") + regular<r.den>() + basic_symbol_text(")");
symbol_text("^(") + regular<r.num>() + symbol_text("/") + regular<r.den>() + symbol_text(")");
return copy<CharT>(txt, encoding, out);
}
} else if constexpr (r.num != 1) {
Expand Down
14 changes: 7 additions & 7 deletions src/core/include/mp-units/bits/unit_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ concept Unit = detail::is_unit<T>::value;
template<Magnitude auto M, Unit U>
struct scaled_unit;

MP_UNITS_EXPORT template<basic_symbol_text Symbol, auto...>
MP_UNITS_EXPORT template<symbol_text Symbol, auto...>
struct named_unit;

namespace detail {

template<basic_symbol_text Symbol, auto... Args>
template<symbol_text Symbol, auto... Args>
void to_base_specialization_of_named_unit(const volatile named_unit<Symbol, Args...>*);

template<typename T>
Expand All @@ -64,7 +64,7 @@ inline constexpr bool is_derived_from_specialization_of_named_unit =
template<typename T>
inline constexpr bool is_specialization_of_named_unit = false;

template<basic_symbol_text Symbol, auto... Args>
template<symbol_text Symbol, auto... Args>
inline constexpr bool is_specialization_of_named_unit<named_unit<Symbol, Args...>> = true;

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ concept DerivedUnitExpr = Unit<T> || detail::is_power_of_unit<T> || detail::is_p
template<detail::DerivedUnitExpr... Expr>
struct derived_unit;

MP_UNITS_EXPORT template<basic_symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
MP_UNITS_EXPORT template<symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
requires(!Symbol.empty())
struct prefixed_unit;

Expand All @@ -123,7 +123,7 @@ namespace detail {
template<auto M, typename U>
void is_unit_impl(const scaled_unit<M, U>*);

template<basic_symbol_text Symbol, auto... Args>
template<symbol_text Symbol, auto... Args>
void is_unit_impl(const named_unit<Symbol, Args...>*);

template<typename... Expr>
Expand All @@ -132,13 +132,13 @@ void is_unit_impl(const derived_unit<Expr...>*);
template<typename T>
inline constexpr bool is_specialization_of_unit = false;

template<basic_symbol_text Symbol, auto... Args>
template<symbol_text Symbol, auto... Args>
inline constexpr bool is_specialization_of_unit<named_unit<Symbol, Args...>> = true;

template<typename T>
inline constexpr bool is_specialization_of_prefixed_unit = false;

template<basic_symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
template<symbol_text Symbol, Magnitude auto M, PrefixableUnit auto U>
inline constexpr bool is_specialization_of_prefixed_unit<prefixed_unit<Symbol, M, U>> = true;

template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/mp-units/dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace mp_units {
*
* @tparam Symbol an unique identifier of the base dimension used to provide dimensional analysis support
*/
MP_UNITS_EXPORT template<basic_symbol_text Symbol>
MP_UNITS_EXPORT template<symbol_text Symbol>
struct base_dimension {
static constexpr auto symbol = Symbol; ///< Unique base dimension identifier
};
Expand Down
Loading

0 comments on commit fb97c2e

Please sign in to comment.