59 lines
1.5 KiB
C++
59 lines
1.5 KiB
C++
/**
|
|
* @file rotatingIndex.hpp
|
|
* @author Gustice
|
|
* @brief Classes for typical index manipulation
|
|
*
|
|
* @copyright Copyright (c) 2020
|
|
*/
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief Index with automatic revolution back to start position
|
|
* @details Useful for Buffer-Pointer, or similar tasks
|
|
*/
|
|
class RotatingIndex {
|
|
public:
|
|
/// @brief Value at witch the index rotates back to beginning
|
|
const uint16_t MaxIndex;
|
|
|
|
/// @brief Constructor for rotating index
|
|
/// @brief output index will be set to 0 if incremented value reaches Maximum
|
|
/// @param max Maximum index for overflow-condition
|
|
RotatingIndex(uint16_t max) : MaxIndex(max) { _index = 0; }
|
|
|
|
/// @brief Get the current index and inkrement index afterwards
|
|
/// @return uint16_t current index
|
|
uint16_t getIndexAndIncrement(void) {
|
|
auto retVal = _index;
|
|
_index++;
|
|
if (_index >= MaxIndex) {
|
|
_index = 0;
|
|
}
|
|
return retVal;
|
|
};
|
|
|
|
/// @brief Get the current index without incrementing the index
|
|
/// @return uint16_t current index
|
|
uint16_t getIndex(void) { return _index; }
|
|
|
|
/// @brief Check if next get will lead to overflow condition
|
|
bool peekRevolution(void) {
|
|
if (MaxIndex == 0)
|
|
return true;
|
|
|
|
if (_index >= MaxIndex - 1) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// @brief Reset Index to 0
|
|
void resetIndex(void) { _index = 0; }
|
|
|
|
private:
|
|
/// Current Index
|
|
uint16_t _index;
|
|
};
|