Skip to content

Commit

Permalink
refactoring: binary type std::vector< char> -> std::vector< std::byte>
Browse files Browse the repository at this point in the history
This is a major refactoring, since we use platform::binary::type in a
lot of places.

Why? To have a distinct type that represent binary is clearer and less
error prone. std::as_bytes can be used seamless to handle trivially
copyable types to and from binary.
Also the binary type does not look like a string-like type, and should
not be used as such (which we did/do) in some places.

A lot of hacking in `buffer` (fielded & CO) where the implementation
is very C:ish. I didn't want to rewrite the whole thing...
  • Loading branch information
lazan committed Jun 28, 2024
1 parent a7b55dc commit 6c8dfee
Show file tree
Hide file tree
Showing 78 changed files with 829 additions and 668 deletions.
51 changes: 28 additions & 23 deletions middleware/buffer/sample/protocol/source/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace casual
float v_float = 1 / float( 42);
double v_double = 1 / double( 42);
std::string v_string = "casual";
platform::binary::type v_binary = { '1', '2', '3', '1', '2', '3', '1', '2', '3', '1', '2', '3'};
platform::binary::type v_binary = { std::byte{ '1'}, std::byte{ '2'}, std::byte{ '3'}, std::byte{ '1'}, std::byte{ '2'}, std::byte{ '3'}, std::byte{ '1'}, std::byte{ '2'}, std::byte{ '3'}, std::byte{ '1'}, std::byte{ '2'}, std::byte{ '3'}};

auto net_char() const { return common::network::byteorder::encode( v_char);}
auto net_short() const { return common::network::byteorder::encode( v_short);}
Expand Down Expand Up @@ -69,7 +69,8 @@ namespace casual
casual_field_add_float( &buffer, FLD_FLOAT, value.v_float);
casual_field_add_double( &buffer, FLD_DOUBLE, value.v_double);
casual_field_add_string( &buffer, FLD_STRING, value.v_string.c_str());
casual_field_add_binary( &buffer, FLD_BINARY, value.v_binary.data(), value.v_binary.size());
auto string_like = common::view::binary::to_string_like( value.v_binary);
casual_field_add_binary( &buffer, FLD_BINARY, string_like.data(), string_like.size());

long size = 0;
long used = 0;
Expand Down Expand Up @@ -111,17 +112,19 @@ Every field in the buffer has the following parts: `<field-id><size><data>`
)";
Value value;

{
out << std::fixed;
out << "char | " << FLD_CHAR << " | " << sizeof( value.v_char) << " | " << value.v_char << '\n';
out << "short | " << FLD_SHORT << " | " << sizeof( value.v_short) << " | " << value.v_short << '\n';
out << "long | " << FLD_LONG << " | " << sizeof( value.v_long) << " | " << value.v_long << '\n';
out << "float | " << FLD_FLOAT << " | " << sizeof( value.v_float) << " | " << std::setprecision( std::numeric_limits<float>::digits10 + 1) << value.v_float << '\n';
out << "double | " << FLD_DOUBLE << " | " << sizeof( value.v_double) << " | " << std::setprecision( std::numeric_limits<double>::digits10 + 1) << value.v_double << '\n';
out << "string | " << FLD_STRING << " | " << value.v_string.size() << " | " << value.v_string << '\n';
auto string_like = common::view::binary::to_string_like( value.v_binary);
out << "binary | " << FLD_BINARY << " | " << string_like.size() << " | "; out.write( string_like.data(), string_like.size()) ;out << '\n';

out << std::fixed;
out << "char | " << FLD_CHAR << " | " << sizeof( value.v_char) << " | " << value.v_char << '\n';
out << "short | " << FLD_SHORT << " | " << sizeof( value.v_short) << " | " << value.v_short << '\n';
out << "long | " << FLD_LONG << " | " << sizeof( value.v_long) << " | " << value.v_long << '\n';
out << "float | " << FLD_FLOAT << " | " << sizeof( value.v_float) << " | " << std::setprecision( std::numeric_limits<float>::digits10 + 1) << value.v_float << '\n';
out << "double | " << FLD_DOUBLE << " | " << sizeof( value.v_double) << " | " << std::setprecision( std::numeric_limits<double>::digits10 + 1) << value.v_double << '\n';
out << "string | " << FLD_STRING << " | " << value.v_string.size() << " | " << value.v_string << '\n';
out << "binary | " << FLD_BINARY << " | " << value.v_binary.size() << " | "; out.write( value.v_binary.data(), value.v_binary.size()) ;out << '\n';

out << '\n';
out << '\n';
}

