ReadBufferSection.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "ReadBufferSection.h"
  31. #include <algorithm>
  32. namespace EmbeddedProto
  33. {
  34. ReadBufferSection::ReadBufferSection(ReadBufferInterface& buffer, const uint32_t size)
  35. : buffer_(buffer),
  36. size_(std::min(size, buffer.get_size())),
  37. max_size_(std::min(size, buffer.get_size()))
  38. {
  39. }
  40. uint32_t ReadBufferSection::get_size() const
  41. {
  42. return size_;
  43. }
  44. uint32_t ReadBufferSection::get_max_size() const
  45. {
  46. return max_size_;
  47. }
  48. bool ReadBufferSection::peek(uint8_t& byte) const
  49. {
  50. bool result = 0 < size_;
  51. if(result)
  52. {
  53. result = buffer_.peek(byte);
  54. }
  55. return result;
  56. }
  57. void ReadBufferSection::advance()
  58. {
  59. if(0 < size_)
  60. {
  61. buffer_.advance();
  62. --size_;
  63. }
  64. }
  65. void ReadBufferSection::advance(const uint32_t N)
  66. {
  67. if(0 < size_)
  68. {
  69. uint32_t n = (N <= size_) ? N : size_;
  70. buffer_.advance(n);
  71. size_ -= n;
  72. }
  73. }
  74. bool ReadBufferSection::pop(uint8_t& byte)
  75. {
  76. bool result = 0 < size_;
  77. if(result)
  78. {
  79. result = buffer_.pop(byte);
  80. --size_;
  81. }
  82. return result;
  83. }
  84. } // End of namespace EmbeddedProto