From 7a647dffca69ba256a0b3ce57e3480dbd2caae79 Mon Sep 17 00:00:00 2001 From: Mislav Bradac Date: Wed, 22 Feb 2017 11:33:46 +0100 Subject: [PATCH] Add overloads for Code and cast enums explicitly Summary: Cast pack::Code enum to byte and size_t where neccessary. g++ is confused by some of implicit casts when function has different valid overloads. There is still problem in states/init.cpp but that file will disappear after bolt is refactored. Next step towards safe code is to make Code and Rule enum class. Reviewers: buda, mferencevic Reviewed By: buda Subscribers: pullbot, mferencevic, buda Differential Revision: https://phabricator.memgraph.io/D58 --- .../bolt/v1/serialization/bolt_serializer.cpp | 4 +- .../bolt/v1/transport/bolt_encoder.hpp | 54 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/communication/bolt/v1/serialization/bolt_serializer.cpp b/src/communication/bolt/v1/serialization/bolt_serializer.cpp index 49008a80b..9a5fb8b25 100644 --- a/src/communication/bolt/v1/serialization/bolt_serializer.cpp +++ b/src/communication/bolt/v1/serialization/bolt_serializer.cpp @@ -12,7 +12,7 @@ template void bolt::BoltSerializer::write(const VertexAccessor &vertex) { // write signatures for the node struct and node data type encoder.write_struct_header(3); - encoder.write(underlying_cast(pack::Node)); + encoder.write(underlying_cast(pack::Code::Node)); // IMPORTANT: here we write a hardcoded 0 because we don't // use internal IDs, but need to give something to Bolt @@ -40,7 +40,7 @@ template void bolt::BoltSerializer::write(const EdgeAccessor &edge) { // write signatures for the edge struct and edge data type encoder.write_struct_header(5); - encoder.write(underlying_cast(pack::Relationship)); + encoder.write(underlying_cast(pack::Code::Relationship)); // IMPORTANT: here we write a hardcoded 0 because we don't // use internal IDs, but need to give something to Bolt diff --git a/src/communication/bolt/v1/transport/bolt_encoder.hpp b/src/communication/bolt/v1/transport/bolt_encoder.hpp index fdd54ddb3..6364618b9 100644 --- a/src/communication/bolt/v1/transport/bolt_encoder.hpp +++ b/src/communication/bolt/v1/transport/bolt_encoder.hpp @@ -26,6 +26,7 @@ class BoltEncoder { } void write(byte value) { write_byte(value); } + void write(pack::Code value) { write_byte(static_cast(value)); } void write_byte(byte value) { logger.trace("write byte: {}", value); @@ -34,7 +35,7 @@ class BoltEncoder { void write(const byte *values, size_t n) { stream.write(values, n); } - void write_null() { stream.write(pack::Null); } + void write_null() { stream.write(static_cast(pack::Code::Null)); } void write(bool value) { write_bool(value); } @@ -45,9 +46,9 @@ class BoltEncoder { write_false(); } - void write_true() { stream.write(pack::True); } + void write_true() { stream.write(pack::Code::True); } - void write_false() { stream.write(pack::False); } + void write_false() { stream.write(pack::Code::False); } template void write_value(T value) { @@ -59,16 +60,16 @@ class BoltEncoder { if (value >= minus_2_to_the_4 && value < plus_2_to_the_7) { write(static_cast(value)); } else if (value >= minus_2_to_the_7 && value < minus_2_to_the_4) { - write(pack::Int8); + write(pack::Code::Int8); write(static_cast(value)); } else if (value >= minus_2_to_the_15 && value < plus_2_to_the_15) { - write(pack::Int16); + write(pack::Code::Int16); write_value(static_cast(value)); } else if (value >= minus_2_to_the_31 && value < plus_2_to_the_31) { - write(pack::Int32); + write(pack::Code::Int32); write_value(static_cast(value)); } else { - write(pack::Int64); + write(pack::Code::Int64); write_value(value); } } @@ -76,55 +77,57 @@ class BoltEncoder { void write(double value) { write_double(value); } void write_double(double value) { - write(pack::Float64); + write(pack::Code::Float64); write_value(*reinterpret_cast(&value)); } void write_map_header(size_t size) { if (size < 0x10) { - write(static_cast(pack::TinyMap | size)); + write(static_cast(static_cast(pack::Code::TinyMap) | size)); } else if (size <= 0xFF) { - write(pack::Map8); + write(pack::Code::Map8); write(static_cast(size)); } else if (size <= 0xFFFF) { - write(pack::Map16); + write(pack::Code::Map16); write_value(size); } else { - write(pack::Map32); + write(pack::Code::Map32); write_value(size); } } - void write_empty_map() { write(pack::TinyMap); } + void write_empty_map() { write(pack::Code::TinyMap); } void write_list_header(size_t size) { if (size < 0x10) { - write(static_cast(pack::TinyList | size)); + write( + static_cast(static_cast(pack::Code::TinyList) | size)); } else if (size <= 0xFF) { - write(pack::List8); + write(pack::Code::List8); write(static_cast(size)); } else if (size <= 0xFFFF) { - write(pack::List16); + write(pack::Code::List16); write_value(size); } else { - write(pack::List32); + write(pack::Code::List32); write_value(size); } } - void write_empty_list() { write(pack::TinyList); } + void write_empty_list() { write(pack::Code::TinyList); } void write_string_header(size_t size) { if (size < 0x10) { - write(static_cast(pack::TinyString | size)); + write( + static_cast(static_cast(pack::Code::TinyString) | size)); } else if (size <= 0xFF) { - write(pack::String8); + write(pack::Code::String8); write(static_cast(size)); } else if (size <= 0xFFFF) { - write(pack::String16); + write(pack::Code::String16); write_value(size); } else { - write(pack::String32); + write(pack::Code::String32); write_value(size); } } @@ -142,12 +145,13 @@ class BoltEncoder { void write_struct_header(size_t size) { if (size < 0x10) { - write(static_cast(pack::TinyStruct | size)); + write(static_cast(static_cast(pack::Code::TinyStruct) | + size)); } else if (size <= 0xFF) { - write(pack::Struct8); + write(pack::Code::Struct8); write(static_cast(size)); } else { - write(pack::Struct16); + write(pack::Code::Struct16); write_value(size); } }