00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef SHTTL_COUNTER_H
00037 #define SHTTL_COUNTER_H
00038
00039 #include <stdexcept>
00040
00041 #include "bit.h"
00042
00043 namespace shttl
00044 {
00045
00046
00047
00048
00049
00050
00051 template < typename value_type = u8, typename value_body_type = value_type >
00052 class counter_base
00053 {
00054
00055 public:
00056 explicit counter_base(
00057 value_body_type value,
00058 value_type initv = value_type(),
00059 value_type min = 0,
00060 value_type max = 3,
00061 value_type add = 1,
00062 value_type sub = 1,
00063 value_type threshold = 0
00064 ) :
00065 m_value( value ),
00066 m_initv( initv ),
00067 m_min ( min ),
00068 m_max ( max ),
00069 m_add ( add ),
00070 m_sub ( sub ),
00071 m_threshold( threshold )
00072 {
00073
00074
00075
00076
00077 }
00078
00079 value_type initv() const
00080 {
00081 return m_initv;
00082 }
00083
00084 value_type min() const
00085 {
00086 return m_min;
00087 }
00088
00089 value_type max() const
00090 {
00091 return m_max;
00092 }
00093
00094 value_type threshold() const
00095 {
00096 return m_threshold;
00097 }
00098
00099
00100 operator value_type() const
00101 {
00102 return m_value;
00103 }
00104
00105 void set(
00106 value_type initv = value_type(),
00107 value_type min = 0,
00108 value_type max = 3,
00109 value_type add = 1,
00110 value_type sub = 1,
00111 value_type threshold = 0
00112 )
00113 {
00114 m_initv = initv;
00115 m_min = min;
00116 m_max = max;
00117 m_add = add;
00118 m_sub = sub;
00119 m_threshold = threshold;
00120 }
00121
00122 void reset()
00123 {
00124 m_value = m_initv;
00125 }
00126
00127 value_type inc()
00128 {
00129 int value = m_value;
00130 value += m_add;
00131 if( value > m_max ){
00132 value = m_max;
00133 }
00134 m_value = (value_type)value;
00135 return m_value;
00136 }
00137
00138 value_type dec()
00139 {
00140 int value = m_value;
00141 value -= m_sub;
00142 if( value < m_min ){
00143 value = m_min;
00144 }
00145 m_value = (value_type)value;
00146 return m_value;
00147 }
00148
00149
00150 bool above_threshold() const
00151 {
00152 return m_value >= m_threshold;
00153 }
00154
00155 protected:
00156 value_body_type m_value;
00157 value_type m_initv;
00158 value_type m_min;
00159 value_type m_max;
00160 value_type m_add;
00161 value_type m_sub;
00162 value_type m_threshold;
00163
00164 };
00165
00166 template < typename value_type = u8 >
00167 class counter : public counter_base< value_type, value_type >
00168 {
00169
00170 public:
00171 counter(
00172 value_type initv = value_type(),
00173 value_type min = 0,
00174 value_type max = 3,
00175 value_type add = 1,
00176 value_type sub = 1,
00177 value_type threshold = 0
00178 ) :
00179 base_type( initv, initv, min, max, add, sub, threshold )
00180 {
00181 }
00182
00183 protected:
00184 typedef counter_base< value_type, value_type > base_type;
00185
00186 };
00187
00188
00189 }
00190
00191 #endif // SHTTL_COUNTER_H