gunkiss/src/Carousel.hpp

86 lines
2.3 KiB
C++

/* _______________ ,----------------------------------------------------------------.
//`````````````\\ \ \
//~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \
//=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \
// \\ \ \
// \\ \ code released under the zlib license [git.nugget.fun/pudding] \
// ☆ GUNKISS ☆ \\ \ \
//_________________________\\ `---------------------------------------------------------------*/
#ifndef CAROUSEL_H_
#define CAROUSEL_H_
#include <cstdint>
#include <iterator>
#include "utility.hpp"
class Carousel
{
private:
std::uint32_t offset = 0;
public:
template<typename Container>
auto current(Container& container) const
{
auto location = container.begin();
if (location != container.end())
{
std::advance(location, offset);
return location;
}
else
{
throw std::out_of_range("Container is empty");
}
}
template<typename Container>
void next(const Container& container)
{
offset = ++offset % container.size();
}
template<typename Container>
void previous(const Container& container)
{
offset = sb::mod(--offset, container.size());
}
template<typename Container>
void increment(const Container& container, int amount)
{
offset = sb::mod(offset + amount, container.size());
}
void beginning()
{
offset = 0;
}
template<typename Container>
void end(const Container& container)
{
offset = container.size() - 1;
}
/* Return true if the carousel currently points to the location at the beginning of the container. */
bool at_beginning() const
{
return offset == 0;
}
/* Return true if the carousel currently points to the location at the end of the container. */
template<typename Container>
bool at_end(const Container& container)
{
return offset >= container.size() - 1;
}
};
#endif