1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//-----------------------------------------------------------------------------
/** @file libpentobi_base/Color.h
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBPENTOBI_BASE_COLOR_H
#define LIBPENTOBI_BASE_COLOR_H
#include <cstdint>
#include "libboardgame_base/Assert.h"
namespace libpentobi_base {
using namespace std;
//-----------------------------------------------------------------------------
class Color
{
public:
using IntType = uint_fast8_t;
class Iterator
{
public:
explicit Iterator(IntType i) { m_i = i; }
bool operator==(Iterator it) const { return m_i == it.m_i; }
bool operator!=(Iterator it) const { return m_i != it.m_i; }
void operator++() { ++m_i; }
Color operator*() const { return Color(m_i); }
private:
IntType m_i;
};
class Range
{
public:
explicit Range(IntType nu_colors)
: m_nu_colors(nu_colors)
{ }
Iterator begin() const { return Iterator(0); }
Iterator end() const { return Iterator(m_nu_colors); }
private:
IntType m_nu_colors;
};
static constexpr IntType range = 4;
Color();
explicit Color(IntType i);
bool operator==(Color c) const;
bool operator!=(Color c) const { return ! operator==(c); }
bool operator<(Color c) const;
IntType to_int() const;
Color get_next(IntType nu_colors) const;
Color get_previous(IntType nu_colors) const;
private:
static constexpr IntType value_uninitialized = range;
IntType m_i;
#ifdef LIBBOARDGAME_DEBUG
bool is_initialized() const { return m_i < value_uninitialized; }
#endif
};
inline Color::Color()
{
#ifdef LIBBOARDGAME_DEBUG
m_i = value_uninitialized;
#endif
}
inline Color::Color(IntType i)
{
LIBBOARDGAME_ASSERT(i < range);
m_i = i;
}
inline bool Color::operator==(Color c) const
{
LIBBOARDGAME_ASSERT(is_initialized());
LIBBOARDGAME_ASSERT(c.is_initialized());
return m_i == c.m_i;
}
inline bool Color::operator<(Color c) const
{
LIBBOARDGAME_ASSERT(is_initialized());
LIBBOARDGAME_ASSERT(c.is_initialized());
return m_i < c.m_i;
}
inline Color Color::get_next(IntType nu_colors) const
{
return Color(static_cast<IntType>(m_i + 1) % nu_colors);
}
inline Color Color::get_previous(IntType nu_colors) const
{
return Color(static_cast<IntType>(m_i + nu_colors - 1) % nu_colors);
}
inline Color::IntType Color::to_int() const
{
LIBBOARDGAME_ASSERT(is_initialized());
return m_i;
}
//-----------------------------------------------------------------------------
/** Unrolled loop over all colors. */
template<class FUNCTION>
inline void for_each_color(FUNCTION f)
{
static_assert(Color::range == 4);
f(Color(0));
f(Color(1));
f(Color(2));
f(Color(3));
}
//-----------------------------------------------------------------------------
} // namespace libpentobi_base
#endif // LIBPENTOBI_BASE_COLOR_H
|