#ifndef SEEN_PERCENT_H #define SEEN_PERCENT_H #include#include namespace Seen { /** * @brief Header only implementation of an Percent class */ class Percent : boost::operators { public: Percent(int value) : m_value(value) { truncate(); } Percent & operator= (Percent const & other) { if (this == &other) { return *this; } m_value = other.m_value; return *this; } Percent & operator= (int value) { m_value = value; truncate(); return *this; } bool operator<(Percent const & other) const { return m_value < other.m_value; } bool operator==(Percent const & other) const { return m_value == other.m_value; } Percent & operator+= (Percent const & other) { m_value += other.m_value; truncate(); return *this; } Percent & operator-= (Percent const & other) { m_value -= other.m_value; truncate(); return *this; } Percent & operator*= (Percent const & other) { m_value *= other.m_value; m_value /= 100; return *this; } Percent & operator/= (Percent const & other) { m_value /= other.m_value; return *this; } Percent & operator%= (Percent const & other) { m_value %= other.m_value; return *this; } Percent & operator|= (Percent const & other) { m_value |= other.m_value; return *this; } Percent & operator&= (Percent const & other) { m_value &= other.m_value; return *this; } Percent & operator^= (Percent const & other) { m_value ^= other.m_value; return *this; } Percent & operator++() { m_value ++; truncate(); return *this; } Percent & operator--() { m_value --; truncate(); return *this; } /** * @brief Truncates the value between 0 and 100 */ void truncate() { if (m_value < 0) m_value = 0; else if (m_value > 100) m_value = 100; } explicit operator int() const { return m_value; } private: int m_value; }; std::ostream & operator<<(std::ostream & os, Percent const & percent); std::istream & operator>>(std::istream & is, Percent & percent); } #endif