Browse Source

Added a test to see if the assignment of a nested message with a repeated field works.

Bart Hertog 5 năm trước cách đây
mục cha
commit
66c0def90c

+ 6 - 0
generator/Header_Template.h

@@ -132,6 +132,11 @@ inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value)
   {{_field.which_oneof}} = id::{{_field.variable_id_name}};
   {{_field.variable_full_name}}.set(index, value);
 }
+inline void set_{{_field.name}}(const {{_field.repeated_type}}& values)
+{
+  {{_field.which_oneof}} = id::{{_field.variable_id_name}};
+  {{_field.variable_full_name}} = values;
+}
 inline void add_{{_field.name}}(const {{_field.type}}& value)
 {
   {{_field.which_oneof}} = id::{{_field.variable_id_name}};
@@ -146,6 +151,7 @@ inline {{_field.repeated_type}}& mutable_{{_field.name}}()
 inline void clear_{{_field.name}}() { {{_field.variable_full_name}}.clear(); }
 inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}& value) { {{_field.variable_full_name}}.set(index, value); }
 inline void set_{{_field.name}}(uint32_t index, const {{_field.type}}&& value) { {{_field.variable_full_name}}.set(index, value); }
+inline void set_{{_field.name}}(const {{_field.repeated_type}}& values) { {{_field.variable_full_name}} = values; }
 inline void add_{{_field.name}}(const {{_field.type}}& value) { {{_field.variable_full_name}}.add(value); }
 inline {{_field.repeated_type}}& mutable_{{_field.name}}() { return {{_field.variable_full_name}}; }
 {% endif %}

+ 13 - 0
src/RepeatedFieldFixedSize.h

@@ -63,6 +63,19 @@ namespace EmbeddedProto
 
       ~RepeatedFieldFixedSize() override = default;
 
+      //! Assign one repieted field to the other, but only when the length and type matches.
+      RepeatedFieldFixedSize<DATA_TYPE, MAX_LENGTH>& operator=(const 
+                                                RepeatedFieldFixedSize<DATA_TYPE, MAX_LENGTH>& rhs)
+      {
+        for(uint32_t i = 0; i < rhs.get_length(); ++i) 
+        {
+          data_[i] = rhs.get_const(i);
+        }
+        current_length_ = rhs.get_length();
+        
+        return *this;
+      }
+
       //! Obtain the total number of DATA_TYPE items in the array.
       uint32_t get_length() const override { return current_length_; }
 

+ 1 - 1
test/proto/repeated_fields.proto

@@ -54,5 +54,5 @@ message repeated_message
 
 message nested_repeated_message
 {
-    repeated_message rm = 1;
+    repeated_fields rf = 1;
 }

+ 24 - 0
test/test_RepeatedFieldMessage.cpp

@@ -483,4 +483,28 @@ TEST(RepeatedFieldMessage, deserialize_max)
 
 }
 
+TEST(RepeatedFieldMessage, assign_a_nested_message) 
+{
+  // Assign a nested message which holds an repeated field.
+  nested_repeated_message<Y_SIZE>  top_level_msg;
+
+  // The nexted message
+  repeated_fields<Y_SIZE> nested_msg;
+
+  // Fill the array with some data.
+  nested_msg.add_y(1);
+  nested_msg.add_y(2);
+  nested_msg.add_y(3);
+
+
+  // And assign
+  top_level_msg.set_rf(nested_msg);
+  
+  EXPECT_EQ(3, top_level_msg.get_rf().get_y().get_length());
+  EXPECT_EQ(1, top_level_msg.get_rf().get_y()[0]);
+  EXPECT_EQ(2, top_level_msg.get_rf().get_y()[1]);
+  EXPECT_EQ(3, top_level_msg.get_rf().get_y()[2]);
+
+}
+
 } // End of namespace test_EmbeddedAMS_RepeatedFieldMessage