Prechádzať zdrojové kódy

Added the ReadBufferSection class.

Bart Hertog 6 rokov pred
rodič
commit
2a061ac62b

+ 1 - 0
CMakeLists.txt

@@ -8,6 +8,7 @@ set(CMAKE_CXX_FLAGS "-std=c++14 -Wall")
 add_subdirectory(external/googletest)
 add_subdirectory(external/googletest)
 
 
 file(GLOB src_files
 file(GLOB src_files
+    "src/*.cpp"
     "test/*.cpp"
     "test/*.cpp"
     "build/EAMS/*cpp"
     "build/EAMS/*cpp"
 )
 )

+ 3 - 1
generator/Header_Template.h

@@ -83,7 +83,8 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
               {% if field.of_type_message %}
               {% if field.of_type_message %}
               uint32_t size;
               uint32_t size;
               result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
               result = ::EmbeddedProto::WireFormatter::DeserializeVarint(buffer, size);
-              result = result && {{field.variable_name}}.deserialize(buffer);
+              ::EmbeddedProto::ReadBufferSection bufferSection(buffer, size);
+              result = result && {{field.variable_name}}.deserialize(bufferSection);
               {% else %}
               {% else %}
               result = ::EmbeddedProto::WireFormatter::{{field.deserialization_func}}(buffer, {{field.variable_name}});
               result = ::EmbeddedProto::WireFormatter::{{field.deserialization_func}}(buffer, {{field.variable_name}});
               {% endif %}
               {% endif %}
@@ -133,6 +134,7 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
 #include <MessageInterface.h>
 #include <MessageInterface.h>
 #include <WireFormatter.h>
 #include <WireFormatter.h>
 #include <MessageSizeCalculator.h>
 #include <MessageSizeCalculator.h>
+#include <ReadBufferSection.h>
 {% endif %}
 {% endif %}
 
 
 {% if namespace %}
 {% if namespace %}

+ 64 - 0
src/ReadBufferSection.cpp

@@ -0,0 +1,64 @@
+
+#include <ReadBufferSection.h>
+
+namespace EmbeddedProto 
+{
+
+  ReadBufferSection::ReadBufferSection(ReadBufferInterface& buffer, const uint32_t size)
+    : buffer_(buffer),
+      size_(size),
+      max_size_(size)
+  {
+
+  }
+
+  uint32_t ReadBufferSection::get_size() const
+  {
+    return size_;
+  }
+
+  uint32_t ReadBufferSection::get_max_size() const
+  {
+    return max_size_;
+  }
+
+  bool ReadBufferSection::peak(uint8_t& byte) const
+  {
+    bool result = 0 < size_;
+    if(result)
+    {
+      result = buffer_.peak(byte);
+    }
+    return result;
+  }
+
+  void ReadBufferSection::advance()
+  {
+    if(0 < size_) 
+    {
+      buffer_.advance();
+    }
+  }
+
+  void ReadBufferSection::advance(const uint32_t N)
+  {
+    if(0 < size_) 
+    {
+      uint32_t n = (N <= size_) ? N : size_;
+      buffer_.advance(n);
+      size_ -= n;
+    }
+  }
+
+  bool ReadBufferSection::pop(uint8_t& byte)
+  {
+    bool result = 0 < size_;
+    if(result)
+    {
+      result = buffer_.pop(byte);
+      --size_;
+    }
+    return result;
+  }
+
+} // End of namespace EmbeddedProto

+ 83 - 0
src/ReadBufferSection.h

@@ -0,0 +1,83 @@
+
+#ifndef _MESSAGE_BUFFER_SECTIOn_H_
+#define _MESSAGE_BUFFER_SECTION_H_
+
+#include <cstdint>
+
+#include <ReadBufferInterface.h>
+
+namespace EmbeddedProto 
+{
+  //! This is a wrapper around a ReadBufferInterface only exposing a given number of bytes.
+  /*!
+      This class is used when decoding a length delimited fields. It is constructed given a message
+      buffer and a size. This class will return bytes from the buffer for the given number of bytes
+      givne in the size parameter.
+
+      \see ReadBufferInterface
+  */
+  class ReadBufferSection : public ReadBufferInterface
+  {
+    public:
+
+      //! Explicitly delete the default constructor in favour of the one with parameters.
+      ReadBufferSection() = delete;
+
+      //! The constructor of the class with the required parameters
+      /*!
+        \param buffer The actual data buffer from which the bytes are obtained.
+        \param size The maximum number of bytes to return from buffer.
+      */
+      ReadBufferSection(ReadBufferInterface& buffer, const uint32_t size);
+
+      virtual ~ReadBufferSection() = default;
+
+
+      //! Return the number of bytes remaining.
+      uint32_t get_size() const override;
+
+      //! Obtain the total number of bytes which can at most be stored in the buffer.
+      /*!
+        In the case of this buffer section this will return the size of the section.
+      */
+      uint32_t get_max_size() const override;
+
+      //! Expose the function of the parent buffer.
+      /*!
+        This will not do anything if size zero is reached.
+      */
+      bool peak(uint8_t& byte) const override;
+
+      //! Decrement the size and call advance on the parent buffer.
+      /*!
+        This will not do anything if size zero is reached.
+      */
+      void advance() override;
+
+      //! Decrement the size by N bytes and call advance on the parent buffer.
+      /*!
+        This will not do anything if size zero is reached.
+      */
+      void advance(const uint32_t N) override;
+
+      //! Decrement the size and pop the next byte from the parent buffer.
+      /*!
+        This will not do anything if size zero is reached.
+      */
+      bool pop(uint8_t& byte) override;
+
+    private:
+
+      //! A reference to the buffer containing the actual data.
+      ReadBufferInterface& buffer_;
+
+      //! The number of bytes left in this section.
+      uint32_t size_;
+
+      //! The total number of bytes masked of by this section.
+      const uint32_t max_size_;
+  };
+
+} // End of namespace EmbeddedProto
+
+#endif