Commit f51bca65 authored by Jan Koniarik's avatar Jan Koniarik
Browse files

register_map now has endinaess config and operator<< for it is improved

parent 66b1c4e9
......@@ -23,8 +23,7 @@ struct protocol_register_handler
template < key_type Key >
static message_type serialize( typename map_type::reg_value_type< Key > val )
{
using def =
protocol_def< typename map_type::reg_def_type< Key >, PROTOCOL_BIG_ENDIAN >;
using def = protocol_def< typename map_type::reg_def_type< Key >, Map::endianess >;
std::array< uint8_t, max_size > buffer;
static_assert( def::max_size <= max_size );
......@@ -46,8 +45,7 @@ struct protocol_register_handler
static either< typename map_type::reg_value_type< Key >, protocol_error_record >
extract( const view< const uint8_t* >& msg )
{
using def =
protocol_def< typename map_type::reg_def_type< Key >, PROTOCOL_BIG_ENDIAN >;
using def = protocol_def< typename map_type::reg_def_type< Key >, Map::endianess >;
auto opt_view = bounded_view< const uint8_t*, typename def::size_type >::make(
view_n( msg.begin(), min( def::max_size, msg.size() ) ) );
......
......@@ -28,14 +28,15 @@ struct protocol_reg
// serialization and deserialization of bytes into the values defined in the map. This includes
// additional information that can be accessed about the map. This can also be used as simple table
// of configuration values.
template < typename... Regs >
template < protocol_endianess_enum Endianess, typename... Regs >
class protocol_register_map
{
static_assert( are_same_v< typename Regs::key_type... > );
public:
using registers_tuple = std::tuple< Regs... >;
using key_type = typename std::tuple_element_t< 0, registers_tuple >::key_type;
static constexpr protocol_endianess_enum endianess = Endianess;
using registers_tuple = std::tuple< Regs... >;
using key_type = typename std::tuple_element_t< 0, registers_tuple >::key_type;
static constexpr std::size_t registers_count = sizeof...( Regs );
using register_index = bounded< std::size_t, 0, registers_count - 1 >;
......
......@@ -21,11 +21,14 @@ template < std::size_t N >
inline std::ostream& operator<<( std::ostream& os, const protocol_message< N >& msg )
{
std::ios_base::fmtflags f( os.flags() );
os << std::hex << std::setfill( '0' ) << std::setw( 2 );
std::string l = "";
for ( uint8_t v : msg ) {
os << l << int( v );
l = " ";
os << std::hex;
char l = '|';
for ( std::size_t i : range( msg.size() ) ) {
if ( i % 4 == 0 ) {
l = '|';
}
os << l << std::setfill( '0' ) << std::setw( 2 ) << int( msg[i] );
l = ':';
}
os.flags( f );
return os;
......@@ -55,20 +58,33 @@ inline std::ostream& operator<<( std::ostream& os, const protocol_endianess_enum
return os;
}
template < typename... Regs >
inline std::ostream& operator<<( std::ostream& os, const protocol_register_map< Regs... >& m )
template < protocol_endianess_enum Endianess, typename... Regs >
inline std::ostream&
operator<<( std::ostream& os, const protocol_register_map< Endianess, Regs... >& m )
{
using map = protocol_register_map< Regs... >;
for_each_index< sizeof...( Regs ) >( [&]< std::size_t i >() {
static constexpr auto key = map::register_key( bounded_constant< i > );
const auto& val = m.template get_val< key >();
using map = protocol_register_map< Endianess, Regs... >;
auto key_to_str = []( auto key ) {
#ifdef EMLABCPP_USE_MAGIC_ENUM
os << magic_enum::enum_name( key );
return magic_enum::enum_name( key );
#else
os << key;
return std::to_string( key );
#endif
os << "\t" << val << "\n";
};
std::size_t max_key_size = 0;
for_each_index< sizeof...( Regs ) >( [&]< std::size_t i >() {
static constexpr auto key = map::register_key( bounded_constant< i > );
max_key_size = key_to_str( key ).size();
} );
for_each_index< sizeof...( Regs ) >( [&]< std::size_t i >() {
static constexpr auto key = map::register_key( bounded_constant< i > );
const auto& val = m.template get_val< key >();
os << std::left << std::setw( static_cast< int >( max_key_size ) )
<< key_to_str( key ) << "\t" << val << "\n";
} );
return os;
}
......
......@@ -17,6 +17,7 @@ enum test_keys
};
struct test_map : protocol_register_map<
PROTOCOL_BIG_ENDIAN,
protocol_reg< FOO, uint32_t >,
protocol_reg< WOO, uint32_t >,
protocol_reg< TOO, uint8_t >,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment