~ubuntu-branches/debian/experimental/libtorrent/experimental

« back to all changes in this revision

Viewing changes to src/torrent/data/file_list_iterator.h

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas
  • Date: 2007-03-31 10:31:05 UTC
  • mto: (4.1.4 gutsy) (6.2.1 squeeze) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20070331103105-jzpp1rml6ud0ff75
Tags: upstream-0.11.4
ImportĀ upstreamĀ versionĀ 0.11.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2006, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#ifndef LIBTORRENT_FILE_LIST_ITERATOR_H
 
38
#define LIBTORRENT_FILE_LIST_ITERATOR_H
 
39
 
 
40
#include <torrent/common.h>
 
41
#include <torrent/data/file_list.h>
 
42
 
 
43
namespace torrent {
 
44
 
 
45
class File;
 
46
 
 
47
// A special purpose iterator class for iterating through FileList as
 
48
// a dired structure.
 
49
class LIBTORRENT_EXPORT FileListIterator {
 
50
public:
 
51
  typedef FileList::iterator iterator;
 
52
  typedef File*              reference;
 
53
  typedef File**             pointer;
 
54
 
 
55
  FileListIterator() {}
 
56
  explicit FileListIterator(iterator pos, uint32_t depth = 0) : m_position(pos), m_depth(depth) {}
 
57
 
 
58
  bool                is_file() const;
 
59
  bool                is_empty() const;
 
60
 
 
61
  bool                is_entering() const;
 
62
  bool                is_leaving() const  { return m_depth < 0; }
 
63
 
 
64
  uint32_t            depth() const       { return std::abs(m_depth); }
 
65
 
 
66
  iterator            base() const        { return m_position; }
 
67
  reference           file() const        { return *m_position; }
 
68
 
 
69
  reference           operator *() const  { return *m_position; }
 
70
  pointer             operator ->() const { return &*m_position; }
 
71
  
 
72
  FileListIterator&   operator ++();
 
73
  FileListIterator&   operator --();
 
74
 
 
75
  FileListIterator    operator ++(int);
 
76
  FileListIterator    operator --(int);
 
77
 
 
78
  FileListIterator&   forward_current_depth();  
 
79
  FileListIterator&   backward_current_depth();  
 
80
 
 
81
  friend bool         operator ==(const FileListIterator& left, const FileListIterator& right);
 
82
  friend bool         operator !=(const FileListIterator& left, const FileListIterator& right);
 
83
 
 
84
private:
 
85
  iterator            m_position;
 
86
  int32_t             m_depth;
 
87
};
 
88
 
 
89
inline FileListIterator
 
90
FileListIterator::operator ++(int) {
 
91
  FileListIterator tmp = *this;
 
92
  ++(*this);
 
93
  return tmp;
 
94
}
 
95
 
 
96
inline FileListIterator
 
97
FileListIterator::operator --(int) {
 
98
  FileListIterator tmp = *this;
 
99
  --(*this);
 
100
  return tmp;
 
101
}
 
102
 
 
103
inline bool
 
104
operator ==(const FileListIterator& left, const FileListIterator& right) {
 
105
  return left.m_position == right.m_position && left.m_depth == right.m_depth;
 
106
}
 
107
 
 
108
inline bool
 
109
operator !=(const FileListIterator& left, const FileListIterator& right) {
 
110
  return left.m_position != right.m_position || left.m_depth != right.m_depth;
 
111
}
 
112
 
 
113
// Take a range as input and return the next entry at the same
 
114
// directory depth as first. If the returned iterator equals 'last' or
 
115
// is_leaving() == true then the search failed.
 
116
class LIBTORRENT_EXPORT file_list_collapsed_iterator : private FileListIterator {
 
117
public:
 
118
  typedef FileListIterator             base_type;
 
119
  typedef file_list_collapsed_iterator this_type;
 
120
 
 
121
  using base_type::iterator;
 
122
  using base_type::reference;
 
123
  using base_type::pointer;
 
124
 
 
125
  using base_type::is_file;
 
126
  using base_type::is_empty;
 
127
  using base_type::is_entering;
 
128
  using base_type::is_leaving;
 
129
  using base_type::depth;
 
130
  using base_type::file;
 
131
 
 
132
  file_list_collapsed_iterator() {}
 
133
  file_list_collapsed_iterator(const FileListIterator& src) : FileListIterator(src) {}
 
134
  explicit file_list_collapsed_iterator(iterator pos, uint32_t depth = 0) : FileListIterator(pos, depth) {}
 
135
 
 
136
  base_type           base() const { return *static_cast<const base_type*>(this); }
 
137
 
 
138
  this_type&          operator ++() { base_type::forward_current_depth(); return *this; }
 
139
  this_type&          operator --() { base_type::backward_current_depth(); return *this; }
 
140
 
 
141
  this_type           operator ++(int);
 
142
  this_type           operator --(int);
 
143
 
 
144
  friend bool         operator ==(const file_list_collapsed_iterator& left, const file_list_collapsed_iterator& right);
 
145
  friend bool         operator !=(const file_list_collapsed_iterator& left, const file_list_collapsed_iterator& right);
 
146
};
 
147
 
 
148
inline bool
 
149
operator ==(const file_list_collapsed_iterator& left, const file_list_collapsed_iterator& right) {
 
150
  return left.base() == right.base();
 
151
}
 
152
 
 
153
inline bool
 
154
operator !=(const file_list_collapsed_iterator& left, const file_list_collapsed_iterator& right) {
 
155
  return left.base() != right.base();
 
156
}
 
157
 
 
158
inline file_list_collapsed_iterator
 
159
file_list_collapsed_iterator::operator ++(int) {
 
160
  this_type tmp = *this;
 
161
  ++(*this);
 
162
  return tmp;
 
163
}
 
164
 
 
165
inline file_list_collapsed_iterator
 
166
file_list_collapsed_iterator::operator --(int) {
 
167
  this_type tmp = *this;
 
168
  --(*this);
 
169
  return tmp;
 
170
}
 
171
 
 
172
}
 
173
 
 
174
#endif