diff options
Diffstat (limited to 'src/unit_print.h')
-rw-r--r-- | src/unit_print.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/unit_print.h b/src/unit_print.h new file mode 100644 index 0000000..0f3cb72 --- /dev/null +++ b/src/unit_print.h @@ -0,0 +1,55 @@ +#include "./unit.h" + +#include <iostream> + + +namespace std { +template<typename UnitT, int64_t UnitE> +inline ostream& operator<<(ostream& o, const kelun::unit_base<UnitT,UnitE>& ele){ + o<<UnitT::short_name; + if constexpr ( UnitE != 1 ){ + o<<'^'<<'('<<UnitE<<')'; + } + return o; +} +} + +namespace kelun { +template<typename... T> +struct unit_print_impl { + static_assert(is_always_false<T...>, "Template type not supported"); +}; + +template<typename UnitT, int64_t UnitE, typename... UnitTL, int64_t... UnitEL> +struct unit_print_impl<unit_base<UnitT,UnitE>,unit_base<UnitTL,UnitEL>...> { + static std::ostream& print(std::ostream& o){ + + unit_base<UnitT,UnitE> element; + + std::ostream& o_ret = o << element; + + if constexpr (sizeof...(UnitTL) > 0){ + std::ostream& o_ret_next = o_ret << ' ' << '*' << ' '; + return unit_print_impl<unit_base<UnitTL,UnitEL>...>::print(o_ret_next); + } + + return o_ret<<']'; + } +}; +} + +namespace std { + +template<typename StorageT, typename... T> +inline ostream& operator<<(ostream& o, const kelun::unit<StorageT,T...>& unit); + +template<typename StorageT, typename... UnitT, int64_t... UnitE> +inline ostream& operator<<(ostream& o, const kelun::unit<StorageT,kelun::unit_base<UnitT,UnitE>...>& unit){ + o << unit.data(); + if constexpr (sizeof...(UnitT) > 0) { + auto& o_ret = o << ' '<<'['; + return kelun::unit_print_impl<kelun::unit_base<UnitT,UnitE>...>::print(o_ret); + } + return o; +} +} |