C++ circular_buffer: support types not implicitly convertible from int (#2350)

Also fixes two cases of returning a reference to a constant.
This commit is contained in:
Oblarg
2020-02-08 16:35:21 -05:00
committed by GitHub
parent 1b85066d26
commit 05b7593e66
2 changed files with 10 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -12,7 +12,7 @@
namespace wpi {
template <class T>
circular_buffer<T>::circular_buffer(size_t size) : m_data(size, 0) {}
circular_buffer<T>::circular_buffer(size_t size) : m_data(size, T{0}) {}
/**
* Returns number of elements in buffer
@@ -45,7 +45,7 @@ template <class T>
T& circular_buffer<T>::back() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0;
return zero_val;
}
return m_data[(m_front + m_length - 1) % m_data.size()];
@@ -58,7 +58,7 @@ template <class T>
const T& circular_buffer<T>::back() const {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0;
return zero_val;
}
return m_data[(m_front + m_length - 1) % m_data.size()];
@@ -110,7 +110,7 @@ template <class T>
T circular_buffer<T>::pop_front() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0;
return T{0};
}
T& temp = m_data[m_front];
@@ -126,7 +126,7 @@ template <class T>
T circular_buffer<T>::pop_back() {
// If there are no elements in the buffer, do nothing
if (m_length == 0) {
return 0;
return T{0};
}
m_length--;
@@ -191,7 +191,7 @@ void circular_buffer<T>::resize(size_t size) {
*/
template <class T>
void circular_buffer<T>::reset() {
std::fill(m_data.begin(), m_data.end(), 0);
std::fill(m_data.begin(), m_data.end(), T{0});
m_front = 0;
m_length = 0;
}