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

Removed the separate files for field and string.

Bart Hertog 5 лет назад
Родитель
Сommit
5a94d86df8
6 измененных файлов с 30 добавлено и 408 удалено
  1. 0 2
      generator/Header_Template.h
  2. 2 2
      generator/protoc-gen-eams.py
  3. 0 223
      src/FieldBytes.h
  4. 0 175
      src/FieldString.h
  5. 26 4
      src/FieldStringBytes.h
  6. 2 2
      test/test_string_bytes.cpp

+ 0 - 2
generator/Header_Template.h

@@ -505,8 +505,6 @@ class {{ msg.name }} final: public ::EmbeddedProto::MessageInterface
 #include <MessageSizeCalculator.h>
 #include <ReadBufferSection.h>
 #include <RepeatedFieldFixedSize.h>
-#include <FieldString.h>
-#include <FieldBytes.h>
 #include <FieldStringBytes.h>
 #include <Errors.h>
 {% endif %}

+ 2 - 2
generator/protoc-gen-eams.py

@@ -138,8 +138,8 @@ class FieldTemplateParameters:
             # Store only the type without namespace or class scopes
             self.short_type = self.type.split("::")[-1]
         elif self.is_string:
-            self.type = "::EmbeddedProto::FieldStringB"
-            self.short_type = "FieldStringB"
+            self.type = "::EmbeddedProto::FieldString"
+            self.short_type = "FieldString"
         elif self.is_bytes:
             self.type = "::EmbeddedProto::FieldBytes"
             self.short_type = "FieldBytes"

+ 0 - 223
src/FieldBytes.h

@@ -1,223 +0,0 @@
-/*
- *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
- *
- *  This file is part of Embedded Proto.
- *
- *  Embedded Proto is open source software: you can redistribute it and/or 
- *  modify it under the terms of the GNU General Public License as published 
- *  by the Free Software Foundation, version 3 of the license.
- *
- *  Embedded Proto  is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
- *
- *  For commercial and closed source application please visit:
- *  <https://EmbeddedProto.com/license/>.
- *
- *  Embedded AMS B.V.
- *  Info:
- *    info at EmbeddedProto dot com
- *
- *  Postal address:
- *    Johan Huizingalaan 763a
- *    1066 VH, Amsterdam
- *    the Netherlands
- */
-
-#ifndef _FIELD_BYTES_H_
-#define _FIELD_BYTES_H_
-
-#include "Fields.h"
-#include "Errors.h"
-
-#include <string.h>
-
-namespace EmbeddedProto
-{
-
-  template<uint32_t MAX_LENGTH>
-  class FieldBytes : public Field
-  {
-    public:
-
-      FieldBytes()
-        : current_length_(0),
-          bytes_{0}
-      {
-
-      }
-
-      virtual ~FieldBytes()
-      {
-        clear();
-      }
-
-      //! Obtain the number of characters in the string right now.
-      uint32_t get_length() const { return current_length_; }
-
-      //! Obtain the maximum number characters in the string.
-      uint32_t get_max_length() const { return MAX_LENGTH; }
-
-      //! Get a constant pointer to the first element in the array.
-      const uint8_t* get_data() const { return bytes_; }
-
-      //! Get a reference to the value at the given index. 
-      /*!
-        This function will update the number of elements used in the array.
-
-        \param[in] index The desired index to return.
-        \return The reference to the value at the given index. Will return the last element if the 
-                index is out of bounds
-      */
-      uint8_t& get(uint32_t index) 
-      { 
-        uint32_t limited_index = std::min(index, MAX_LENGTH-1);
-        // Check if we need to update the number of elements in the array.
-        if(limited_index >= current_length_) {
-          current_length_ = limited_index + 1;
-        }
-        return bytes_[limited_index]; 
-      }
-
-      //! Get a constant reference to the value at the given index. 
-      /*!
-        \param[in] index The desired index to return.
-        \return The reference to the value at the given index. Will return the last element if the 
-                index is out of bounds
-      */
-      const uint8_t& get_const(uint32_t index) const 
-      { 
-        uint32_t limited_index = std::min(index, MAX_LENGTH-1);
-        return bytes_[limited_index]; 
-      }
-
-      //! Get a reference to the value at the given index. 
-      /*!
-        This function will update the number of elements used in the array.
-
-        \param[in] index The desired index to return.
-        \return The reference to the value at the given index. Will return the last element if the 
-                index is out of bounds
-      */
-      uint8_t& operator[](uint32_t index) { return this->get(index); }
-
-      //! Get a constant reference to the value at the given index. 
-      /*!
-        \param[in] index The desired index to return.
-        \return The reference to the value at the given index. Will return the last element if the 
-                index is out of bounds
-      */
-      const uint8_t& operator[](uint32_t index) const { return this->get_const(index); }
-
-      Error set_data(const uint8_t* data, const uint32_t length) 
-      {
-        Error return_value = Error::NO_ERRORS;
-        if(MAX_LENGTH >= length) 
-        {
-          current_length_ = length;
-          memcpy(bytes_, data, length);
-        }
-        else 
-        {
-          return_value = Error::ARRAY_FULL;
-        }
-        return return_value;
-      }
-
-      Error serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const override 
-      {
-        Error return_value = Error::NO_ERRORS;
-
-        if(0 < current_length_) 
-        {
-          if(current_length_ <= buffer.get_available_size())
-          {
-            uint32_t tag = WireFormatter::MakeTag(field_number, 
-                                                  WireFormatter::WireType::LENGTH_DELIMITED);
-            return_value = WireFormatter::SerializeVarint(tag, buffer);
-            if(Error::NO_ERRORS == return_value) 
-            {
-              return_value = serialize(buffer);
-            }
-          }
-          else 
-          {
-            return_value = Error::BUFFER_FULL;
-          }
-        }
-
-        return return_value;
-      }
-
-      Error serialize(WriteBufferInterface& buffer) const override 
-      { 
-        Error return_value = WireFormatter::SerializeVarint(current_length_, buffer);
-        if(Error::NO_ERRORS == return_value) 
-        {
-          if(!buffer.push(bytes_, current_length_))
-          {
-            return_value = Error::BUFFER_FULL;
-          }
-        }
-        return return_value;
-      }
-
-      Error deserialize(ReadBufferInterface& buffer) override 
-      {
-        uint32_t availiable;
-        Error return_value = WireFormatter::DeserializeVarint(buffer, availiable);
-        if(Error::NO_ERRORS == return_value)
-        {
-          if(MAX_LENGTH >= availiable) 
-          {
-            clear();
-
-            uint8_t byte;
-            while((current_length_ < availiable) && buffer.pop(byte)) 
-            {
-              bytes_[current_length_] = byte;
-              ++current_length_;
-            }
-
-            if(current_length_ != availiable)
-            {
-              // If at the end we did not read the same number of characters something went wrong.
-              return_value = Error::END_OF_BUFFER;
-            }
-          }
-          else 
-          {
-            return_value = Error::ARRAY_FULL;
-          }
-        }
-        else 
-        {
-          current_length_ = 0;
-        }
-
-        return return_value;
-      }
-
-      //! Reset the field to it's initial value.
-      void clear() override 
-      { 
-        memset(bytes_, 0, current_length_);
-        current_length_ = 0; 
-      }
-  
-    private:
-
-      //! Number of item in the data array.
-      uint32_t current_length_;
-
-      //! The text.
-      uint8_t bytes_[MAX_LENGTH];
-  };
-
-} // End of namespace EmbeddedProto
-
-#endif // End of _FIELD_BYTES_H_