out << R"(
Expand All @@ -131,17 +134,19 @@ Every field in the buffer has the following parts: `<field-id><size><data>`
--------|--------------|--------------|-------------
)";


out << std::fixed;
out << "char | " << encode( FLD_CHAR) << " | " << encode( sizeof( value.net_char())) << " | " << value.net_char() << '\n';
out << "short | " << encode( FLD_SHORT) << " | " << encode( sizeof( value.net_short())) << " | " << value.net_short() << '\n';
out << "long | " << encode( FLD_LONG ) << " | " << encode( sizeof( value.net_long())) << " | " << value.net_long() << '\n';
out << "float | " << encode( FLD_FLOAT) << " | " << encode( sizeof( value.net_float())) << " | " << value.net_float() << '\n';
out << "double | " << encode( FLD_DOUBLE) << " | " << encode( sizeof( value.net_double())) << " | " << value.net_double() << '\n';
out << "string | " << encode( FLD_STRING) << " | " << encode( value.v_string.size()) << " | " << value.v_string << '\n';
out << "binary | " << encode( FLD_BINARY) << " | " << encode( value.v_binary.size()) << " | "; out.write( value.v_binary.data(), value.v_binary.size()); out << '\n';

out << '\n';
{
out << std::fixed;
out << "char | " << encode( FLD_CHAR) << " | " << encode( sizeof( value.net_char())) << " | " << value.net_char() << '\n';
out << "short | " << encode( FLD_SHORT) << " | " << encode( sizeof( value.net_short())) << " | " << value.net_short() << '\n';
out << "long | " << encode( FLD_LONG ) << " | " << encode( sizeof( value.net_long())) << " | " << value.net_long() << '\n';
out << "float | " << encode( FLD_FLOAT) << " | " << encode( sizeof( value.net_float())) << " | " << value.net_float() << '\n';
out << "double | " << encode( FLD_DOUBLE) << " | " << encode( sizeof( value.net_double())) << " | " << value.net_double() << '\n';
out << "string | " << encode( FLD_STRING) << " | " << encode( value.v_string.size()) << " | " << value.v_string << '\n';
auto string_like = common::view::binary::to_string_like( value.v_binary);
out << "binary | " << encode( FLD_BINARY) << " | " << encode( string_like.size()) << " | "; out.write( string_like.data(), string_like.size()); out << '\n';

out << '\n';
}
}

int main( int argc, char **argv)
Expand Down
5 changes: 3 additions & 2 deletions middleware/buffer/source/admin/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ if --verbose is provided the type of the buffer will be sent to stderr.
message.payload.type = type ? *type : common::buffer::type::x_octet;

while( std::cin.peek() != std::istream::traits_type::eof())
message.payload.data.push_back( std::cin.get());
message.payload.data.push_back( static_cast< std::byte>( std::cin.get()));

communication::stream::outbound::Device out{ std::cout};
communication::device::blocking::send( out, message);
Expand Down Expand Up @@ -216,7 +216,8 @@ if --verbose is provided the type of the buffer will be sent to stderr.
if( terminal::output::directive().verbose())
std::cerr << message.payload.type << '\n';

std::cout.write( message.payload.data.data(), message.payload.data.size());
auto string_like = view::binary::to_string_like( message.payload.data);
std::cout.write(string_like.data(), string_like.size());
}),
std::ref( done)
);
Expand Down
Loading

0 comments on commit 6c8dfee

Please sign in to comment.