EmbeddedProto  2.0.0
EmbeddedProto is a C++ Protocol Buffer implementation specifically suitable for microcontrollers.
RepeatedFieldFixedSize.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
3  *
4  * This file is part of Embedded Proto.
5  *
6  * Embedded Proto is open source software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation, version 3 of the license.
9  *
10  * Embedded Proto is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
17  *
18  * For commercial and closed source application please visit:
19  * <https://EmbeddedProto.com/license/>.
20  *
21  * Embedded AMS B.V.
22  * Info:
23  * info at EmbeddedProto dot com
24  *
25  * Postal address:
26  * Johan Huizingalaan 763a
27  * 1066 VH, Amsterdam
28  * the Netherlands
29  */
30 
31 #ifndef _REPEATED_FIELD_SIZE_H_
32 #define _REPEATED_FIELD_SIZE_H_
33 
34 #include "RepeatedField.h"
35 #include "Errors.h"
36 
37 #include <cstdint>
38 #include <cstring>
39 #include <algorithm>
40 
41 
42 namespace EmbeddedProto
43 {
44 
46 
50  template<class DATA_TYPE, uint32_t MAX_LENGTH>
51  class RepeatedFieldFixedSize : public RepeatedField<DATA_TYPE>
52  {
53  static constexpr uint32_t BYTES_PER_ELEMENT = sizeof(DATA_TYPE);
54 
55  public:
56 
58  : current_length_(0),
59  data_{}
60  {
61 
62  }
63 
64  ~RepeatedFieldFixedSize() override = default;
65 
69  {
70  for(uint32_t i = 0; i < rhs.get_length(); ++i)
71  {
72  data_[i] = rhs.get_const(i);
73  }
74  current_length_ = rhs.get_length();
75 
76  return *this;
77  }
78 
80  uint32_t get_length() const override { return current_length_; }
81 
83  uint32_t get_max_length() const override { return MAX_LENGTH; }
84 
86  uint32_t get_size() const override { return BYTES_PER_ELEMENT * current_length_; }
87 
89  uint32_t get_max_size() const override { return BYTES_PER_ELEMENT * MAX_LENGTH; }
90 
91  DATA_TYPE& get(uint32_t index) override
92  {
93  uint32_t limited_index = std::min(index, MAX_LENGTH-1);
94  // Check if we need to update the number of elements in the array.
95  if(limited_index >= current_length_) {
96  current_length_ = limited_index + 1;
97  }
98  return data_[limited_index];
99  }
100 
101  const DATA_TYPE& get_const(uint32_t index) const override
102  {
103  uint32_t limited_index = std::min(index, MAX_LENGTH-1);
104  return data_[limited_index];
105  }
106 
107  void set(uint32_t index, const DATA_TYPE& value) override
108  {
109  uint32_t limited_index = std::min(index, MAX_LENGTH-1);
110  // Check if we need to update the number of elements in the array.
111  if(limited_index >= current_length_) {
112  current_length_ = limited_index + 1;
113  }
114  data_[limited_index] = value;
115  }
116 
117  Error set_data(const DATA_TYPE* data, const uint32_t length) override
118  {
119  Error return_value = Error::NO_ERRORS;
120  if(MAX_LENGTH >= length)
121  {
122  const DATA_TYPE* d = data;
123  for(uint32_t i = 0; i < length; ++i)
124  {
125  data_[i] = *d;
126  ++d;
127  }
128  current_length_ = length;
129  }
130  else
131  {
132  return_value = Error::ARRAY_FULL;
133  }
134  return return_value;
135  }
136 
137  Error add(const DATA_TYPE& value) override
138  {
139  Error return_value = Error::NO_ERRORS;
140  if(MAX_LENGTH > current_length_)
141  {
142  data_[current_length_] = value;
143  ++current_length_;
144  }
145  else
146  {
147  return_value = Error::ARRAY_FULL;
148  }
149  return return_value;
150  }
151 
152  void clear() override
153  {
154  for(uint32_t i = 0; i < current_length_; ++i)
155  {
156  data_[i].clear();
157  }
158  current_length_ = 0;
159  }
160 
161  private:
162 
164  uint32_t current_length_;
165 
167  DATA_TYPE data_[MAX_LENGTH];
168  };
169 
170 } // End of namespace EmbeddedProto
171 
172 #endif // End of _REPEATED_FIELD_SIZE_H_
Errors.h
EmbeddedProto::RepeatedFieldFixedSize::add
Error add(const DATA_TYPE &value) override
Append a value to the end of the array.
Definition: RepeatedFieldFixedSize.h:137
EmbeddedProto::RepeatedField
Class template that specifies the interface of an arry with the data type.
Definition: RepeatedField.h:50
EmbeddedProto::RepeatedFieldFixedSize::get
DATA_TYPE & get(uint32_t index) override
Get a reference to the value at the given index.
Definition: RepeatedFieldFixedSize.h:91
EmbeddedProto
Definition: Errors.h:34
EmbeddedProto::Error::NO_ERRORS
@ NO_ERRORS
No errors have occurred.
EmbeddedProto::RepeatedFieldFixedSize::~RepeatedFieldFixedSize
~RepeatedFieldFixedSize() override=default
EmbeddedProto::RepeatedFieldFixedSize::get_size
uint32_t get_size() const override
Obtain the total number of bytes currently stored in the array.
Definition: RepeatedFieldFixedSize.h:86
EmbeddedProto::Error
Error
This enumeration defines errors which can occur during serialization and deserialization.
Definition: Errors.h:38
EmbeddedProto::RepeatedFieldFixedSize::get_const
const DATA_TYPE & get_const(uint32_t index) const override
Get a constant reference to the value at the given index.
Definition: RepeatedFieldFixedSize.h:101
EmbeddedProto::RepeatedFieldFixedSize::get_max_length
uint32_t get_max_length() const override
Obtain the maximum number of DATA_TYPE items which can at most be stored in the array.
Definition: RepeatedFieldFixedSize.h:83
EmbeddedProto::RepeatedFieldFixedSize::set_data
Error set_data(const DATA_TYPE *data, const uint32_t length) override
Given a different array of known length copy that data into this object.
Definition: RepeatedFieldFixedSize.h:117
EmbeddedProto::RepeatedFieldFixedSize::set
void set(uint32_t index, const DATA_TYPE &value) override
Set the value at the given index.
Definition: RepeatedFieldFixedSize.h:107
EmbeddedProto::RepeatedFieldFixedSize::clear
void clear() override
Remove all data in the array and set it to the default value.
Definition: RepeatedFieldFixedSize.h:152
EmbeddedProto::Error::ARRAY_FULL
@ ARRAY_FULL
The array is full, it is not possible to push more items in it.
EmbeddedProto::RepeatedFieldFixedSize::RepeatedFieldFixedSize
RepeatedFieldFixedSize()
Definition: RepeatedFieldFixedSize.h:57
EmbeddedProto::RepeatedFieldFixedSize
A template class that actually holds some data.
Definition: RepeatedFieldFixedSize.h:51
EmbeddedProto::RepeatedFieldFixedSize::get_max_size
uint32_t get_max_size() const override
Obtain the maximum number of bytes which can at most be stored in the array.
Definition: RepeatedFieldFixedSize.h:89
RepeatedField.h
EmbeddedProto::RepeatedFieldFixedSize::operator=
RepeatedFieldFixedSize< DATA_TYPE, MAX_LENGTH > & operator=(const RepeatedFieldFixedSize< DATA_TYPE, MAX_LENGTH > &rhs)
Assign one repieted field to the other, but only when the length and type matches.
Definition: RepeatedFieldFixedSize.h:67
EmbeddedProto::RepeatedFieldFixedSize::get_length
uint32_t get_length() const override
Obtain the total number of DATA_TYPE items in the array.
Definition: RepeatedFieldFixedSize.h:80