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 
av::LinkedListWrapper::base_iterator::iterator_category
iterator_category iterator_category
Definition: linkedlistutils.h:122
av::IncrementNextElement
This functor uses increment operation to take next element in list/array.
Definition: linkedlistutils.h:49
av::LinkedListWrapper
Universal wrapper for one directional linked list elements.
Definition: linkedlistutils.h:104
av::LinkedListWrapper::base_iterator::operator->
WrapperType * operator->() noexcept
Definition: linkedlistutils.h:170
av::LinkedListWrapper::LinkedListWrapper
LinkedListWrapper()
Definition: linkedlistutils.h:180
av::LinkedListWrapper::m_begin
element_type * m_begin
Definition: linkedlistutils.h:265
av::LinkedListWrapper::raw
element_type * raw()
Definition: linkedlistutils.h:205
av::PtrDeleter
This deleter simple call 'delete' operator.
Definition: linkedlistutils.h:22
av::LinkedListWrapper::begin
iterator begin()
Definition: linkedlistutils.h:215
av::DefaultWrapperCast::operator()
T * operator()(const W &wrapper) const
Definition: linkedlistutils.h:64
av::LinkedListWrapper::element_type
T element_type
Definition: linkedlistutils.h:107
av::DefaultNextElement::operator()
T * operator()(T *x) const
Definition: linkedlistutils.h:38
av::LinkedListWrapper::next_element_type
N next_element_type
Definition: linkedlistutils.h:109
av::NullDeleter
This deleter does nothing.
Definition: linkedlistutils.h:11
av::NullDeleter::operator()
void operator()(T *) const
Definition: linkedlistutils.h:13
av::LinkedListWrapper::end
const_iterator end() const
Definition: linkedlistutils.h:233
av::LinkedListWrapper::base_iterator::reference
value_type & reference
Definition: linkedlistutils.h:126
av::LinkedListWrapper::m_end
element_type * m_end
Definition: linkedlistutils.h:266
av::LinkedListWrapper::base_iterator::d
std::iterator_traits< base_iterator > d
Definition: linkedlistutils.h:120
av::DefaultNextElement
This functor uses simple notation to take next element from linked list.
Definition: linkedlistutils.h:36
av::IncrementNextElement::operator()
T * operator()(T *x) const
Definition: linkedlistutils.h:51
av::LinkedListWrapper::base_iterator::difference_type
ptrdiff_t difference_type
Definition: linkedlistutils.h:124
av::LinkedListWrapper::iterator_category
I iterator_category
Definition: linkedlistutils.h:113
av::LinkedListWrapper::base_iterator::value_type
typename std::conditional< constIterator, const element_wrapper_type, element_wrapper_type >::type value_type
Definition: linkedlistutils.h:123
av::LinkedListWrapper::begin
const_iterator begin() const
Definition: linkedlistutils.h:227
av::LinkedListWrapper::base_iterator::operator==
bool operator==(const base_iterator &rhs) const
Definition: linkedlistutils.h:162
av::DefaultWrapperCast
This functor used by default to take access to raw pointer that wrapped by W wrapper.
Definition: linkedlistutils.h:62
av::LinkedListWrapper::base_iterator::operator++
base_iterator & operator++()
Definition: linkedlistutils.h:147
av::LinkedListWrapper::count
size_t count() const
Log(N) complexity!
Definition: linkedlistutils.h:241
av::LinkedListWrapper::isValid
bool isValid()
Definition: linkedlistutils.h:200
av::PtrDeleter::operator()
void operator()(T *x) const
Definition: linkedlistutils.h:24
av::LinkedListWrapper::base_iterator::~base_iterator
~base_iterator()
Definition: linkedlistutils.h:144
av::LinkedListWrapper::base_iterator::operator*
WrapperType & operator*() noexcept
Definition: linkedlistutils.h:165
av::LinkedListWrapper::base_iterator::pointer
value_type * pointer
Definition: linkedlistutils.h:125
av::DefaultResetPtr::operator()
void operator()(W &wrapper, T *ptr) const
Definition: linkedlistutils.h:78
av::LinkedListWrapper::base_iterator::operator++
base_iterator operator++(int)
Definition: linkedlistutils.h:155
av::LinkedListWrapper::~LinkedListWrapper
~LinkedListWrapper()
Definition: linkedlistutils.h:193
av::LinkedListWrapper::iterator
base_iterator< false > iterator
Definition: linkedlistutils.h:177
av::LinkedListWrapper::base_iterator::base_iterator
base_iterator(element_type *ptr)
Definition: linkedlistutils.h:138
av::LinkedListWrapper::raw
const element_type * raw() const
Definition: linkedlistutils.h:210
av::LinkedListWrapper::base_iterator::operator!=
bool operator!=(const base_iterator &rhs) const
Definition: linkedlistutils.h:163
av::LinkedListWrapper::const_iterator
base_iterator< true > const_iterator
Definition: linkedlistutils.h:178
av
Definition: audioresampler.cpp:8
av::LinkedListWrapper::at
element_wrapper_type at(size_t idx) const
Log(N) complexity.
Definition: linkedlistutils.h:251
av::LinkedListWrapper::base_iterator
Definition: linkedlistutils.h:116
av::LinkedListWrapper::wrapper_reset_type
R wrapper_reset_type
Definition: linkedlistutils.h:111
av::DefaultResetPtr
This functor used by default to set new pointer that will be wrapped by W wrapper.
Definition: linkedlistutils.h:76
av::LinkedListWrapper::LinkedListWrapper
LinkedListWrapper(element_type *begin)
Definition: linkedlistutils.h:186
av::LinkedListWrapper::end
iterator end()
Definition: linkedlistutils.h:221
av::LinkedListWrapper::wrapper_cast_type
C wrapper_cast_type
Definition: linkedlistutils.h:110
av::LinkedListWrapper::element_wrapper_type
W element_wrapper_type
Definition: linkedlistutils.h:108
av::LinkedListWrapper::deleter_type
D deleter_type
Definition: linkedlistutils.h:112
av::LinkedListWrapper::m_wrapperCast
wrapper_cast_type m_wrapperCast
Definition: linkedlistutils.h:264