summaryrefslogtreecommitdiff
path: root/modules/codec/c++/data_print.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/codec/c++/data_print.hpp')
-rw-r--r--modules/codec/c++/data_print.hpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/modules/codec/c++/data_print.hpp b/modules/codec/c++/data_print.hpp
new file mode 100644
index 0000000..8eba274
--- /dev/null
+++ b/modules/codec/c++/data_print.hpp
@@ -0,0 +1,94 @@
+#pragma once
+
+#include "data.hpp"
+#include <iostream>
+
+namespace saw {
+namespace impl {
+struct data_print_tab_helper {
+ static void apply(std::ostream& o, uint64_t tabs){
+ for(uint64_t i = 0u; i < tabs; ++i){
+ o<<'\t';
+ }
+ }
+};
+
+template<typename Schema, typename Encode>
+struct data_print_std_helper {
+ static void apply(std::ostream& o, const data<Schema,Encode>& dat){
+ // Blueprint
+ (void)o;
+ (void)dat;
+ }
+};
+
+template<typename T, uint64_t N, typename Encode>
+struct data_print_std_helper<schema::Primitive<T,N>, Encode> {
+ static void apply(std::ostream& o, const data<schema::Primitive<T,N>,Encode>& dat, uint64_t tabs){
+ (void) tabs;
+ // Blueprint
+ o<<dat.get();
+ }
+};
+
+template<typename Encode>
+struct data_print_std_helper<schema::String, Encode> {
+ static void apply(std::ostream& o, const data<schema::String,Encode>& dat, uint64_t tabs){
+ (void) tabs;
+ // Blueprint
+ auto size = dat.size();
+ o<<"\"";
+ for(saw::data<schema::UInt64> i{0u}; i < size; ++i){
+ o<<dat.at(i);
+ }
+ o<<"\"";
+ }
+};
+template<typename T, uint64_t N, typename Encode>
+struct data_print_std_helper<schema::Array<T,N>, Encode> {
+ static void apply(std::ostream& o, const data<schema::Array<T,N>,Encode>& dat, uint64_t tabs){
+ (void) tabs;
+ // Blueprint
+ auto size = dat.size();
+ o<<"\"";
+ for(saw::data<schema::UInt64> i{0u}; i < size; ++i){
+ o<<dat.at(i);
+ }
+ o<<"\"";
+ }
+};
+
+template<typename... Members, typename Encode>
+struct data_print_std_helper<schema::Struct<Members...>, Encode> {
+ template<uint64_t i>
+ static void apply(std::ostream& o, const data<schema::Struct<Members...>,Encode>& dat, uint64_t tabs){
+ if constexpr (i < sizeof...(Members)){
+ using Memb = parameter_pack_type<i,Members...>::type;
+
+ data_print_tab_helper::apply(o,tabs);
+ o<<"- \""<<Memb::KeyLiteral.view()<<"\" : ";
+ data_print_std_helper<typename Memb::ValueType,Encode>::apply(o,dat.template get<Memb::KeyLiteral>(), tabs);
+ o<<"\n";
+ apply<i+1u>(o,dat,tabs);
+ }
+ }
+
+ static void apply(std::ostream& o, const data<schema::Struct<Members...>,Encode>& dat, uint64_t tabs){
+ data_print_tab_helper::apply(o,tabs);
+
+ apply<0u>(o,dat,tabs+1u);
+ }
+};
+
+}
+}
+
+namespace std {
+template<typename Schema, typename Encode>
+inline ostream& operator<<(ostream& o, const saw::data<Schema, Encode>& dat){
+
+ saw::impl::data_print_std_helper<Schema,Encode>::apply(o, dat, 0u);
+
+ return o;
+}
+}