// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #pragma once #include #include #include #include namespace units::detail { template constexpr auto ratio_text() { if constexpr(Ratio::num == 1 && Ratio::den == 1 && Ratio::exp != 0) { return basic_fixed_string("\u00D7 10") + superscript() + basic_fixed_string(" "); } else if constexpr(Ratio::num != 1 || Ratio::den != 1 || Ratio::exp != 0) { auto txt = basic_fixed_string("[") + regular(); if constexpr(Ratio::den == 1) { if constexpr(Ratio::exp == 0) { return txt + basic_fixed_string("] "); } else { return txt + basic_fixed_string(" \u00D7 10") + superscript() + basic_fixed_string("] "); } } else { if constexpr(Ratio::exp == 0) { return txt + basic_fixed_string("/") + regular() + basic_fixed_string("] "); } else { return txt + basic_fixed_string("/") + regular() + basic_fixed_string(" \u00D7 10") + superscript() + basic_fixed_string("] "); } } } else { return basic_fixed_string(""); } } template constexpr auto prefix_or_ratio_text() { if constexpr(Ratio::num == 1 && Ratio::den == 1 && Ratio::exp == 0) { // no ratio/prefix return basic_fixed_string(""); } else { if constexpr (!std::is_same_v) { // try to form a prefix using prefix = downcast>; if constexpr(!std::is_same_v>) { // print as a prefixed unit return prefix::symbol; } else { // print as a ratio of the coherent unit return ratio_text(); } } else { // print as a ratio of the coherent unit return ratio_text(); } } } template constexpr auto derived_dimension_unit_text(exp_list, std::index_sequence) { constexpr auto neg_exp = negative_exp_count; return (exp_text::symbol, neg_exp, Idxs>() + ...); } template constexpr auto derived_dimension_unit_text(exp_list list) { return derived_dimension_unit_text(list, std::index_sequence_for()); } template constexpr auto exp_list_with_named_units(exp_list); template constexpr auto exp_list_with_named_units(Exp) { using dim = Exp::dimension; if constexpr(dimension_unit::is_named) { return exp_list(); } else { using recipe = dim::recipe; return exp_list_with_named_units(recipe()); } } template constexpr auto exp_list_with_named_units(exp_list) { return type_list_join(); } template constexpr auto derived_dimension_unit_text() { using recipe = Dim::recipe; return derived_dimension_unit_text(exp_list_with_named_units(recipe())); } // TODO Inline below concept when switched to gcc-10 template concept has_symbol = requires{ T::symbol; }; template constexpr auto unit_text() { if constexpr(has_symbol) { // already has a symbol so print it return U::symbol; } else { // print as a prefix or ratio of a coherent unit using coherent_unit = dimension_unit; using ratio = ratio_divide; auto prefix_txt = prefix_or_ratio_text(); if constexpr(has_symbol) { // use predefined coherent unit symbol return prefix_txt + coherent_unit::symbol; } else { // use derived dimension ingredients to create a unit symbol return prefix_txt + derived_dimension_unit_text(); } } } } // namespace units::detail