Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 class debug_count
66 {
67 public:
68 debug_count()
69 : count(0)
70 {
71 }
72
73 ~debug_count()
74 {
75 assert(count == 0);
76 }
77
78 debug_count& operator++()
79 {
80 ++count;
81 return *this;
82 }
83
84 debug_count& operator--()
85 {
86 --count;
87 assert(count >= 0);
88 return *this;
89 }
90
91 template <typename T>
92 debug_count& operator+=(T n)
93 {
94 count += int32_t(n);
95 return *this;
96 }
97
98 template <typename T>
99 debug_count& operator-=(T n)
100 {
101 count -= int32_t(n);
102 return *this;
103 }
104
105 debug_count& operator=(const debug_count& other)
106 {
107 count.store(other.count.load());
108
109 return *this;
110 }
111
112 #if ETL_HAS_ATOMIC
113 void swap(debug_count& other) ETL_NOEXCEPT // NOT ATOMIC
114 {
115 int32_t temp = other.count.load();
116 other.count.store(count.load());
117 count.store(temp);
118 }
119 #else
120 void swap(debug_count& other) ETL_NOEXCEPT
121 {
122 swap(count, other.count);
123 }
124 #endif
125
126 operator int32_t() const
127 {
128 return count;
129 }
130
131 int32_t get() const
132 {
133 return int32_t(count);
134 }
135
136 void set(int32_t n)
137 {
138 count = n;
139 }
140
141 void clear()
142 {
143 count = 0;
144 }
145
146 private:
147 #if ETL_HAS_ATOMIC
149 #else
150 int32_t count;
151 #endif
152 };
153} // namespace etl
154
155inline void swap(etl::debug_count& lhs, etl::debug_count& rhs)
156{
157 lhs.swap(rhs);
158}
159
160#else
161 #define ETL_DECLARE_DEBUG_COUNT etl::debug_count etl_debug_count
162 #define ETL_SET_DEBUG_COUNT(n) ETL_DO_NOTHING
163 #define ETL_GET_DEBUG_COUNT ETL_DO_NOTHING
164 #define ETL_INCREMENT_DEBUG_COUNT ETL_DO_NOTHING
165 #define ETL_DECREMENT_DEBUG_COUNT ETL_DO_NOTHING
166 #define ETL_ADD_DEBUG_COUNT(n) ETL_DO_NOTHING
167 #define ETL_SUBTRACT_DEBUG_COUNT(n) ETL_DO_NOTHING
168 #define ETL_RESET_DEBUG_COUNT ETL_DO_NOTHING
169 #define ETL_OBJECT_RESET_DEBUG_COUNT(object) ETL_DO_NOTHING
170 #define ETL_OBJECT_GET_DEBUG_COUNT(object) ETL_DO_NOTHING
171
172namespace etl
173{
175 {
176 };
177}
178#endif // ETL_DEBUG_COUNT
179
180#endif
Definition debug_count.h:175
bitset_ext
Definition absolute.h:38
T & get(array< T, MAXN > &a)
Definition array.h:719
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition array.h:630
pair holds two objects of arbitrary type
Definition utility.h:164