00001 #include "Counter.h"
00002 #include "math.h"
00003 #include <limits.h>
00004
00009
00013 Counter::Counter()
00014 {
00015 setMin(1);
00016 setMax(USHRT_MAX);
00017 setTo(1);
00018 }
00019
00020
00025 Counter::Counter(int maxNumber)
00026 {
00027 setMin(1);
00028 setMax(maxNumber);
00029 setTo(1);
00030 }
00031
00032
00036 Counter::Counter(int minNumber, int maxNumber)
00037 {
00038 setMin(minNumber);
00039 setMax(maxNumber);
00040 setTo(minNumber);
00041 }
00042
00043
00048 Counter::Counter(int minNumber, int maxNumber, int startValue)
00049 {
00050 setMin(minNumber);
00051 setMax(maxNumber);
00052 setTo(startValue);
00053 }
00054
00055
00059 Counter::Counter(const Counter& c) {
00060 m_count = c.m_count;
00061 m_maxValue = c.m_maxValue;
00062 m_minValue = c.m_minValue;
00063 }
00064
00065
00070 int
00071 Counter::increment()
00072 {
00073 ++m_count;
00074 m_count = m_count % m_maxValue;
00075 if (m_count == 0) {
00076 m_count = m_minValue;
00077 }
00078 return m_count;
00079 }
00080
00081
00086 int
00087 Counter::decrement()
00088 {
00089 --m_count;
00090 if (m_count == (m_minValue - 1) ) {
00091 m_count = m_maxValue;
00092 }
00093 return m_count;
00094 }
00095
00096
00101 void
00102 Counter::setTo(int value)
00103 {
00104 if ((value >= m_minValue) && (value <= m_maxValue)) {
00105 m_count = value;
00106 }
00107 else if (value > m_maxValue) {
00108 m_count = m_maxValue;
00109 }
00110 else if (value < m_minValue) {
00111 m_count = m_minValue;
00112 }
00113 }
00114
00115
00116 void
00117 Counter::setMax(int max) {
00118 if (max < 0) {
00119 m_maxValue = USHRT_MAX;
00120 }
00121 else {
00122 m_maxValue = max;
00123 }
00124 }
00125
00126
00127 void
00128 Counter::setMin(int min) {
00129 if (min < 0) {
00130 min = 1;
00131 }
00132 else {
00133 m_minValue = min;
00134 }
00135 }
00136
00137
00141 int
00142 Counter::getCount()
00143 {
00144 return m_count;
00145 }
00146
00147
00148 void
00149 Counter::toStream(std::ostream& out) {
00150 out << "Counter: " << m_count << " (Max: " << m_maxValue << ", Min: " << m_minValue << ") " << std::endl;
00151 }
00152