foobar2000 SDK  2015-08-03
iterators.h
Go to the documentation of this file.
1 namespace pfc {
3  template<typename t_item> class _list_node : public refcounted_object_root {
4  public:
6 
8 
9  t_item m_content;
10 
11  virtual t_self * prev() throw() {return NULL;}
12  virtual t_self * next() throw() {return NULL;}
13 
14  t_self * walk(bool forward) throw() {return forward ? next() : prev();}
15  };
16 
17  template<typename t_item> class const_iterator {
18  public:
22 
23  bool is_empty() const throw() {return m_content.is_empty();}
24  bool is_valid() const throw() {return m_content.is_valid();}
25  void invalidate() throw() {m_content = NULL;}
26 
27  void walk(bool forward) throw() {m_content = m_content->walk(forward);}
28  void prev() throw() {m_content = m_content->prev();}
29  void next() throw() {m_content = m_content->next();}
30 
32  t_node* _node() const throw() {return m_content.get_ptr();}
33 
35  const_iterator(t_node* source) : m_content(source) {}
36  const_iterator(t_nodeptr const & source) : m_content(source) {}
37  const_iterator(t_self const & other) : m_content(other.m_content) {}
38  const_iterator(t_self && other) : m_content(std::move(other.m_content)) {}
39 
40  t_self const & operator=(t_self const & other) {m_content = other.m_content; return *this;}
41  t_self const & operator=(t_self && other) {m_content = std::move(other.m_content); return *this;}
42 
43  const t_item& operator*() const throw() {return m_content->m_content;}
44  const t_item* operator->() const throw() {return &m_content->m_content;}
45 
46  const t_self & operator++() throw() {this->next(); return *this;}
47  const t_self & operator--() throw() {this->prev(); return *this;}
48  t_self operator++(int) throw() {t_self old = *this; this->next(); return old;}
49  t_self operator--(int) throw() {t_self old = *this; this->prev(); return old;}
50 
51  bool operator==(const t_self & other) const throw() {return this->m_content == other.m_content;}
52  bool operator!=(const t_self & other) const throw() {return this->m_content != other.m_content;}
53  protected:
54  t_nodeptr m_content;
55  };
56  template<typename t_item> class iterator : public const_iterator<t_item> {
57  public:
62 
63  iterator() {}
64  iterator(t_node* source) : t_selfConst(source) {}
65  iterator(t_nodeptr const & source) : t_selfConst(source) {}
66  iterator(t_self const & other) : t_selfConst(other) {}
67  iterator(t_self && other) : t_selfConst(std::move(other)) {}
68 
69  t_self const & operator=(t_self const & other) {this->m_content = other.m_content; return *this;}
70  t_self const & operator=(t_self && other) {this->m_content = std::move(other.m_content); return *this;}
71 
72  t_item& operator*() const throw() {return this->m_content->m_content;}
73  t_item* operator->() const throw() {return &this->m_content->m_content;}
74 
75  const t_self & operator++() throw() {this->next(); return *this;}
76  const t_self & operator--() throw() {this->prev(); return *this;}
77  t_self operator++(int) throw() {t_self old = *this; this->next(); return old;}
78  t_self operator--(int) throw() {t_self old = *this; this->prev(); return old;}
79 
80  bool operator==(const t_self & other) const throw() {return this->m_content == other.m_content;}
81  bool operator!=(const t_self & other) const throw() {return this->m_content != other.m_content;}
82  };
83 
84  template<typename t_comparator = comparator_default>
86  public:
87  template<typename t_list1, typename t_list2>
88  static int compare(const t_list1 & p_list1, const t_list2 & p_list2) {
89  typename t_list1::const_iterator iter1 = p_list1.first();
90  typename t_list2::const_iterator iter2 = p_list2.first();
91  for(;;) {
92  if (iter1.is_empty() && iter2.is_empty()) return 0;
93  else if (iter1.is_empty()) return -1;
94  else if (iter2.is_empty()) return 1;
95  else {
96  int state = t_comparator::compare(*iter1,*iter2);
97  if (state != 0) return state;
98  }
99  ++iter1; ++iter2;
100  }
101  }
102  };
103 
104  template<typename t_list1, typename t_list2>
105  static bool listEquals(const t_list1 & p_list1, const t_list2 & p_list2) {
106  typename t_list1::const_iterator iter1 = p_list1.first();
107  typename t_list2::const_iterator iter2 = p_list2.first();
108  for(;;) {
109  if (iter1.is_empty() && iter2.is_empty()) return true;
110  else if (iter1.is_empty() || iter2.is_empty()) return false;
111  else if (*iter1 != *iter2) return false;
112  ++iter1; ++iter2;
113  }
114  }
115 }
Base class for list nodes. Implemented by list implementers.
Definition: iterators.h:3
virtual t_self * next()
Definition: iterators.h:12
static bool listEquals(const t_list1 &p_list1, const t_list2 &p_list2)
Definition: iterators.h:105
bool operator!=(const t_self &other) const
Definition: iterators.h:81
iterator< t_item > t_self
Definition: iterators.h:59
t_nodeptr m_content
Definition: iterators.h:54
static int compare(const t_list1 &p_list1, const t_list2 &p_list2)
Definition: iterators.h:88
const t_item * operator->() const
Definition: iterators.h:44
t_self operator++(int)
Definition: iterators.h:77
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
STL namespace.
const t_self & operator++()
Definition: iterators.h:46
_list_node< t_item > t_node
Definition: iterators.h:19
t_self operator--(int)
Definition: iterators.h:49
void walk(bool forward)
Definition: iterators.h:27
iterator(t_node *source)
Definition: iterators.h:64
bool operator==(const t_self &other) const
Definition: iterators.h:80
iterator(t_self const &other)
Definition: iterators.h:66
t_self * walk(bool forward)
Definition: iterators.h:14
TEMPLATE_CONSTRUCTOR_FORWARD_FLOOD(_list_node, m_content) t_item m_content
bool is_valid() const
Definition: iterators.h:24
t_self const & operator=(t_self &&other)
Definition: iterators.h:41
t_self const & operator=(t_self const &other)
Definition: iterators.h:40
t_self const & operator=(t_self const &other)
Definition: iterators.h:69
iterator(t_self &&other)
Definition: iterators.h:67
t_node * _node() const
For internal use / list implementations only! Do not call!
Definition: iterators.h:32
_list_node< t_item > t_node
Definition: iterators.h:60
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: iterators.h:20
refcounted_object_ptr_t< t_node > t_nodeptr
Definition: iterators.h:61
t_self const & operator=(t_self &&other)
Definition: iterators.h:70
t_self operator++(int)
Definition: iterators.h:48
bool is_empty() const
Definition: iterators.h:23
const_iterator< t_item > t_self
Definition: iterators.h:21
const t_self & operator++()
Definition: iterators.h:75
bool operator!=(const t_self &other) const
Definition: iterators.h:52
virtual t_self * prev()
Definition: iterators.h:11
const t_item & operator*() const
Definition: iterators.h:43
const_iterator(t_nodeptr const &source)
Definition: iterators.h:36
const t_self & operator--()
Definition: iterators.h:47
const_iterator(t_self &&other)
Definition: iterators.h:38
t_item & operator*() const
Definition: iterators.h:72
_list_node< t_item > t_self
Definition: iterators.h:5
bool operator==(const t_self &other) const
Definition: iterators.h:51
const_iterator(t_self const &other)
Definition: iterators.h:37
iterator(t_nodeptr const &source)
Definition: iterators.h:65
t_item * operator->() const
Definition: iterators.h:73
t_self operator--(int)
Definition: iterators.h:78
const_iterator(t_node *source)
Definition: iterators.h:35
const_iterator< t_item > t_selfConst
Definition: iterators.h:58
const t_self & operator--()
Definition: iterators.h:76