Embedded Template Library 1.0
Loading...
Searching...
No Matches
buffer_descriptors.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) 2020 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_BUFFER_DESCRIPTORS_INCLUDED
32#define ETL_BUFFER_DESCRIPTORS_INCLUDED
33
34#include "platform.h"
35#include "algorithm.h"
36#include "array.h"
37#include "cyclic_value.h"
38#include "delegate.h"
39#include "static_assert.h"
40#include "type_traits.h"
41
42#include <cstring>
43
44#if ETL_USING_CPP11
45
46namespace etl
47{
48 //***************************************************************************
50 //***************************************************************************
51 template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag = bool>
52 class buffer_descriptors
53 {
54 private:
55
56 struct descriptor_item;
57
58 public:
59
60 typedef TBuffer value_type;
61 typedef value_type* pointer;
62 typedef size_t size_type;
63 typedef TFlag flag_type;
64
65 static ETL_CONSTANT size_type N_BUFFERS = N_BUFFERS_;
66 static ETL_CONSTANT size_type BUFFER_SIZE = BUFFER_SIZE_;
67
68 //*********************************
70 //*********************************
71 class descriptor
72 {
73 public:
74
75 friend class buffer_descriptors;
76
77 static ETL_CONSTANT size_type MAX_SIZE = buffer_descriptors::BUFFER_SIZE;
78
79 //*********************************
80 descriptor()
81 : pdesc_item(ETL_NULLPTR)
82 {
83 }
84
85 //*********************************
86 descriptor(const descriptor& other)
87 : pdesc_item(other.pdesc_item)
88 {
89 }
90
91 //*********************************
92 descriptor& operator=(const descriptor& other)
93 {
94 pdesc_item = other.pdesc_item;
95 return *this;
96 }
97
98 //*********************************
99 pointer data() const
100 {
101 assert(pdesc_item != ETL_NULLPTR);
103 return pdesc_item->pbuffer;
104 #include "private/diagnostic_pop.h"
105 }
106
107 //*********************************
108 ETL_NODISCARD ETL_CONSTEXPR size_type max_size() const
109 {
110 return BUFFER_SIZE;
111 }
112
113 //*********************************
114 ETL_NODISCARD
115 bool is_allocated() const
116 {
117 return bool(pdesc_item->in_use);
118 }
119
120 //*********************************
121 ETL_NODISCARD
122 bool is_released() const
123 {
124 return bool(!pdesc_item->in_use);
125 }
126
127 //*********************************
128 ETL_NODISCARD
129 bool is_valid() const
130 {
131 return pdesc_item != ETL_NULLPTR;
132 }
133
134 //*********************************
135 void release()
136 {
137 pdesc_item->in_use = false;
138 }
139
140 private:
141
142 //*********************************
143 descriptor(descriptor_item* pdesc_item_)
144 : pdesc_item(pdesc_item_)
145 {
146 }
147
148 //*********************************
149 void allocate()
150 {
151 pdesc_item->in_use = true;
152 }
153
155 descriptor_item* pdesc_item;
156 };
157
158 //*********************************
160 //*********************************
161 class notification
162 {
163 public:
164
165 //*********************************
166 notification()
167 : desc()
168 , count(0U)
169 {
170 }
171
172 //*********************************
173 notification(descriptor desc_, size_t count_)
174 : desc(desc_)
175 , count(count_)
176 {
177 }
178
179 //*********************************
180 ETL_NODISCARD
181 descriptor get_descriptor() const
182 {
183 return desc;
184 }
185
186 //*********************************
187 ETL_NODISCARD
188 size_t get_count() const
189 {
190 return count;
191 }
192
193 private:
194
195 descriptor desc;
196 size_t count;
197 };
198
199 // The type of the callback function.
200 typedef etl::delegate<void(notification)> callback_type;
201
202 //*********************************
203 buffer_descriptors(TBuffer* pbuffers_, callback_type callback_ = callback_type())
204 : callback(callback_)
205 {
206 for (size_t i = 0UL; i < N_BUFFERS; ++i)
207 {
208 descriptor_items[i].pbuffer = pbuffers_ + (i * BUFFER_SIZE);
209 descriptor_items[i].in_use = false;
210 }
211 }
212
213 //*********************************
214 void set_callback(const callback_type& callback_)
215 {
216 callback = callback_;
217 }
218
219 //*********************************
220 void clear()
221 {
222 for (size_t i = 0UL; i < N_BUFFERS; ++i)
223 {
224 descriptor_items[i].in_use = false;
225 }
226
227 next.to_first();
228 }
229
230 //*********************************
231 ETL_NODISCARD
232 bool is_valid() const
233 {
234 return callback.is_valid();
235 }
236
237 //*********************************
238 void notify(notification n)
239 {
240 // Do we have a valid callback?
241 if (callback.is_valid())
242 {
243 callback(n);
244 }
245 }
246
247 //*********************************
248 ETL_NODISCARD
249 descriptor allocate()
250 {
251 descriptor desc(&descriptor_items[next]);
252
253 if (desc.is_released())
254 {
255 ++next;
256
257 desc.allocate();
258
259 return desc;
260 }
261 else
262 {
263 return descriptor();
264 }
265 }
266
267 //*********************************
268 ETL_NODISCARD
269 descriptor allocate(value_type fill_)
270 {
271 descriptor desc = allocate();
272
273 if (desc.is_valid())
274 {
275 etl::fill_n(desc.data(), BUFFER_SIZE, fill_);
276 }
277
278 return desc;
279 }
280
281 private:
282
283 //*********************************
284 struct descriptor_item
285 {
286 pointer pbuffer;
287 volatile flag_type in_use;
288 };
289
290 callback_type callback;
291 etl::array<descriptor_item, N_BUFFERS> descriptor_items;
292 etl::cyclic_value<uint_least8_t, 0U, N_BUFFERS - 1> next;
293 };
294
295 template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
296 ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
297 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::N_BUFFERS;
298
299 template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
300 ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
301 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::BUFFER_SIZE;
302
303 template <typename TBuffer, size_t BUFFER_SIZE_, size_t N_BUFFERS_, typename TFlag>
304 ETL_CONSTANT typename buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::size_type
305 buffer_descriptors<TBuffer, BUFFER_SIZE_, N_BUFFERS_, TFlag>::descriptor::MAX_SIZE;
306} // namespace etl
307#endif
308#endif
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228