SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
limitedcounter.h
Go to the documentation of this file.
1 // Copyright (C) 2018 Edouard Griffiths, F4EXB. //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #ifndef SDRBASE_UTIL_LIMITEDCOUNTER_H_
19 #define SDRBASE_UTIL_LIMITEDCOUNTER_H_
20 
21 #include <stdint.h>
22 
23 template<typename StoreType, uint32_t Limit>
25 {
26 public:
28  m_counter(0)
29  {}
30 
31  LimitedCounter(StoreType value) :
32  m_counter(value < Limit ? value : Limit - 1)
33  {}
34 
35  void operator=(const LimitedCounter& rhs) {
36  m_counter = rhs.m_counter;
37  }
38 
40  m_counter++;
41  if (m_counter >= Limit) {
42  m_counter = 0;
43  }
44  return *this;
45  }
46 
48  LimitedCounter temp = *this ;
49  m_counter++;
50  if (m_counter >= Limit) {
51  m_counter = 0;
52  }
53  return temp;
54  }
55 
57  m_counter += rhs;
58  if (m_counter >= Limit) {
59  m_counter -= Limit;
60  }
61  return *this;
62  }
63 
64  StoreType value() const { return m_counter; }
65 
66  friend LimitedCounter operator-(const LimitedCounter lhs, const LimitedCounter& rhs)
67  {
68  LimitedCounter result;
69 
70  if (lhs.m_counter < rhs.m_counter) {
71  result.m_counter = Limit - lhs.m_counter + rhs.m_counter + 1;
72  } else {
73  result.m_counter = lhs.m_counter - rhs.m_counter;
74  }
75 
76  return result;
77  }
78 
79 private:
80  StoreType m_counter;
81 };
82 
83 
84 
85 #endif /* SDRBASE_UTIL_LIMITEDCOUNTER_H_ */
friend LimitedCounter operator-(const LimitedCounter lhs, const LimitedCounter &rhs)
unsigned int uint32_t
Definition: rtptypes_win.h:46
StoreType value() const
LimitedCounter & operator+=(const uint32_t rhs)
LimitedCounter & operator++()
LimitedCounter(StoreType value)
StoreType m_counter
void operator=(const LimitedCounter &rhs)
LimitedCounter operator++(int)