summaryrefslogtreecommitdiff
path: root/modules/codec-unit/c++/unit_print.hpp
blob: 724e9d6f502775bc11b2d80beab111f0b4e7e4eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
}
}