FileObjectIterator.hpp 2.11 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#ifndef __HEPNOS_FILE_OBJECT_ITERATOR_H
#define __HEPNOS_FILE_OBJECT_ITERATOR_H

#include <boost/filesystem.hpp>

namespace hepnos {

namespace fs = boost::filesystem;

template<typename T>
class FileObjectIterator {

    private:

        std::string   _path;
        std::uint64_t _numObjects;
        std::uint64_t _currentIdx;
        T             _current;

    public:

        FileObjectIterator(const std::string& path, std::uint64_t idx, std::uint64_t nobj)
        : _path(path), _numObjects(nobj), _currentIdx(idx) {
            if(_path.back() != '/') _path += std::string("/");
            if(_currentIdx != _numObjects) {
                std::stringstream ss;
                ss << _path << std::setfill('0') << std::setw(12) << _currentIdx << "/";
                _current = T(_currentIdx, ss.str());
            }
        }

        bool operator==(const FileObjectIterator& other) const {
            return _path == other._path
                && _currentIdx == other._currentIdx
                && _numObjects == other._numObjects;
        }

        bool operator!=(const FileObjectIterator& other) const {
            return !(*this == other);
        }

        const T& operator*() const {
            return _current;
        }

        const T* operator->() const {
            return &_current;
        }

        T* operator->() {
            return &_current;
        }

        FileObjectIterator operator++() {
            FileObjectIterator old = *this;
            _currentIdx += 1;
            if(_currentIdx != _numObjects) {
                std::stringstream ss;
                ss << _path << std::setfill('0') << std::setw(12) << _currentIdx << "/";
                _current = T(_currentIdx, ss.str());
            }
            return old;
        }

        FileObjectIterator& operator++(int) {
            _currentIdx += 1;
            if(_currentIdx != _numObjects) {
                std::stringstream ss;
                ss << _path << std::setfill('0') << std::setw(12) <<  _currentIdx << "/";
                _current = T(_currentIdx, ss.str());
            }
            return *this;
        }
};

}

#endif