xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deModularCounter.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _DEMODULARCOUNTER_HPP
2 #define _DEMODULARCOUNTER_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements C++ Base Library
5  * -----------------------------
6  *
7  * Copyright 2023 Valve Corporation.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Modular counter helper class.
24  *//*--------------------------------------------------------------------*/
25 
26 #include <type_traits>
27 #include <cstdint>
28 
29 namespace de
30 {
31 
32 template <typename T>
33 class ModularCounter
34 {
35     static_assert(std::is_unsigned<T>::value, "Invalid underlying type");
36 
37 public:
38     typedef T value_type;
39 
ModularCounter(T period,T initialValue=T{0})40     explicit ModularCounter(T period, T initialValue = T{0}) : m_period(period), m_value(initialValue)
41     {
42     }
43 
operator ++()44     ModularCounter &operator++()
45     {
46         m_value = ((m_value + T{1}) % m_period);
47         return *this;
48     }
operator --()49     ModularCounter &operator--()
50     {
51         m_value = ((m_value - T{1}) % m_period);
52         return *this;
53     }
operator ++(int)54     ModularCounter operator++(int)
55     {
56         ModularCounter ret(*this);
57         ++(*this);
58         return ret;
59     }
operator --(int)60     ModularCounter operator--(int)
61     {
62         ModularCounter ret(*this);
63         --(*this);
64         return ret;
65     }
operator T(void)66     operator T(void)
67     {
68         return m_value;
69     }
70 
71 protected:
72     const T m_period;
73     T m_value;
74 };
75 
76 using ModCounter64 = ModularCounter<uint64_t>;
77 using ModCounter32 = ModularCounter<uint32_t>;
78 
79 } // namespace de
80 
81 #endif // _DEMODULARCOUNTER_HPP
82