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  {
63  "You are trying to overwrite parts of the circular buffer that have not yet been read!");
64  }
65 
66  // Writes in a single step
67  if (bytes_to_write <= capacity - head_)
68  {
69 
70  memcpy(data_ + head_, data, bytes_to_write);
71  head_ += bytes_to_write;
72  if (head_ == capacity)
73  head_ = 0;
74  }
75  // Writes in two steps. Here the circular nature comes to the surface
76  else
77  {
78  std::size_t size_1 = capacity - head_;
79  memcpy(data_ + head_, data, size_1);
80  std::size_t size_2 = bytes_to_write - size_1;
81  memcpy(data_, data + size_1, size_2);
82  head_ =
83  size_2; // Hence setting head_ = 0 three lines above was not necessary.
84  }
85  size_ += bytes_to_write;
86  return bytes_to_write;
87 }
88 
89 std::size_t CircularBuffer::read(uint8_t* data, std::size_t bytes)
90 {
91  if (bytes == 0)
92  return 0;
93  std::size_t capacity = capacity_;
94  std::size_t bytes_to_read = std::min(bytes, size_);
95  if (bytes_to_read != bytes)
96  {
98  "You are trying to read parts of the circular buffer that have not yet been written!");
99  }
100 
101  // Read in a single step
102  if (bytes_to_read <=
103  capacity - tail_) // Note that it is not size_ - tail_:
104  // If write() hasn't written something into all of capacity
105  // yet (first round of writing), we would still read those
106  // unknown bytes..
107  {
108  memcpy(data, data_ + tail_, bytes_to_read);
109  tail_ += bytes_to_read;
110  if (tail_ == capacity)
111  tail_ = 0; // Same here?
112  }
113 
114  // Read in two steps
115  else
116  {
117  std::size_t size_1 = capacity - tail_;
118  memcpy(data, data_ + tail_, size_1);
119  std::size_t size_2 = bytes_to_read - size_1;
120  memcpy(data + size_1, data_, size_2);
121  tail_ = size_2;
122  }
123 
124  size_ -= bytes_to_read;
125  return bytes_to_read;
126 }
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:208
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:160


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Thu Jun 23 2022 02:11:38