[protocol] Packets now use std::string_view

This commit is contained in:
DankParrot 2020-08-19 17:41:28 -07:00
parent 1e2e6da787
commit a2ac2531bb
5 changed files with 13 additions and 7 deletions

View File

@ -110,7 +110,7 @@ namespace Feather
{
Log::Info("Client Intention Packet: version={}, serverIp={}, port={}, intention={}\n",
handshake.protocolVersion,
handshake.serverIP.c_str(),
handshake.serverIP.data(),
handshake.port,
handshake.intention
);
@ -154,7 +154,8 @@ namespace Feather
// .uuid = { 4658857991808325907ull, 7518717155607718277ull },
// .username = start.username
//};
client.SetUsername(start.username);
// TODO: This is copying...
client.SetUsername(std::string(start.username));
std::string uuid = "ecb99913-96a8-40a7-8529-a2ca6ad95768";
Login::ClientboundSuccess success =

View File

@ -74,11 +74,18 @@ namespace Feather
WriteData(string, stringSize);
}
// Write string using std::string reference
inline void WriteString(const std::string& string)
{
WriteString(string.c_str(), string.length());
}
// Write string using string_view
inline void WriteString(std::string_view view)
{
WriteString(view.data(), view.size());
}
inline void WritePosition(BlockPos pos)
{
Write<int64_t>(pos.Encode());

View File

@ -92,6 +92,7 @@ namespace Feather
return static_cast<T>(result);
}
// can be assigned to string&, string_view
string ReadString()
{
int size = ReadVarInt();

View File

@ -299,10 +299,7 @@ def print_definition(types, name, value, serverbound):
add_text('}} {};', resolved_name)
else:
resolved_type = resolve_type(types, value)
if not serverbound and resolved_type == 'std::string':
add_text('const {}& {};', resolved_type, resolved_name)
else:
add_text('{} {};', resolved_type, resolved_name)
add_text('{} {};', resolved_type, resolved_name)
def get_enum_type(dict, fallback = 'int32'):

View File

@ -20,7 +20,7 @@
}
string: {
type: std::string
type: std::string_view
method: String
// requires count: e.g. string[256]
// generated C type will not be an array