avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
linkedlistutils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iterator>
4 
5 namespace av {
6 
10 template<typename T>
12 {
13  void operator()(T*) const
14  {
15  }
16 };
17 
21 template<typename T>
22 struct PtrDeleter
23 {
24  void operator()(T * x) const
25  {
26  delete x;
27  }
28 };
29 
30 
35 template<typename T>
37 {
38  T * operator()(T * x) const
39  {
40  return x->next;
41  }
42 };
43 
44 
48 template<typename T>
50 {
51  T * operator()(T * x) const
52  {
53  return x + 1;
54  }
55 };
56 
61 template<typename T, typename W>
63 {
64  T * operator()(const W& wrapper) const
65  {
66  return wrapper.raw();
67  }
68 };
69 
70 
75 template<typename T, typename W>
77 {
78  void operator()(W& wrapper, T * ptr) const
79  {
80  wrapper.reset(ptr);
81  }
82 };
83 
97 template<typename T,
98  typename W,
99  typename N = DefaultNextElement<T>,
100  typename C = DefaultWrapperCast<T, W>,
101  typename R = DefaultResetPtr<T, W>,
102  typename D = NullDeleter<T>,
103  typename I = std::forward_iterator_tag>
105 {
106 public:
107  typedef T element_type;
109  typedef N next_element_type;
110  typedef C wrapper_cast_type;
112  typedef D deleter_type;
113  typedef I iterator_category;
114 
115  template<bool constIterator = false>
117  {
118  public:
119 
120  std::iterator_traits<base_iterator> d;
121  // Iterator interface
123  using value_type = typename std::conditional<constIterator, const element_wrapper_type, element_wrapper_type>::type;
124  using difference_type = ptrdiff_t;
125  using pointer = value_type*;
127 
128  private:
129  using ElementType = typename std::conditional<constIterator, const element_type, element_type>::type;
130  using WrapperType = typename std::conditional<constIterator, const element_wrapper_type, element_wrapper_type>::type;
131 
132  ElementType *ptr;
133  WrapperType wrapper;
134  next_element_type getNextPtr;
135  wrapper_reset_type resetPtr;
136 
137  public:
139  ptr(ptr)
140  {
141  resetPtr(wrapper, ptr);
142  }
143 
145 
146  // prefix++
148  {
149  ptr = getNextPtr(ptr);
150  resetPtr(wrapper, ptr);
151  return *this;
152  }
153 
154  // postfix++
156  {
157  base_iterator tmp = *this;
158  operator++();
159  return tmp;
160  }
161 
162  bool operator==(const base_iterator& rhs) const {return ptr == rhs.ptr;}
163  bool operator!=(const base_iterator& rhs) const {return !(ptr == rhs.ptr);}
164 
165  WrapperType& operator*() noexcept
166  {
167  return wrapper;
168  }
169 
170  WrapperType* operator->() noexcept
171  {
172  return &wrapper;
173  }
174 
175  };
176 
179 
181  : m_begin(0),
182  m_end(0)
183  {
184  }
185 
187  : m_begin(begin),
188  m_end(begin)
189  {
190  }
191 
192 
194  {
195  // Free resources
197  }
198 
199 
200  bool isValid()
201  {
202  return !!m_begin;
203  }
204 
206  {
207  return m_begin;
208  }
209 
210  const element_type *raw() const
211  {
212  return m_begin;
213  }
214 
216  {
217  return iterator(m_begin);
218  }
219 
220 
222  {
223  return iterator(0);
224  }
225 
226 
228  {
229  return const_iterator(m_begin);
230  }
231 
232 
234  {
235  return const_iterator(0);
236  }
237 
241  size_t count() const
242  {
243  return std::distance(begin(), end());
244  }
245 
251  element_wrapper_type at(size_t idx) const
252  {
253  size_t size = count();
254  if (idx >= size)
255  return element_wrapper_type();
256 
257  const_iterator it = begin();
258  std::advance(it, idx);
259 
260  return *it;
261  }
262 
263 protected:
267 
268 };
269 }
270 
Definition: linkedlistutils.h:117
typename std::conditional< constIterator, const element_wrapper_type, element_wrapper_type >::type value_type
Definition: linkedlistutils.h:123
~base_iterator()
Definition: linkedlistutils.h:144
base_iterator operator++(int)
Definition: linkedlistutils.h:155
ptrdiff_t difference_type
Definition: linkedlistutils.h:124
bool operator!=(const base_iterator &rhs) const
Definition: linkedlistutils.h:163
base_iterator(element_type *ptr)
Definition: linkedlistutils.h:138
WrapperType & operator*() noexcept
Definition: linkedlistutils.h:165
bool operator==(const base_iterator &rhs) const
Definition: linkedlistutils.h:162
value_type * pointer
Definition: linkedlistutils.h:125
std::iterator_traits< base_iterator > d
Definition: linkedlistutils.h:120
iterator_category iterator_category
Definition: linkedlistutils.h:122
base_iterator & operator++()
Definition: linkedlistutils.h:147
value_type & reference
Definition: linkedlistutils.h:126
WrapperType * operator->() noexcept
Definition: linkedlistutils.h:170
Universal wrapper for one directional linked list elements.
Definition: linkedlistutils.h:105
W element_wrapper_type
Definition: linkedlistutils.h:108
element_type * raw()
Definition: linkedlistutils.h:205
const element_type * raw() const
Definition: linkedlistutils.h:210
base_iterator< false > iterator
Definition: linkedlistutils.h:177
iterator begin()
Definition: linkedlistutils.h:215
element_type * m_begin
Definition: linkedlistutils.h:265
LinkedListWrapper(element_type *begin)
Definition: linkedlistutils.h:186
D deleter_type
Definition: linkedlistutils.h:112
iterator end()
Definition: linkedlistutils.h:221
wrapper_cast_type m_wrapperCast
Definition: linkedlistutils.h:264
element_type * m_end
Definition: linkedlistutils.h:266
N next_element_type
Definition: linkedlistutils.h:109
const_iterator end() const
Definition: linkedlistutils.h:233
size_t count() const
Log(N) complexity!
Definition: linkedlistutils.h:241
C wrapper_cast_type
Definition: linkedlistutils.h:110
R wrapper_reset_type
Definition: linkedlistutils.h:111
bool isValid()
Definition: linkedlistutils.h:200
~LinkedListWrapper()
Definition: linkedlistutils.h:193
I iterator_category
Definition: linkedlistutils.h:113
element_wrapper_type at(size_t idx) const
Log(N) complexity.
Definition: linkedlistutils.h:251
const_iterator begin() const
Definition: linkedlistutils.h:227
LinkedListWrapper()
Definition: linkedlistutils.h:180
T element_type
Definition: linkedlistutils.h:107
base_iterator< true > const_iterator
Definition: linkedlistutils.h:178
Definition: audioresampler.cpp:8
This functor uses simple notation to take next element from linked list.
Definition: linkedlistutils.h:37
T * operator()(T *x) const
Definition: linkedlistutils.h:38
This functor used by default to set new pointer that will be wrapped by W wrapper.
Definition: linkedlistutils.h:77
void operator()(W &wrapper, T *ptr) const
Definition: linkedlistutils.h:78
This functor used by default to take access to raw pointer that wrapped by W wrapper.
Definition: linkedlistutils.h:63
T * operator()(const W &wrapper) const
Definition: linkedlistutils.h:64
This functor uses increment operation to take next element in list/array.
Definition: linkedlistutils.h:50
T * operator()(T *x) const
Definition: linkedlistutils.h:51
This deleter does nothing.
Definition: linkedlistutils.h:12
void operator()(T *) const
Definition: linkedlistutils.h:13
This deleter simple call 'delete' operator.
Definition: linkedlistutils.h:23
void operator()(T *x) const
Definition: linkedlistutils.h:24