circular_buffer.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
32 
39 CircularBuffer::CircularBuffer(ROSaicNodeBase* node, std::size_t capacity) :
40  node_(node), head_(0), tail_(0), size_(0), capacity_(capacity)
41 {
42  data_ = new uint8_t[capacity];
43 }
44 
48 {
49  delete[] data_;
50  data_ = NULL;
51 }
52 
53 std::size_t CircularBuffer::write(const uint8_t* data, std::size_t bytes)
54 {
55  if (bytes == 0)
56  return 0;
57 
58  std::size_t capacity = capacity_;
59  std::size_t bytes_to_write = std::min(bytes, capacity - size_);
60  if (bytes_to_write != bytes)
61  {
62  node_->log(
64  "You are trying to overwrite parts of the circular buffer that have not yet been read!");
65  }
66 
67  // Writes in a single step
68  if (bytes_to_write <= capacity - head_)
69  {
70 
71  memcpy(data_ + head_, data, bytes_to_write);
72  head_ += bytes_to_write;
73  if (head_ == capacity)
74  head_ = 0;
75  }
76  // Writes in two steps. Here the circular nature comes to the surface
77  else
78  {
79  std::size_t size_1 = capacity - head_;
80  memcpy(data_ + head_, data, size_1);
81  std::size_t size_2 = bytes_to_write - size_1;
82  memcpy(data_, data + size_1, size_2);
83  head_ =
84  size_2; // Hence setting head_ = 0 three lines above was not necessary.
85  }
86  size_ += bytes_to_write;
87  return bytes_to_write;
88 }
89 
90 std::size_t CircularBuffer::read(uint8_t* data, std::size_t bytes)
91 {
92  if (bytes == 0)
93  return 0;
94  std::size_t capacity = capacity_;
95  std::size_t bytes_to_read = std::min(bytes, size_);
96  if (bytes_to_read != bytes)
97  {
98  node_->log(
100  "You are trying to read parts of the circular buffer that have not yet been written!");
101  }
102 
103  // Read in a single step
104  if (bytes_to_read <=
105  capacity - tail_) // Note that it is not size_ - tail_:
106  // If write() hasn't written something into all of capacity
107  // yet (first round of writing), we would still read those
108  // unknown bytes..
109  {
110  memcpy(data, data_ + tail_, bytes_to_read);
111  tail_ += bytes_to_read;
112  if (tail_ == capacity)
113  tail_ = 0; // Same here?
114  }
115 
116  // Read in two steps
117  else
118  {
119  std::size_t size_1 = capacity - tail_;
120  memcpy(data, data_ + tail_, size_1);
121  std::size_t size_2 = bytes_to_read - size_1;
122  memcpy(data + size_1, data_, size_2);
123  tail_ = size_2;
124  }
125 
126  size_ -= bytes_to_read;
127  return bytes_to_read;
128 }
std::size_t read(uint8_t *data, std::size_t bytes)
Returns number of bytes read.
ROSaicNodeBase * node_
Pointer to the node.
std::size_t capacity() const
Returns capacity_.
void log(LogLevel logLevel, const std::string &s)
Log function to provide abstraction of ROS loggers.
Definition: typedefs.hpp:231
std::size_t write(const uint8_t *data, std::size_t bytes)
Returns number of bytes written.
std::size_t size_
Number of bytes that have been written but not yet read.
CircularBuffer(ROSaicNodeBase *node, std::size_t capacity)
Constructor of CircularBuffer.
~CircularBuffer()
Destructor of CircularBuffer.
std::size_t capacity_
Capacity of the circular buffer.
std::size_t head_
Specifies where we start writing.
Declares a class for creating, writing to and reading from a circular bufffer.
std::size_t tail_
Specifies where we start reading.
This class is the base class for abstraction.
Definition: typedefs.hpp:174


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Sat Mar 11 2023 03:12:55