/src/connectedhomeip/src/lib/support/CHIPCounter.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2020 Project CHIP Authors |
4 | | * Copyright (c) 2016-2017 Nest Labs, Inc. |
5 | | * All rights reserved. |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | /** |
21 | | * @file |
22 | | * |
23 | | * @brief |
24 | | * Class declarations for a Counter base class, and a monotonically-increasing counter. |
25 | | */ |
26 | | |
27 | | #pragma once |
28 | | |
29 | | #include <lib/core/CHIPError.h> |
30 | | |
31 | | namespace chip { |
32 | | |
33 | | /** |
34 | | * @class Counter |
35 | | * |
36 | | * @brief |
37 | | * An interface for managing a counter as an integer value. |
38 | | */ |
39 | | |
40 | | template <typename T> |
41 | | class Counter |
42 | | { |
43 | | public: |
44 | 0 | Counter() {} |
45 | 0 | virtual ~Counter() {} |
46 | | |
47 | | /** |
48 | | * @brief Advance the value of the counter. |
49 | | * |
50 | | * @return A CHIP error code if anything failed, CHIP_NO_ERROR otherwise. |
51 | | */ |
52 | | virtual CHIP_ERROR Advance() = 0; |
53 | | |
54 | | /** |
55 | | * @brief Advances the current counter value by N |
56 | | * |
57 | | * @param value value of N |
58 | | * |
59 | | * @return A CHIP error code if anything failed, CHIP_NO_ERROR otherwise. |
60 | | */ |
61 | | virtual CHIP_ERROR AdvanceBy(T value) = 0; |
62 | | |
63 | | /** |
64 | | * @brief Get the current value of the counter. |
65 | | * |
66 | | * @return The current value of the counter. |
67 | | */ |
68 | | virtual T GetValue() = 0; |
69 | | }; |
70 | | |
71 | | /** |
72 | | * @class MonotonicallyIncreasingCounter |
73 | | * |
74 | | * @brief |
75 | | * A class for managing a monotonically-increasing counter as an integer value. |
76 | | */ |
77 | | |
78 | | template <typename T> |
79 | | class MonotonicallyIncreasingCounter : public Counter<T> |
80 | | { |
81 | | public: |
82 | 0 | MonotonicallyIncreasingCounter() : mCounterValue(0) {} |
83 | | ~MonotonicallyIncreasingCounter() override{}; |
84 | | |
85 | | /** |
86 | | * @brief Initialize a MonotonicallyIncreasingCounter object. |
87 | | * |
88 | | * @param[in] aStartValue The starting value of the counter. |
89 | | * |
90 | | * @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise |
91 | | */ |
92 | | CHIP_ERROR Init(T aStartValue) |
93 | 0 | { |
94 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
95 | |
|
96 | 0 | mCounterValue = aStartValue; |
97 | |
|
98 | 0 | return err; |
99 | 0 | } |
100 | | |
101 | | /** |
102 | | * @brief Advance the value of the counter. |
103 | | * |
104 | | * @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise |
105 | | */ |
106 | | CHIP_ERROR Advance() override |
107 | 0 | { |
108 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
109 | |
|
110 | 0 | mCounterValue++; |
111 | |
|
112 | 0 | return err; |
113 | 0 | } Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned int>::Advance() Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned long>::Advance() |
114 | | |
115 | | /** |
116 | | * @brief Advances the current counter value by N |
117 | | * |
118 | | * @param value value of N |
119 | | * |
120 | | * @return A CHIP error code if something fails, CHIP_NO_ERROR otherwise |
121 | | */ |
122 | | CHIP_ERROR AdvanceBy(T value) override |
123 | 0 | { |
124 | 0 | mCounterValue = static_cast<T>(mCounterValue + value); |
125 | 0 | return CHIP_NO_ERROR; |
126 | 0 | } Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned int>::AdvanceBy(unsigned int) Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned long>::AdvanceBy(unsigned long) |
127 | | |
128 | | /** |
129 | | * @brief Get the current value of the counter. |
130 | | * |
131 | | * @return The current value of the counter. |
132 | | */ |
133 | 0 | T GetValue() override { return mCounterValue; }Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned int>::GetValue() Unexecuted instantiation: chip::MonotonicallyIncreasingCounter<unsigned long>::GetValue() |
134 | | |
135 | | protected: |
136 | | T mCounterValue; |
137 | | }; |
138 | | |
139 | | } // namespace chip |