emlabcpp
modern opinionated embedded C++ library
streams.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../enum.h"
27 #include "../range.h"
28 #include "./base.h"
29 #include "./register_map.h"
30 
31 #include <iomanip>
32 
33 namespace emlabcpp::protocol
34 {
35 template < typename T >
36 struct msg_format
37 {
38  T item;
39 };
40 
41 template < typename T >
42 static void pretty_print_msg_format( auto&& w, msg_format< T > wrapper )
43 {
44  // TODO: this might benefit from some refactoring?
45  static constexpr char hex_chars[16] = {
46  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
47 
48  char l = '|';
49  std::size_t i = 0;
50  for ( std::byte const b : wrapper.item ) {
51  if ( i % 4 == 0 )
52  l = '|';
53  auto const val = std::to_integer< uint8_t >( b );
54  w( l );
55  w( hex_chars[val / 16] );
56  w( hex_chars[val % 16] );
57  l = ':';
58 
59  i++;
60  }
61 }
62 
63 #ifdef EMLABCPP_USE_OSTREAM
64 template < std::size_t N >
65 inline std::ostream& operator<<( std::ostream& os, message< N > const& m )
66 {
68  [&]( auto c ) {
69  os << c;
70  },
71  msg_format{ m } );
72  return os;
73 }
74 
75 inline std::ostream& operator<<( std::ostream& os, mark const& m )
76 {
77  return os << std::string_view{ m.data(), m.size() };
78 }
79 
80 inline std::ostream& operator<<( std::ostream& os, error_record const& rec )
81 {
82  return os << rec.error_mark << '(' << rec.offset << ')';
83 }
84 
85 inline std::ostream& operator<<( std::ostream& os, std::endian const& val )
86 {
87  switch ( val ) {
88  case std::endian::big:
89  return os << "big endian";
90  case std::endian::little:
91  return os << "little endian";
92  }
93  return os;
94 }
95 #endif
96 } // namespace emlabcpp::protocol
MIT License.
Definition: multiplexer.h:33
string_buffer< mark_size > mark
Definition: error.h:36
static void pretty_print_msg_format(auto &&w, msg_format< T > wrapper)
Definition: streams.h:42
T item
Definition: streams.h:38
Definition: streams.h:37
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:112
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118