+ 0 - 175
src/FieldString.h

@@ -1,175 +0,0 @@
-/*
- *  Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
- *
- *  This file is part of Embedded Proto.
- *
- *  Embedded Proto is open source software: you can redistribute it and/or 
- *  modify it under the terms of the GNU General Public License as published 
- *  by the Free Software Foundation, version 3 of the license.
- *
- *  Embedded Proto  is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
- *
- *  For commercial and closed source application please visit:
- *  <https://EmbeddedProto.com/license/>.
- *
- *  Embedded AMS B.V.
- *  Info:
- *    info at EmbeddedProto dot com
- *
- *  Postal address:
- *    Johan Huizingalaan 763a
- *    1066 VH, Amsterdam
- *    the Netherlands
- */
-
-#ifndef _FIELD_STRING_H_
-#define _FIELD_STRING_H_
-
-#include "Fields.h"
-#include "Errors.h"
-
-#include <string.h>
-
-namespace EmbeddedProto
-{
-
-  template<uint32_t MAX_LENGTH>
-  class FieldString : public Field 
-  {
-    public:
-
-      FieldString()
-        : current_length_(0),
-          string_{0}
-      {
-
-      }
-
-      virtual ~FieldString()
-      {
-        clear();
-      }
-
-      //! Obtain the number of characters in the string right now.
-      uint32_t get_length() const { return current_length_; }
-
-      //! Obtain the maximum number characters in the string.
-      uint32_t get_max_length() const { return MAX_LENGTH; }
-
-      //! Assignment operator for character array's, only use null terminated strings!
-      void operator=(const char* const &&rhs)
-      {
-        const uint32_t rhs_MAX_LENGTH = strlen(rhs);
-        current_length_ = std::min(rhs_MAX_LENGTH, MAX_LENGTH);
-        strncpy(string_, rhs, current_length_);
-
-        // Make sure the string is null terminated.
-        if(MAX_LENGTH > current_length_)
-        {
-          string_[current_length_] = 0;
-        }
-      }
-
-      const char* get() const { return string_; }
-
-      Error serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const override 
-      {
-        Error return_value = Error::NO_ERRORS;
-
-        if(0 < current_length_) 
-        {
-          if(current_length_ <= buffer.get_available_size())
-          {
-            uint32_t tag = WireFormatter::MakeTag(field_number, 
-                                                  WireFormatter::WireType::LENGTH_DELIMITED);
-            return_value = WireFormatter::SerializeVarint(tag, buffer);
-            if(Error::NO_ERRORS == return_value) 
-            {
-              return_value = serialize(buffer);
-            }
-          }
-          else 
-          {
-            return_value = Error::BUFFER_FULL;
-          }
-        }
-
-        return return_value;
-      }
-
-      Error serialize(WriteBufferInterface& buffer) const override 
-      { 
-        Error return_value = WireFormatter::SerializeVarint(current_length_, buffer);
-        if(Error::NO_ERRORS == return_value) 
-        {
-          const void* void_pointer = static_cast<const void*>(&(string_[0]));
-          const uint8_t* byte_pointer = static_cast<const uint8_t*>(void_pointer);
-          if(!buffer.push(byte_pointer, current_length_))
-          {
-            return_value = Error::BUFFER_FULL;
-          }
-        }
-        return return_value;
-      }
-
-      Error deserialize(ReadBufferInterface& buffer) override 
-      {
-        uint32_t availiable;
-        Error return_value = WireFormatter::DeserializeVarint(buffer, availiable);
-        if(Error::NO_ERRORS == return_value)
-        {
-          if(MAX_LENGTH >= availiable) 
-          {
-            clear();
-
-            uint8_t byte;
-            while((current_length_ < availiable) && buffer.pop(byte)) 
-            {
-              string_[current_length_] = static_cast<char>(byte);
-              ++current_length_;
-            }
-
-            if(current_length_ != availiable)
-            {
-              // If at the end we did not read the same number of characters something went wrong.
-              return_value = Error::END_OF_BUFFER;
-            }
-          }
-          else 
-          {
-            return_value = Error::ARRAY_FULL;
-          }
-        }
-        else 
-        {
-          current_length_ = 0;
-        }
-
-        return return_value;
-      }
-
-      //! Reset the field to it's initial value.
-      void clear() override 
-      { 
-        memset(string_, 0, current_length_);
-        current_length_ = 0; 
-      }
-  
-    private:
-
-      //! Number of item in the data array.
-      uint32_t current_length_;
-
-      //! The text.
-      char string_[MAX_LENGTH];
-  };
-
-} // End of namespace EmbeddedProto
-
-#endif // End of _FIELD_STRING_H_

