Gorgon Game Engine
Iterator.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <iterator>
6 
7 namespace Gorgon {
8 
9  namespace Filesystem {
10 
13  namespace internal {
14  class iterator_data;
15  }
18 
22  class Iterator : public std::iterator<std::forward_iterator_tag, std::string> {
23  public:
24 
30  Iterator(const std::string &directory, const std::string &pattern="*");
31 
34  Iterator(Iterator &&dir) : data(dir.data), basedir(std::move(dir.basedir)),
35  current(std::move(dir.current)) {
36  dir.data=nullptr;
37  }
38 
41  Iterator(const Iterator &other);
42 
45  Iterator() : data(nullptr) { }
46 
49  Iterator &operator =(Iterator other) { Swap(other); return *this; }
50 
53  ~Iterator() { Destroy(); }
54 
57  void Swap(Iterator &other) {
58  using std::swap;
59 
60  swap(data, other.data);
61  swap(basedir, other.basedir);
62  swap(current, other.current);
63  }
64 
67  std::string Get() const {
68 #ifdef DEBUG
69  if(!data || current=="") {
70  throw std::runtime_error("Invalid iterator.");
71  }
72 #endif
73 
74  return current;
75  }
76 
79  const std::string *operator ->() {
80 #ifdef DEBUG
81  if(!data || current=="") {
82  throw std::runtime_error("Invalid iterator.");
83  }
84 #endif
85 
86  return &current;
87  }
88 
91  operator std::string() const {
92  return Get();
93  }
94 
97  std::string operator *() const {
98  return Get();
99  }
100 
103  std::string Current() {
104  return Get();
105  }
106 
110  Next();
111 
112  return *this;
113  }
114 
118  for(int j=0;j<i;j++)
119  ++(*this);
120 
121  return *this;
122  }
123 
127  Iterator it=*this;
128 
129  Next();
130  return it;
131  }
132 
136  bool Next();
137 
142  void Destroy();
143 
146  bool IsValid() const {
147  return data && current!="";
148  }
149 
152  bool operator ==(const Iterator &other) const {
153  if(!data)
154  return other.data==nullptr;
155  else if(!other.data)
156  return false;
157 
158  return other.basedir==basedir && other.current==current;
159  }
160 
163  bool operator !=(const Iterator &other) const {
164  if(!data)
165  return other.data!=nullptr;
166  else if(!other.data)
167  return true;
168 
169  return other.basedir!=basedir || other.current!=current;
170  }
171 
172  private:
173  internal::iterator_data *data;
174  std::string basedir;
175  std::string current;
176  };
177 
178 
181  inline void swap(Iterator &l, Iterator &r) {
182  l.Swap(r);
183  }
184 
185  }
186 }
Gorgon::Filesystem::Iterator::IsValid
bool IsValid() const
Checks whether the iterator is valid.
Definition: Iterator.h:146
Gorgon::Filesystem::Iterator::operator->
const std::string * operator->()
Returns the current path.
Definition: Iterator.h:79
Gorgon::Filesystem::Iterator::operator!=
bool operator!=(const Iterator &other) const
Compares two iterators.
Definition: Iterator.h:163
Gorgon::Filesystem::Iterator::operator++
Iterator & operator++()
Move to the next path in the directory.
Definition: Iterator.h:109
Gorgon::Filesystem::Iterator::~Iterator
~Iterator()
Destructor.
Definition: Iterator.h:53
Gorgon::Filesystem::Iterator::operator*
std::string operator*() const
Returns the current path.
Definition: Iterator.h:97
Gorgon::Filesystem::Iterator::Iterator
Iterator(Iterator &&dir)
Move constructor.
Definition: Iterator.h:34
Gorgon::Filesystem::Iterator::Swap
void Swap(Iterator &other)
Swaps iterators, used for move semantics.
Definition: Iterator.h:57
Gorgon::Filesystem::swap
void swap(Iterator &l, Iterator &r)
Swaps two iterators.
Definition: Iterator.h:181
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Filesystem::Iterator::Destroy
void Destroy()
Destroys the current iterator.
Definition: Linux.cpp:431
Gorgon::Filesystem::Iterator::operator=
Iterator & operator=(Iterator other)
Assignment.
Definition: Iterator.h:49
Gorgon::Filesystem::Iterator
This iterator allows iteration of directories.
Definition: Iterator.h:22
Gorgon::Filesystem::Iterator::operator+=
Iterator & operator+=(int i)
Moves directory by i elements.
Definition: Iterator.h:117
Gorgon::Filesystem::Iterator::operator==
bool operator==(const Iterator &other) const
Compares two iterators.
Definition: Iterator.h:152
Gorgon::Filesystem::Iterator::Current
std::string Current()
Returns the current path.
Definition: Iterator.h:103
Gorgon::Filesystem::Iterator::Next
bool Next()
Next path in the directory.
Definition: Linux.cpp:438
Gorgon::Filesystem::Iterator::Get
std::string Get() const
Returns the current path.
Definition: Iterator.h:67
Gorgon::Filesystem::Iterator::Iterator
Iterator()
Empty constructor. Effectively generates end iterator.
Definition: Iterator.h:45