Make FS::Path::Iterator meet forward iterator requirements
authorMikko Rasa <tdb@tdb.fi>
Sun, 5 Oct 2014 18:20:48 +0000 (21:20 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 5 Oct 2014 18:20:48 +0000 (21:20 +0300)
source/fs/path.cpp
source/fs/path.h

index 4db7f0cc140185d46234aa54eebfb1042af4e160..0fe95d65df943a9f0b4b1b4bf006035a4e7dccee 100644 (file)
@@ -222,7 +222,9 @@ Path::Iterator::Iterator(const Path &p, bool e):
        path(&p),
        iter(e ? path->separators.end() : path->separators.begin()),
        end(e || path->path.empty())
-{ }
+{
+       update();
+}
 
 Path::Iterator &Path::Iterator::operator++()
 {
@@ -234,6 +236,7 @@ Path::Iterator &Path::Iterator::operator++()
                if(path->path.size()==1 && path->separators.size()==1)
                        end = true;
        }
+       update();
        return *this;
 }
 
@@ -247,13 +250,17 @@ Path::Iterator &Path::Iterator::operator--()
        }
        else if(iter!=path->separators.begin())
                --iter;
+       update();
        return *this;
 }
 
-string Path::Iterator::operator*() const
+void Path::Iterator::update()
 {
        if(end)
-               throw logic_error("Path::Iterator::operator*");
+       {
+               current.clear();
+               return;
+       }
 
        string::size_type start = 0;
        if(iter!=path->separators.begin())
@@ -267,8 +274,9 @@ string Path::Iterator::operator*() const
                slash = *iter;
 
        if(slash==0)
-               return path->path.substr(start, 1);
-       return path->path.substr(start, slash-start);
+               current = path->path.substr(start, 1);
+       else
+               current = path->path.substr(start, slash-start);
 }
 
 } // namespace FS
index 5d0a5d353d4d465954b11ed9f7ff57a36aca5a5a..3cbfb16cf6e4052c57d223cd1fafd96b377b1c98 100644 (file)
@@ -36,10 +36,18 @@ private:
 public:
        class Iterator
        {
+       public:
+               typedef PositionArray::difference_type difference_type;
+               typedef const std::string value_type;
+               typedef const std::string *pointer;
+               typedef const std::string &reference;
+               typedef std::input_iterator_tag iterator_category;
+
        private:
                const Path *path;
                PositionArray::const_iterator iter;
                bool end;
+               std::string current;
 
                Iterator(const Path &, bool = false);
        public:
@@ -47,10 +55,14 @@ public:
                static Iterator at_end(const Path &p) { return Iterator(p, true); }
 
                Iterator &operator++();
+               Iterator operator++(int) { Iterator i = *this; ++*this; return i; }
                Iterator &operator--();
-               std::string operator*() const;
+               const std::string &operator*() const { return current; }
+               const std::string *operator->() const { return &current; }
                bool operator==(const Iterator &i) const { return (iter==i.iter && end==i.end); }
                bool operator!=(const Iterator &i) const { return !(*this==i); }
+       private:
+               void update();
        };
 
 private: