Browse Source

Serialze and deserialize functions for RepeatedFields.

Bart Hertog 6 năm trước cách đây
mục cha
commit
ad80305acf
3 tập tin đã thay đổi với 63 bổ sung4 xóa
  1. 12 2
      generator/Header_Template.h
  2. 1 1
      src/MessageInterface.h
  3. 50 1
      src/RepeatedField.h

+ 12 - 2
generator/Header_Template.h

@@ -89,9 +89,14 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
         result = ::EmbeddedProto::{{field.serialization_func}}({{field.variable_id_name}}, value, buffer);
       }
       {% elif field.is_repeated_field %}
-      if(0 != {{field.variable_name}}.get_length() && result)
+      const uint32_t size_{{field.name}} = {{field.variable_name}}.serialized_size();
+      result = (size_{{field.name}} <= buffer.get_available_size());
+      if(result && (0 < size_{{field.name}}))
       {
-        // TODO
+        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 %}
       if(({{field.default_value}} != {{field.variable_name}}.get()) && result)
@@ -124,6 +129,11 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
               result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
               ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
               result = result && {{field.variable_name}}.deserialize(bufferSection);
+              {% elif field.is_repeated_field %}
+              uint32_t size;
+              result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
+              ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
+              result = result && {{field.variable_name}}.deserialize(bufferSection);
               {% elif field.of_type_enum %}
               uint32_t value;
               result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, value);

+ 1 - 1
src/MessageInterface.h

@@ -31,7 +31,7 @@ public:
 
     //! Function to deserialize this message.
     /*!
-        From an array of date fill this message object with data.
+        From an array of data fill this message object with data.
 
         \param buffer [in]  The memory from which the message is obtained.
 

+ 50 - 1
src/RepeatedField.h

@@ -5,6 +5,9 @@
 #include <cstring>
 #include <algorithm>    // std::min
 
+#include <Fields.h>
+#include <MessageSizeCalculator.h>
+
 namespace EmbeddedProto
 {
 
@@ -84,6 +87,52 @@ namespace EmbeddedProto
 
       //! Remove all data in the array and set it to the default value.
       virtual void clear() = 0;
+
+      //! Function to serialize this array.
+      /*!
+          The data this array holds will be serialized into the buffer.
+          \param buffer [in]  The memory in which the serialized array is stored.
+          \return True when every was successfull. 
+      */
+      bool serialize(::EmbeddedProto::WriteBufferInterface& buffer) const
+      {
+        bool result = true;
+        for(uint32_t i = 0; (i < this->get_length()) && result; ++i)
+        {
+          result = ::EmbeddedProto::serialize(this->get(i), buffer);
+        }
+        return result;
+      }
+
+      //! Function to deserialize this array.
+      /*!
+          From a buffer of data fill this array with data.
+          \param buffer [in]  The memory from which the message is obtained.
+          \return True when every was successfull. 
+      */
+      bool deserialize(::EmbeddedProto::ReadBufferInterface& buffer)
+      {
+        this->clear();
+        DATA_TYPE x;
+        bool result = true;
+        while(result && ::EmbeddedProto::deserialize(buffer, x)) 
+        {
+          result = this->add(x);
+        }
+        return result;
+      }
+
+      //! Calculate the size of this message when serialized.
+      /*!
+          \return The number of bytes this message will require once serialized.
+      */
+      uint32_t serialized_size() const 
+      {
+        ::EmbeddedProto::MessageSizeCalculator calcBuffer;
+        this->serialize(calcBuffer);
+        return calcBuffer.get_size();
+      }
+
   };
 
   //! A template class that actually holds some data.
@@ -92,7 +141,7 @@ namespace EmbeddedProto
     class using this type of object.
   */
   template<class DATA_TYPE, uint32_t MAX_SIZE>
-  class RepeatedFieldSize : RepeatedField<DATA_TYPE>
+  class RepeatedFieldSize : public RepeatedField<DATA_TYPE>
   { 
       static constexpr uint32_t BYTES_PER_ELEMENT = sizeof(DATA_TYPE);