EmbeddedProto  2.0.0
EmbeddedProto is a C++ Protocol Buffer implementation specifically suitable for microcontrollers.
MessageSizeCalculator.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 _MESSAGE_SIZE_CALCULATOR_H_
32 #define _MESSAGE_SIZE_CALCULATOR_H_
33 
34 #include "WriteBufferInterface.h"
35 
36 #include <cstdint>
37 #include <limits>
38 
39 
40 namespace EmbeddedProto
41 {
43 
51  {
52  public:
53  MessageSizeCalculator() = default;
54  ~MessageSizeCalculator() override = default;
55 
57  void clear() override
58  {
59  size_ = 0;
60  }
61 
63  uint32_t get_size() const override
64  {
65  return size_;
66  }
67 
69  uint32_t get_max_size() const override
70  {
71  return std::numeric_limits<uint32_t>::max();
72  }
73 
75  uint32_t get_available_size() const override
76  {
77  return std::numeric_limits<uint32_t>::max();
78  }
79 
81  bool push(const uint8_t byte) override
82  {
83  // Ignore the unused parameter
84  (void)byte;
85  ++size_;
86  return true;
87  }
88 
90  bool push(const uint8_t* bytes, const uint32_t length) override
91  {
92  // Ignore the unused parameter
93  (void)bytes;
94  size_ += length;
95  return true;
96  }
97 
98 
99  private:
100 
102  uint32_t size_ = 0;
103 
104  }; // End of class MessageSizeCalculator
105 
106 } // End of namespace EmbeddedProto
107 
108 #endif
EmbeddedProto::MessageSizeCalculator
This class is used in a message to calculate the current serialized size.
Definition: MessageSizeCalculator.h:50
EmbeddedProto::MessageSizeCalculator::~MessageSizeCalculator
~MessageSizeCalculator() override=default
EmbeddedProto::MessageSizeCalculator::get_available_size
uint32_t get_available_size() const override
To continue serialization return the maximum number that fits in a 32bit unsigned int.
Definition: MessageSizeCalculator.h:75
EmbeddedProto::MessageSizeCalculator::get_max_size
uint32_t get_max_size() const override
To continue serialization return the maximum number that fits in a 32bit unsigned int.
Definition: MessageSizeCalculator.h:69
EmbeddedProto
Definition: Errors.h:34
EmbeddedProto::MessageSizeCalculator::clear
void clear() override
Reset the size count of the buffer.
Definition: MessageSizeCalculator.h:57
EmbeddedProto::WriteBufferInterface
The pure virtual definition of a message buffer used for writing .
Definition: WriteBufferInterface.h:46
EmbeddedProto::MessageSizeCalculator::push
bool push(const uint8_t byte) override
For calculating the size we just increment the counter and always return true.
Definition: MessageSizeCalculator.h:81
EmbeddedProto::MessageSizeCalculator::get_size
uint32_t get_size() const override
Obtain the total number of bytes currently stored in the buffer.
Definition: MessageSizeCalculator.h:63
WriteBufferInterface.h
EmbeddedProto::MessageSizeCalculator::MessageSizeCalculator
MessageSizeCalculator()=default
EmbeddedProto::MessageSizeCalculator::push
bool push(const uint8_t *bytes, const uint32_t length) override
Increment the size with the given length.
Definition: MessageSizeCalculator.h:90