Просмотр исходного кода

Initial functioning of a nested message

Bart Hertog 6 лет назад
Родитель
Сommit
18c741d9a9
6 измененных файлов с 108 добавлено и 66 удалено
  1. 2 2
      build_test.sh
  2. 7 6
      generator/Header_Template.h
  3. 0 1
      src/WireFormatter.h
  4. 3 3
      test/proto/nested_message.proto
  5. 2 2
      test/test_SimpleTypes.cpp
  6. 94 52
      test_data.py

+ 2 - 2
build_test.sh

@@ -7,8 +7,8 @@ protoc --plugin=protoc-gen-eams=protoc-gen-eams -I./test/proto --eams_out=./buil
 
 # For validation and testing generate the same message using python
 mkdir -p ./build/python
-protoc -I./test/proto --cpp_out=./build/python ./test/proto/simple_types.proto
-protoc -I./test/proto --cpp_out=./build/python ./test/proto/nested_message.proto
+protoc -I./test/proto --python_out=./build/python ./test/proto/simple_types.proto
+protoc -I./test/proto --python_out=./build/python ./test/proto/nested_message.proto
 
 
 # Build the tests

+ 7 - 6
generator/Header_Template.h

@@ -46,10 +46,12 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
       {% for field in msg.fields() %}
       {% if field.of_type_message %}
       const uint32_t size_{{field.name}} = {{field.variable_name}}.serialized_size();
-      if((0 < size_{{field.name}}) && (size_{{field.name}} <= buffer.get_available_size()) && result)
+      result = (size_{{field.name}} <= buffer.get_available_size());
+      if(result && (0 < size_{{field.name}}))
       {
-        result = ::EmbeddedProto::WireFormatter::WriteVarint32ToArray({{field.variable_id_name}}, ::EmbeddedProto::WireFormatter::WireType::{{field.wire_type}}, buffer);
-        result = result && ::EmbeddedProto::WireFormatter::UIntNoTag(size_{{field.name}}, buffer);
+        uint32_t tag = ::EmbeddedProto::WireFormatter::MakeTag({{field.variable_id_name}}, ::EmbeddedProto::WireFormatter::WireType::{{field.wire_type}});
+        result = ::EmbeddedProto::WireFormatter::SerializeVarint(tag, buffer);
+        result = result && ::EmbeddedProto::WireFormatter::SerializeVarint(size_{{field.name}}, buffer);
         result = result && {{field.variable_name}}.serialize(buffer);
       }
       {% else %}
