فهرست منبع

The non const version of get() will now modify the number of elements in the array.

Bart Hertog 5 سال پیش
والد
کامیت
a921ad789b
3فایلهای تغییر یافته به همراه50 افزوده شده و 18 حذف شده
  1. 5 8
      src/RepeatedField.h
  2. 20 5
      src/RepeatedFieldFixedSize.h
  3. 25 5
      test/test_RepeatedFieldFixedSize.cpp

+ 5 - 8
src/RepeatedField.h

@@ -68,9 +68,6 @@ namespace EmbeddedProto
       //! Obtain the maximum number of bytes which can at most be stored in the array.
       virtual uint32_t get_max_size() const = 0;
 
-      //! Get a pointer to the first element in the array.
-      virtual DATA_TYPE* get_data() = 0;
-
       //! Get a reference to the value at the given index. 
       /*!
         \param[in] index The desired index to return.
@@ -83,7 +80,7 @@ namespace EmbeddedProto
         \param[in] index The desired index to return.
         \return The constant reference to the value at the given index.
       */
-      virtual const DATA_TYPE& get(uint32_t index) const = 0;
+      virtual const DATA_TYPE& get_const(uint32_t index) const = 0;
 
       //! Get a reference to the value at the given index. 
       /*!
@@ -97,7 +94,7 @@ namespace EmbeddedProto
         \param[in] index The desired index to return.
         \return The constant reference to the value at the given index.
       */
-      const DATA_TYPE& operator[](uint32_t index) const { return this->get(index); }
+      const DATA_TYPE& operator[](uint32_t index) const { return this->get_const(index); }
 
       //! Set the value at the given index.
       /*!
@@ -252,7 +249,7 @@ namespace EmbeddedProto
         Error return_value = Error::NO_ERRORS;
         for(uint32_t i = 0; (i < this->get_length()) && (Error::NO_ERRORS == return_value); ++i)
         {
-          return_value = this->get(i).serialize(buffer);
+          return_value = this->get_const(i).serialize(buffer);
         }
         return return_value;
       }
@@ -262,7 +259,7 @@ namespace EmbeddedProto
         Error return_value = Error::NO_ERRORS;
         for(uint32_t i = 0; (i < this->get_length()) && (Error::NO_ERRORS == return_value); ++i)
         {
-          const uint32_t size_x = this->get(i).serialized_size();
+          const uint32_t size_x = this->get_const(i).serialized_size();
           uint32_t tag = WireFormatter::MakeTag(field_number, 
                                     WireFormatter::WireType::LENGTH_DELIMITED);
           return_value = WireFormatter::SerializeVarint(tag, buffer);
@@ -271,7 +268,7 @@ namespace EmbeddedProto
             return_value = WireFormatter::SerializeVarint(size_x, buffer);
             if((Error::NO_ERRORS == return_value) && (0 < size_x)) 
             {
-              return_value = this->get(i).serialize(buffer);
+              return_value = this->get_const(i).serialize(buffer);
             }
           }
         }

+ 20 - 5
src/RepeatedFieldFixedSize.h

@@ -73,15 +73,30 @@ namespace EmbeddedProto
       //! Obtain the maximum number of bytes which can at most be stored in the array.
       uint32_t get_max_size() const override { return BYTES_PER_ELEMENT * MAX_LENGTH; }
 
-      DATA_TYPE* get_data() { return data_; }
+      DATA_TYPE& get(uint32_t index) override 
+      { 
+        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 data_[limited_index]; 
+      }
 
-      DATA_TYPE& get(uint32_t index) override { return data_[index]; }
-      const DATA_TYPE& get(uint32_t index) const override { return data_[index]; }
+      const DATA_TYPE& get_const(uint32_t index) const override 
+      { 
+        uint32_t limited_index = std::min(index, MAX_LENGTH-1);
+        return data_[limited_index]; 
+      }
 
       void set(uint32_t index, const DATA_TYPE& value) override 
       { 
-        data_[index] = value;
-        current_length_ = std::max(index+1, current_length_); 
+        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;
+        }
+        data_[limited_index] = value;  
       }
 
       Error set_data(const DATA_TYPE* data, const uint32_t length) override 

+ 25 - 5
test/test_RepeatedFieldFixedSize.cpp

@@ -85,6 +85,26 @@ TEST(RepeatedFieldFixedSize, add_data)
   EXPECT_EQ(EmbeddedProto::Error::ARRAY_FULL, result);
 }
 
+TEST(RepeatedFieldFixedSize, get) 
+{  
+  static constexpr uint32_t LENGTH = 3;
+  EmbeddedProto::RepeatedFieldFixedSize<::EmbeddedProto::uint32, LENGTH> x;
+
+  // Get (non-const) should modify the number of items in the array.
+
+  // Set the second element
+  x.get(1) = 1;
+  EXPECT_EQ(2, x.get_length());
+  EXPECT_EQ(0, x.get_const(0));
+  EXPECT_EQ(1, x.get_const(1));
+
+  // When going out of bound the last element should be returned.
+  EXPECT_EQ(0, x.get_const(2));
+  x.get(3) = 3;
+  EXPECT_EQ(3, x.get_const(2));
+  EXPECT_EQ(3, x.get_const(3));
+}
+
 TEST(RepeatedFieldFixedSize, set_data_array) 
 {  
   static constexpr uint32_t LENGTH = 3;
@@ -113,14 +133,14 @@ TEST(RepeatedFieldFixedSize, set_element)
 
   // First add a value in the middle and see if we have a size of two.
   x.set(1, 2);
-  EXPECT_EQ(2, x.get(1));
+  EXPECT_EQ(2, x.get_const(1));
   EXPECT_EQ(2, x.get_length());
 
   x.set(0, 1);
-  EXPECT_EQ(1, x.get(0));
+  EXPECT_EQ(1, x.get_const(0));
 
   x.set(2, 3);
-  EXPECT_EQ(3, x.get(2));
+  EXPECT_EQ(3, x.get_const(2));
 }
 
 TEST(RepeatedFieldFixedSize, clear) 
@@ -130,8 +150,8 @@ TEST(RepeatedFieldFixedSize, clear)
   x.add(1);
   x.add(2);
   x.clear();
-  EXPECT_EQ(0U, x.get(0));
-  EXPECT_EQ(0U, x.get(1));
+  EXPECT_EQ(0U, x.get_const(0));
+  EXPECT_EQ(0U, x.get_const(1));
   EXPECT_EQ(0U, x.get_length());
 }