Embedded Template Library 1.0
debug_count.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2017 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_DEBUG_COUNT_INCLUDED
32#define ETL_DEBUG_COUNT_INCLUDED
33
34#include "platform.h"
35#include "atomic.h"
36
37#include <assert.h>
38#include <stdint.h>
39
42
43#if defined(ETL_DEBUG_COUNT)
44
45 #define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count;
46 #define ETL_SET_DEBUG_COUNT(n) etl_debug_count.set(n)
47 #define ETL_GET_DEBUG_COUNT etl_debug_count.get()
48 #define ETL_INCREMENT_DEBUG_COUNT ++etl_debug_count;
49 #define ETL_DECREMENT_DEBUG_COUNT --etl_debug_count;
50 #define ETL_ADD_DEBUG_COUNT(n) etl_debug_count += (n);
51 #define ETL_SUBTRACT_DEBUG_COUNT(n) etl_debug_count -= (n);
52 #define ETL_RESET_DEBUG_COUNT etl_debug_count.clear();
53 #define ETL_OBJECT_RESET_DEBUG_COUNT(object) object.etl_debug_count.clear();
54 #define ETL_OBJECT_GET_DEBUG_COUNT(object) object.etl_debug_count.get()
55
56namespace etl
57{
58 //***************************************************************************
64 //***************************************************************************
65
66
67 class debug_count
68 {
69 public:
70 debug_count()
71 : count(0)
72 {
73 }
74
75 ~debug_count()
76 {
77 assert(count == 0);
78 }
79
80 debug_count& operator++()
81 {
82 ++count;
83 return *this;
84 }
85
86 debug_count& operator--()
87 {
88 --count;
89 assert(count >= 0);
90 return *this;
91 }
92
93 template <typename T>
94 debug_count& operator+=(T n)
95 {
96 count += int32_t(n);
97 return *this;
98 }
99
100 template <typename T>
101 debug_count& operator-=(T n)
102 {
103 count -= int32_t(n);
104 return *this;
105 }
106
107 debug_count& operator=(const debug_count& other)
108 {
109 count.store(other.count.load());
110
111 return *this;
112 }
113
114 #if ETL_HAS_ATOMIC
115 void swap(debug_count& other) ETL_NOEXCEPT // NOT ATOMIC
116 {
117 int32_t temp = other.count.load();
118 other.count.store(count.load());
119 count.store(temp);
120 }
121 #else
122 void swap(debug_count& other) ETL_NOEXCEPT
123 {
124 swap(count, other.count);
125 }
126 #endif
127
128 operator int32_t() const
129 {
130 return count;
131 }
132
133 int32_t get() const
134 {
135 return int32_t(count);
136 }
137
138 void set(int32_t n)
139 {
140 count = n;
141 }
142
143 void clear()
144 {
145 count = 0;
146 }
147
148 private:
149 #if ETL_HAS_ATOMIC
150 etl::atomic_int32_t count;
151 #else
152 int32_t count;
153 #endif
154 };
155} // namespace etl
156
157
158inline void swap(etl::debug_count& lhs, etl::debug_count& rhs)
159{
160 lhs.swap(rhs);
161}
162
163#else
164 #define ETL_DECLARE_DEBUG_COUNT
165 #define ETL_SET_DEBUG_COUNT(n)
166 #define ETL_GET_DEBUG_COUNT
167 #define ETL_INCREMENT_DEBUG_COUNT
168 #define ETL_DECREMENT_DEBUG_COUNT
169 #define ETL_ADD_DEBUG_COUNT(n)
170 #define ETL_SUBTRACT_DEBUG_COUNT(n)
171 #define ETL_RESET_DEBUG_COUNT
172 #define ETL_OBJECT_RESET_DEBUG_COUNT(object)
173 #define ETL_OBJECT_GET_DEBUG_COUNT(object)
174#endif // ETL_DEBUG_COUNT
175
176#endif
bitset_ext
Definition: absolute.h:38
T & get(array< T, MAXN > &a)
Definition: array.h:710
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition: array.h:621