@@ -80,9 +82,8 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
             {
               {% if field.of_type_message %}
               uint32_t size;
-              result = deserialize_VARINT(buffer, size);
-              PartialMessageBufferInterface<size> partial_buffer(buffer);
-              result = result && {{field.variable_name}}.deserialize(partial_buffer);
+              result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
+              result = result && {{field.variable_name}}.deserialize(buffer);
               {% else %}
               result = ::EmbeddedProto::WireFormatter::{{field.deserialization_func}}(buffer, {{field.variable_name}});
               {% endif %}

+ 0 - 1
src/WireFormatter.h

@@ -274,7 +274,6 @@ namespace EmbeddedProto
 
       /** @} **/
 
-    private:
 
       //! This function converts a given value unsigned integer to a varint formated data buffer.
       /*!

+ 3 - 3
test/proto/nested_message.proto

@@ -12,8 +12,8 @@ message message_a
 
 message message_b 
 {
-  double a  = 1;
-  double b  = 2;
-  message_a msg_c = 3;
+  double u  = 1;
+  double v  = 2;
+  message_a nested_a = 3;
 }
 

+ 2 - 2
test/test_SimpleTypes.cpp

@@ -108,8 +108,8 @@ TEST(SimpleTypes, serialize_max)
   msg.set_a_float(std::numeric_limits<float>::max());
 
 
-  uint8_t expected[] = {0x08, 0xFF, 0xFF, 0xFF, 0xFF, 
-                        0x07, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
+  uint8_t expected[] = {0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 
+                        0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
                         0x7F, 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 
                         0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 
                         0x28, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F, 

+ 94 - 52
test_data.py

@@ -1,54 +1,96 @@
 import build.python.simple_types_pb2 as st
+import build.python.nested_message_pb2 as nm
 
-msg = st.Test_Simple_Types()
-# msg.a_int32 = -2147483648
-# msg.a_int64 = -9223372036854775808
-# msg.a_uint32 = 0
-# msg.a_uint64 = 0
-# msg.a_sint32 = -2147483648
-# msg.a_sint64 = -9223372036854775808
-# msg.a_bool = 0
-# msg.a_enum = 0
-# msg.a_fixed64 = 0
-# msg.a_sfixed64 = -9223372036854775808
-# msg.a_double = 0
-# msg.a_fixed32 = 0
-# msg.a_sfixed32 = -2147483648
-# msg.a_float = 0
-
-# msg.a_int32 = 1
-# msg.a_int64 = 1
-# msg.a_uint32 = 1
-# msg.a_uint64 = 1
-# msg.a_sint32 = 1
-# msg.a_sint64 = 1
-# msg.a_bool = 1
-# msg.a_enum = 1
-# msg.a_fixed64 = 1
-# msg.a_sfixed64 = 1
-# msg.a_double = 1
-# msg.a_fixed32 = 1
-# msg.a_sfixed32 = 1
-# msg.a_float = 1
-
-msg.a_double = pow(2, -1022)
-msg.a_float = pow(2, -126)
-
-str = ""
-msg_str = msg.SerializeToString()
-print(len(msg_str))
-print(msg_str)
-for x in msg_str:
-    str += "0x{:02x}, ".format(x)
-
-print(str)
-print()
-
-
-x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
-               0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
-               0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
-msg2 = st.Test_Simple_Types()
-msg2.ParseFromString(x)
-
-print(msg2)
+def test_simple_types():
+    # A test function used to generate encoded data to test the implementation of the wireformatter
+    # and header template.
+
+    msg = st.Test_Simple_Types()
+    # msg.a_int32 = -2147483648
+    # msg.a_int64 = -9223372036854775808
+    # msg.a_uint32 = 0
+    # msg.a_uint64 = 0
+    # msg.a_sint32 = -2147483648
+    # msg.a_sint64 = -9223372036854775808
+    # msg.a_bool = 0
+    # msg.a_enum = 0
+    # msg.a_fixed64 = 0
+    # msg.a_sfixed64 = -9223372036854775808
+    # msg.a_double = 0
+    # msg.a_fixed32 = 0
+    # msg.a_sfixed32 = -2147483648
+    # msg.a_float = 0
+
+    # msg.a_int32 = 1
+    # msg.a_int64 = 1
+    # msg.a_uint32 = 1
+    # msg.a_uint64 = 1
+    # msg.a_sint32 = 1
+    # msg.a_sint64 = 1
+    # msg.a_bool = 1
+    # msg.a_enum = 1
+    # msg.a_fixed64 = 1
+    # msg.a_sfixed64 = 1
+    # msg.a_double = 1
+    # msg.a_fixed32 = 1
+    # msg.a_sfixed32 = 1
+    # msg.a_float = 1
+
+    msg.a_double = pow(2, -1022)
+    msg.a_float = pow(2, -126)
+
+    str = ""
+    msg_str = msg.SerializeToString()
+    print(len(msg_str))
+    print(msg_str)
+    for x in msg_str:
+      str += "0x{:02x}, ".format(x)
+
+    print(str)
+    print()
+
+
+    x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
+                 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
+                 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
+    msg2 = st.Test_Simple_Types()
+    msg2.ParseFromString(x)
+
+    print(msg2)
+
+
+def test_nested_message():
+    msg = nm.message_b()
+
+    # msg.u = 1.0
+    # msg.v = 1.0
+    # msg.nested_a.x = 1
+    # msg.nested_a.y = 1.0
+    # msg.nested_a.z = 1
+
+    msg.u = 0 #pow(2, 1023)
+    msg.v = 0 #pow(2, 1023)
+    msg.nested_a.x = pow(2, 31) - 1
+    msg.nested_a.y = 0 #1.0
+    msg.nested_a.z = 0 #1
+
+    str = ""
+    msg_str = msg.SerializeToString()
+    print(len(msg_str))
+    print(msg_str)
+    for x in msg_str:
+      str += "0x{:02x}, ".format(x)
+
+    print(str)
+    print()
+
+    x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
+                   0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
+
+    msg2 = nm.message_b()
+    msg2.ParseFromString(x)
+
+    print(msg2)
+
+
+test_nested_message()