+ 26 - 4
src/FieldStringBytes.h

@@ -121,7 +121,20 @@ namespace EmbeddedProto
         const DATA_TYPE& operator[](uint32_t index) const { return this->get_const(index); }
 
 
-
+        Error set(const DATA_TYPE* data, const uint32_t length)
+        {
+          Error return_value = Error::NO_ERRORS;
+          if(MAX_LENGTH >= length)
+          {
+            current_length_ = length;
+            memcpy(data_, data, length);
+          }
+          else
+          {
+            return_value = Error::ARRAY_FULL;
+          }
+          return return_value;
+        }
 
 
         Error serialize_with_id(uint32_t field_number, WriteBufferInterface& buffer) const override 
@@ -220,11 +233,11 @@ namespace EmbeddedProto
   } // End of namespace internal
 
   template<uint32_t MAX_LENGTH>
-  class FieldStringB : public internal::FieldStringBytes<MAX_LENGTH, char>
+  class FieldString : public internal::FieldStringBytes<MAX_LENGTH, char>
   {
     public:
-      FieldStringB() = default;
-      ~FieldStringB() = default;
+      FieldString() = default;
+      virtual ~FieldString() = default;
 
       void operator=(const char* const &&rhs)
       {
@@ -240,6 +253,15 @@ namespace EmbeddedProto
       }
   };
 
+  template<uint32_t MAX_LENGTH>
+  class FieldBytes : public internal::FieldStringBytes<MAX_LENGTH, uint8_t>
+  {
+    public:
+      FieldBytes() = default;
+      virtual ~FieldBytes() = default; 
+  };
+
+
 } // End of namespace EmbeddedProto
 
 #endif // End of _FIELD_STRING_BYTES_H_

+ 2 - 2
test/test_string_bytes.cpp

@@ -171,7 +171,7 @@ TEST(FieldBytes, serialize)
   Mocks::WriteBufferMock buffer;
 
   uint8_t bytes[] = {1u, 2u, 3u, 0u};
-  msg.mutable_b().set_data(bytes, 4);
+  msg.mutable_b().set(bytes, 4);
 
   EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(17));
 
@@ -219,7 +219,7 @@ TEST(FieldBytes, oneof_serialize)
   Mocks::WriteBufferMock buffer;
 
   uint8_t bytes[] = {1u, 2u, 3u, 0u};
-  msg.mutable_b().set_data(bytes, 4);
+  msg.mutable_b().set(bytes, 4);
 
   EXPECT_CALL(buffer, get_available_size()).Times(1).WillOnce(Return(17));