diff --git a/wpiutil/src/main/native/include/wpi/circular_buffer.h b/wpiutil/src/main/native/include/wpi/circular_buffer.h index 097d5f65aa..5dc12f8151 100644 --- a/wpiutil/src/main/native/include/wpi/circular_buffer.h +++ b/wpiutil/src/main/native/include/wpi/circular_buffer.h @@ -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. */ @@ -47,6 +47,8 @@ class circular_buffer { private: std::vector m_data; + T zero_val{0}; + // Index of element at front of buffer size_t m_front = 0; diff --git a/wpiutil/src/main/native/include/wpi/circular_buffer.inc b/wpiutil/src/main/native/include/wpi/circular_buffer.inc index 7f029e49b9..20690b7bcf 100644 --- a/wpiutil/src/main/native/include/wpi/circular_buffer.inc +++ b/wpiutil/src/main/native/include/wpi/circular_buffer.inc @@ -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 -circular_buffer::circular_buffer(size_t size) : m_data(size, 0) {} +circular_buffer::circular_buffer(size_t size) : m_data(size, T{0}) {} /** * Returns number of elements in buffer @@ -45,7 +45,7 @@ template T& circular_buffer::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 const T& circular_buffer::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 T circular_buffer::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 T circular_buffer::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::resize(size_t size) { */ template void circular_buffer::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; }