summaryrefslogtreecommitdiff
path: root/modules/codec-unit/c++/unit_print.hpp
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2025-04-20 17:12:55 +0200
committerClaudius "keldu" Holeksa <mail@keldu.de>2025-04-20 17:12:55 +0200
commit0d32f9b2c73690fa188d0f5920dfa9cbe3005200 (patch)
tree7728773c6309e5977fe7d0efd1c718c33147316b /modules/codec-unit/c++/unit_print.hpp
parentd659724e48ce4a703a99b3414b97d582da7cd173 (diff)
Added Unit printing
Diffstat (limited to 'modules/codec-unit/c++/unit_print.hpp')
-rw-r--r--modules/codec-unit/c++/unit_print.hpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/modules/codec-unit/c++/unit_print.hpp b/modules/codec-unit/c++/unit_print.hpp
new file mode 100644
index 0000000..724e9d6
--- /dev/null
+++ b/modules/codec-unit/c++/unit_print.hpp
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <iostream>
+
+namespace std {
+template<typename UT, int64_t UE>
+inline ostream& operator<<(ostream& o, const saw::schema::UnitElement<UT,UE>&){
+ o<<UT::short_name;
+ if constexpr (UE != 1){
+ o<<'^'<<'('<<UE<<')';
+ }
+ return o;
+}
+}
+
+namespace saw {
+namespace impl {
+template<typename... T>
+struct unit_print {
+ static_assert(always_false<T...>, "Template type not supported");
+};
+
+template<typename UT, int64_t UE, typename... UTL, int64_t... UEL>
+struct unit_print<schema::UnitElement<UT,UE>, schema::UnitElement<UTL,UEL>...> {
+ static std::ostream& print(std::ostream& o){
+ schema::UnitElement<UT,UE> element;
+
+ std::ostream& o_ret = o << element;
+
+ if constexpr (sizeof...(UTL) > 0) {
+ std::ostream& o_ret_next = o_ret << ' ' << '*' << ' ';
+ return unit_print<schema::UnitElement<UTL,UEL>...>::print(o_ret_next);
+ }
+
+ return o_ret<<']';
+ }
+};
+}
+}
+
+namespace std {
+template<typename Store, typename Encode, typename... T>
+inline ostream& operator<<(ostream& o, const saw::data<saw::schema::Unit<Store,T...>, Encode>& unit);
+
+template<typename StorageT, typename Encode, typename... UnitT, int64_t... UnitE>
+inline ostream& operator<<(ostream& o, const saw::data<saw::schema::Unit<StorageT,saw::schema::UnitElement<UnitT,UnitE>...>, Encode>& unit){
+ o << unit.handle().get();
+ if constexpr (sizeof...(UnitT) > 0) {
+ auto& o_ret = o << ' '<<'[';
+ return saw::impl::unit_print<saw::schema::UnitElement<UnitT,UnitE>...>::print(o_ret);
+ }
+ return o;
+}
+}