avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
linkedlistutils.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <iterator>
11
12namespace av {
13
17template<typename T>
19{
23 void operator()(T*) const
24 {
25 }
26};
27
31template<typename T>
33{
39 void operator()(T * x) const
40 {
41 delete x;
42 }
43};
44
45
50template<typename T>
52{
60 T * operator()(T * x) const
61 {
62 return x->next;
63 }
64};
65
66
70template<typename T>
72{
80 T * operator()(T * x) const
81 {
82 return x + 1;
83 }
84};
85
90template<typename T, typename W>
92{
100 T * operator()(const W& wrapper) const
101 {
102 return wrapper.raw();
103 }
104};
105
106
111template<typename T, typename W>
113{
120 void operator()(W& wrapper, T * ptr) const
121 {
122 wrapper.reset(ptr);
123 }
124};
125
139template<typename T,
140 typename W,
141 typename N = DefaultNextElement<T>,
142 typename C = DefaultWrapperCast<T, W>,
143 typename R = DefaultResetPtr<T, W>,
144 typename D = NullDeleter<T>,
145 typename I = std::forward_iterator_tag>
147{
148public:
150 typedef T element_type;
160 typedef D deleter_type;
163
164 template<bool constIterator = false>
174 {
175 public:
176
178 std::iterator_traits<base_iterator> d;
179 // Iterator interface
183 using value_type = typename std::conditional<constIterator, const element_wrapper_type, element_wrapper_type>::type;
185 using difference_type = ptrdiff_t;
190
191 private:
192 using ElementType = typename std::conditional<constIterator, const element_type, element_type>::type;
193 using WrapperType = typename std::conditional<constIterator, const element_wrapper_type, element_wrapper_type>::type;
194
195 ElementType *ptr;
196 WrapperType wrapper;
197 next_element_type getNextPtr;
198 wrapper_reset_type resetPtr;
199
200 public:
207 ptr(ptr)
208 {
209 resetPtr(wrapper, ptr);
210 }
211
212 ~base_iterator() {}
213
214 // prefix++
221 {
222 ptr = getNextPtr(ptr);
223 resetPtr(wrapper, ptr);
224 return *this;
225 }
226
227 // postfix++
234 {
235 base_iterator tmp = *this;
236 operator++();
237 return tmp;
238 }
239
247 bool operator==(const base_iterator& rhs) const {return ptr == rhs.ptr;}
255 bool operator!=(const base_iterator& rhs) const {return !(ptr == rhs.ptr);}
256
262 WrapperType& operator*() noexcept
263 {
264 return wrapper;
265 }
266
272 WrapperType* operator->() noexcept
273 {
274 return &wrapper;
275 }
276
277 };
278
283
288 : m_begin(0),
289 m_end(0)
290 {
291 }
292
300 : m_begin(begin),
301 m_end(begin)
302 {
303 }
304
305
307 {
308 // Free resources
310 }
311
312
318 bool isValid()
319 {
320 return !!m_begin;
321 }
322
329 {
330 return m_begin;
331 }
332
338 const element_type *raw() const
339 {
340 return m_begin;
341 }
342
349 {
350 return iterator(m_begin);
351 }
352
353
360 {
361 return iterator(0);
362 }
363
364
371 {
372 return const_iterator(m_begin);
373 }
374
375
382 {
383 return const_iterator(0);
384 }
385
393 size_t count() const
394 {
395 return std::distance(begin(), end());
396 }
397
403 element_wrapper_type at(size_t idx) const
404 {
405 size_t size = count();
406 if (idx >= size)
407 return element_wrapper_type();
408
409 const_iterator it = begin();
410 std::advance(it, idx);
411
412 return *it;
413 }
414
415protected:
422
423};
424}
425
Iterator over the list.
Definition linkedlistutils.h:174
typename std::conditional< constIterator, const element_wrapper_type, element_wrapper_type >::type value_type
Type produced by dereferencing.
Definition linkedlistutils.h:183
WrapperType * operator->() noexcept
Element at the current position.
Definition linkedlistutils.h:272
base_iterator operator++(int)
Advance, returning the previous position.
Definition linkedlistutils.h:233
WrapperType & operator*() noexcept
Element at the current position.
Definition linkedlistutils.h:262
base_iterator & operator++()
Advance to the next element.
Definition linkedlistutils.h:220
ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition linkedlistutils.h:185
bool operator!=(const base_iterator &rhs) const
Compare positions.
Definition linkedlistutils.h:255
base_iterator(element_type *ptr)
Point the iterator at an element.
Definition linkedlistutils.h:206
bool operator==(const base_iterator &rhs) const
Compare positions.
Definition linkedlistutils.h:247
value_type * pointer
Pointer to the wrapper.
Definition linkedlistutils.h:187
std::iterator_traits< base_iterator > d
Definition linkedlistutils.h:178
iterator_category iterator_category
Category of the iteration.
Definition linkedlistutils.h:181
value_type & reference
Reference to the wrapper.
Definition linkedlistutils.h:189
Universal wrapper for one directional linked list elements.
Definition linkedlistutils.h:147
element_type * raw()
The first element of the list.
Definition linkedlistutils.h:328
W element_wrapper_type
avcpp wrapper returned when the iterator is dereferenced.
Definition linkedlistutils.h:152
base_iterator< false > iterator
Mutable iterator over the list.
Definition linkedlistutils.h:280
iterator begin()
First element.
Definition linkedlistutils.h:348
element_type * m_begin
First element of the list; owned when the deleter frees it.
Definition linkedlistutils.h:419
LinkedListWrapper(element_type *begin)
Take ownership of a list.
Definition linkedlistutils.h:299
D deleter_type
Deleter called by the destructor.
Definition linkedlistutils.h:160
iterator end()
End of the list.
Definition linkedlistutils.h:359
wrapper_cast_type m_wrapperCast
Functor used to obtain the raw pointer of a wrapper.
Definition linkedlistutils.h:417
element_type * m_end
Last element the wrapper was constructed with.
Definition linkedlistutils.h:421
N next_element_type
Functor that produces the next element.
Definition linkedlistutils.h:154
const_iterator end() const
End of the list, read-only.
Definition linkedlistutils.h:381
size_t count() const
Number of elements in the list.
Definition linkedlistutils.h:393
C wrapper_cast_type
Functor used to obtain the raw pointer of a wrapper.
Definition linkedlistutils.h:156
R wrapper_reset_type
Functor used to point a wrapper at another element.
Definition linkedlistutils.h:158
bool isValid()
Whether the wrapper holds a list.
Definition linkedlistutils.h:318
I iterator_category
Iterator category forwarded to the iterator traits.
Definition linkedlistutils.h:162
element_wrapper_type at(size_t idx) const
Log(N) complexity.
Definition linkedlistutils.h:403
const_iterator begin() const
First element, read-only.
Definition linkedlistutils.h:370
LinkedListWrapper()
Create an empty wrapper that owns nothing.
Definition linkedlistutils.h:287
const element_type * raw() const
The first element of the list, read-only.
Definition linkedlistutils.h:338
T element_type
Type of the list element, as FFmpeg declares it.
Definition linkedlistutils.h:150
base_iterator< true > const_iterator
Read-only iterator over the list.
Definition linkedlistutils.h:282
Root namespace of the library.
Definition audioresampler.h:19
This functor uses simple notation to take next element from linked list.
Definition linkedlistutils.h:52
T * operator()(T *x) const
Follow the next field of an intrusive list.
Definition linkedlistutils.h:60
This functor used by default to set new pointer that will be wrapped by W wrapper.
Definition linkedlistutils.h:113
void operator()(W &wrapper, T *ptr) const
Point a wrapper at another element.
Definition linkedlistutils.h:120
This functor used by default to take access to raw pointer that wrapped by W wrapper.
Definition linkedlistutils.h:92
T * operator()(const W &wrapper) const
Extract the raw pointer from a wrapper.
Definition linkedlistutils.h:100
This functor uses increment operation to take next element in list/array.
Definition linkedlistutils.h:72
T * operator()(T *x) const
Step to the next element of a contiguous array.
Definition linkedlistutils.h:80
This deleter does nothing.
Definition linkedlistutils.h:19
void operator()(T *) const
Do nothing.
Definition linkedlistutils.h:23
This deleter simple call 'delete' operator.
Definition linkedlistutils.h:33
void operator()(T *x) const
Delete the element.
Definition linkedlistutils.h:39