Audio Processing Framework (APF) version 0.5.0
rtlist.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_RTLIST_H
31#define APF_RTLIST_H
32
33#include <list>
34#include <algorithm> // for std::find()
35
36#include "apf/commandqueue.h"
37
38namespace apf
39{
40
41template<typename T> class RtList; // no implementation, use <T*>!
42
56template<typename T>
57class RtList<T*> : NonCopyable
58{
59 public:
60 using list_t = typename std::list<T*>;
61 using value_type = typename list_t::value_type;
62 using size_type = typename list_t::size_type;
63 using iterator = typename list_t::iterator;
64 using const_iterator = typename list_t::const_iterator;
65
66 class AddCommand; // no implementation, use <T*>!
67 class RemCommand; // no implementation, use <T*>!
68 class ClearCommand; // no implementation, use <T*>!
69
70 // Default constructor is not allowed!
71
74 explicit RtList(CommandQueue& fifo)
75 : _fifo(fifo)
76 {}
77
82 {
83 for (auto& delinquent: _the_actual_list) delete delinquent;
84 _the_actual_list.clear();
85 }
86
91 template<typename X>
92 X* add(X* item)
93 {
94 _fifo.push(new AddCommand(_the_actual_list, item));
95 return item;
96 }
97
102 template<typename ForwardIterator>
103 void add(ForwardIterator first, ForwardIterator last)
104 {
105 _fifo.push(new AddCommand(_the_actual_list, first, last));
106 }
107
109 void rem(T* to_rem)
110 {
111 _fifo.push(new RemCommand(_the_actual_list, to_rem));
112 }
113
117 template<typename ForwardIterator>
118 void rem(ForwardIterator first, ForwardIterator last)
119 {
120 _fifo.push(new RemCommand(_the_actual_list, first, last));
121 }
122
124 void clear()
125 {
126 _fifo.push(new ClearCommand(_the_actual_list));
127 }
128
130 void splice(iterator position, RtList& x)
131 {
132 assert(&_fifo == &x._fifo);
133 _the_actual_list.splice(position, x._the_actual_list);
134 }
135
137 void splice(iterator position, RtList& x, iterator first, iterator last)
138 {
139 assert(&_fifo == &x._fifo);
140 _the_actual_list.splice(position, x._the_actual_list, first, last);
141 }
142
144 iterator begin() { return _the_actual_list.begin(); }
145 const_iterator begin() const { return _the_actual_list.begin(); }
146 iterator end() { return _the_actual_list.end(); }
147 const_iterator end() const { return _the_actual_list.end(); }
148 bool empty() const { return _the_actual_list.empty(); }
149 size_type size() const { return _the_actual_list.size(); }
151
152 private:
153 CommandQueue& _fifo;
154 list_t _the_actual_list;
155};
156
158template<typename T>
159class RtList<T*>::AddCommand : public CommandQueue::Command
160{
161 public:
165 AddCommand(list_t& dst_list, T* element)
166 : _splice_list(1, element) // make list with one element
167 , _dst_list(dst_list)
168 {
169 assert(element != nullptr);
170 }
171
176 template<typename InputIterator>
177 AddCommand(list_t& dst_list, InputIterator first, InputIterator last)
178 : _splice_list(first, last)
179 , _dst_list(dst_list)
180 {}
181
182 virtual void execute()
183 {
184 _dst_list.splice(_dst_list.end(), _splice_list);
185 }
186
187 // Empty function, because no cleanup is necessary
188 virtual void cleanup() {}
189
190 private:
191 list_t _splice_list; // List of elements to be added
192 list_t& _dst_list; // Destination list
193};
194
196template<typename T>
197class RtList<T*>::RemCommand : public CommandQueue::Command
198{
199 public:
203 RemCommand(list_t& dst_list, T* delinquent)
204 : _dst_list(dst_list)
205 , _delinquents(1, delinquent)
206 {}
207
212 template<typename InputIterator>
213 RemCommand(list_t& dst_list, InputIterator first, InputIterator last)
214 : _dst_list(dst_list)
215 , _delinquents(first, last)
216 {}
217
220 virtual void execute()
221 {
222 for (auto& i: _delinquents)
223 {
224 auto delinquent = std::find(_dst_list.begin(), _dst_list.end(), i);
225 if (delinquent != _dst_list.end())
226 {
227 // Note: destruction order is reverse
228 _splice_list.splice(_splice_list.begin(), _dst_list, delinquent);
229 }
230 else
231 {
232 throw std::logic_error("RemCommand: Item not found!");
233 }
234 }
235 }
236
237 // this might be dangerous/unexpected.
238 // but i would need a synchronized Command, which waited
239 // for the operation to complete, otherwise.
240 virtual void cleanup()
241 {
242 for (auto& delinquent: _splice_list) delete delinquent;
243 _splice_list.clear();
244 }
245
246 private:
247 list_t _splice_list; // List of elements to be removed
248 list_t& _dst_list; // Destination list
249 list_t _delinquents; // Temporary list of elements to be removed
250};
251
253template<typename T>
254class RtList<T*>::ClearCommand : public CommandQueue::Command
255{
256 public:
259 ClearCommand(list_t& dst_list)
260 : _dst_list(dst_list)
261 {}
262
263 virtual void execute()
264 {
265 _delinquents.swap(_dst_list);
266 }
267
268 virtual void cleanup()
269 {
270 for (auto& delinquent: _delinquents) delete delinquent;
271 _delinquents.clear();
272 }
273
274 private:
275 list_t _delinquents;
276 list_t& _dst_list;
277};
278
279} // namespace apf
280
281#endif
Manage command queue from non-realtime thread to realtime thread.
Definition: commandqueue.h:49
Classes derived from this class cannot be copied (but still moved).
Definition: misc.h:60
virtual void execute()
The actual implementation of the command.
Definition: rtlist.h:182
virtual void cleanup()
Cleanup of resources.
Definition: rtlist.h:188
AddCommand(list_t &dst_list, InputIterator first, InputIterator last)
Constructor to add a bunch of items at once.
Definition: rtlist.h:177
AddCommand(list_t &dst_list, T *element)
Constructor to add a single item.
Definition: rtlist.h:165
ClearCommand(list_t &dst_list)
Constructor.
Definition: rtlist.h:259
virtual void execute()
The actual implementation of the command.
Definition: rtlist.h:263
virtual void cleanup()
Cleanup of resources.
Definition: rtlist.h:268
RemCommand(list_t &dst_list, T *delinquent)
Constructor to remove a single item.
Definition: rtlist.h:203
RemCommand(list_t &dst_list, InputIterator first, InputIterator last)
Constructor to remove a bunch of items at once.
Definition: rtlist.h:213
virtual void execute()
The actual implementation of the command.
Definition: rtlist.h:220
virtual void cleanup()
Cleanup of resources.
Definition: rtlist.h:240
void add(ForwardIterator first, ForwardIterator last)
Add a range of elements to the list.
Definition: rtlist.h:103
~RtList()
Destructor.
Definition: rtlist.h:81
X * add(X *item)
Add an element to the list.
Definition: rtlist.h:92
void splice(iterator position, RtList &x, iterator first, iterator last)
Splice a part of another RtList into the RtList.
Definition: rtlist.h:137
void rem(T *to_rem)
Remove an element from the list.
Definition: rtlist.h:109
void clear()
Remove all elements from the list.
Definition: rtlist.h:124
void rem(ForwardIterator first, ForwardIterator last)
Remove a range of elements from the list.
Definition: rtlist.h:118
RtList(CommandQueue &fifo)
Constructor.
Definition: rtlist.h:74
void splice(iterator position, RtList &x)
Splice another RtList into the RtList.
Definition: rtlist.h:130
Command queue.
Audio Processing Framework.
Definition: iterator.h:61
Abstract base class for realtime commands.
Definition: commandqueue.h:55