00001 #ifndef _CYCLIC_NUMBER_H
00002 #define _CYCLIC_NUMBER_H
00003
00004 #include "utils.h"
00005 #include "RangedNumber.h"
00006
00007 namespace luma
00008 {
00009 namespace numbers
00010 {
00011
00028 template <class T>
00029 class CyclicNumber: public RangedNumber<T>
00030 {
00031
00032 public:
00033 CyclicNumber(T value, T min, T max, T increment);
00034
00035
00036 CyclicNumber<T>& operator=(const CyclicNumber<T>& other);
00037 CyclicNumber<T>& operator=(const T& value);
00043 CyclicNumber<T>& operator++();
00044
00050 CyclicNumber<T> operator++(int);
00051
00057 CyclicNumber<T>& operator--();
00058
00064 CyclicNumber<T> operator--(int);
00065
00070 CyclicNumber<T>& operator+=(const T& increment);
00071
00076 CyclicNumber<T>& operator-=(const T& decrement);
00077
00078 virtual T getValidValue(const T& value) const;
00079
00080 virtual void dec(float elapsedTime = 1);
00081 virtual void inc(float elapsedTime = 1);
00082 };
00083
00084 template <class T>
00085 CyclicNumber<T>::CyclicNumber(T value, T min, T max, T increment):
00086 RangedNumber(mod(value, min, max), min, max, increment)
00087 {
00088 }
00089
00090 template <class T>
00091 CyclicNumber<T>& CyclicNumber<T>::operator=(const CyclicNumber<T>& other)
00092 {
00093 this->RangedNumber::operator=(other);
00094
00095
00096
00097 return *this;
00098 }
00099
00100 template <class T>
00101 CyclicNumber<T>& CyclicNumber<T>::operator=(const T& value)
00102 {
00103 mValue = mod((T) value, mMin, mMax);
00104
00105 return *this;
00106 }
00107
00108 template <class T>
00109 CyclicNumber<T>& CyclicNumber<T>::operator++()
00110 {
00111 inc();
00112
00113 return *this;
00114 }
00115
00116 template <class T>
00117 CyclicNumber<T> CyclicNumber<T>::operator++(int)
00118 {
00119 CyclicNumber<T> tmp = *this;
00120 ++*this;
00121
00122 return tmp;
00123 }
00124
00125 template <class T>
00126 CyclicNumber<T>& CyclicNumber<T>::operator--()
00127 {
00128 dec();
00129
00130 return *this;
00131 }
00132
00133 template <class T>
00134 CyclicNumber<T> CyclicNumber<T>::operator--(int)
00135 {
00136 CyclicNumber<T> tmp = *this;
00137 --*this;
00138
00139 return tmp;
00140 }
00141
00142 template <class T>
00143 CyclicNumber<T>& CyclicNumber<T>::operator+=(const T& increment)
00144 {
00145 mValue += increment;
00146 mValue = mod(mValue, mMin, mMax);
00147
00148 return *this;
00149 }
00150
00151 template <class T>
00152 CyclicNumber<T>& CyclicNumber<T>::operator-=(const T& increment)
00153 {
00154 mValue -= increment;
00155 mValue = mod(mValue, mMin, mMax);
00156
00157 return *this;
00158 }
00159
00160 template <class T>
00161 T CyclicNumber<T>::getValidValue(const T& value) const
00162 {
00163 return mod(value, mMin, mMax);
00164 }
00165
00166 template <class T>
00167 void CyclicNumber<T>::inc(float ellapsedTime)
00168 {
00169 mValue += (T)(mIncrement * ellapsedTime);
00170 mValue = mod(mValue, mMin, mMax);
00171 }
00172
00173 template <class T>
00174 void CyclicNumber<T>::dec(float ellapsedTime)
00175 {
00176 mValue -= (T) (mIncrement * ellapsedTime);
00177 mValue = mod(mValue, mMin, mMax);
00178 }
00179
00180 }}
00181 #endif //_CYCLIC_NUMBER_H