2016-02-09 20:35:13 +00:00
|
|
|
/* Copyright 2015, 2016, Michele Santullo
|
|
|
|
* This file is part of "dindexer".
|
|
|
|
*
|
|
|
|
* "dindexer" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "dindexer" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-12 20:02:26 +00:00
|
|
|
#ifndef id040FEEC20F7B4F65A3EF67BA6460E737
|
|
|
|
#define id040FEEC20F7B4F65A3EF67BA6460E737
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
#include "dindexer-machinery/recorddata.hpp"
|
|
|
|
#include <vector>
|
|
|
|
#include <boost/iterator/iterator_facade.hpp>
|
|
|
|
#include <memory>
|
2016-02-12 20:02:26 +00:00
|
|
|
#include <type_traits>
|
2016-02-16 17:51:17 +00:00
|
|
|
#include <ciso646>
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
namespace mchlib {
|
2016-02-16 17:51:17 +00:00
|
|
|
namespace implem {
|
|
|
|
template <bool Const> class DirIterator;
|
|
|
|
} //namespace implem
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
class PathName;
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool Const> class SetListingView;
|
|
|
|
template <bool Const> const PathName& get_pathname ( const implem::DirIterator<Const>& parIter );
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-16 19:29:50 +00:00
|
|
|
template <bool Const>
|
|
|
|
implem::DirIterator<Const> first_file ( const SetListingView<Const>& parList );
|
|
|
|
template <bool Const>
|
|
|
|
implem::DirIterator<Const> first_file ( SetListingView<Const>& parList );
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
namespace implem {
|
2016-02-12 20:02:26 +00:00
|
|
|
template <bool Const>
|
|
|
|
class DirIterator : public boost::iterator_facade<DirIterator<Const>, FileRecordData, boost::forward_traversal_tag> {
|
2016-02-16 17:51:17 +00:00
|
|
|
friend class mchlib::SetListingView<Const>;
|
2016-02-09 20:35:13 +00:00
|
|
|
friend class boost::iterator_core_access;
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool> friend class DirIterator;
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool B> friend const PathName& mchlib::get_pathname ( const DirIterator<B>& parIter );
|
2016-02-12 20:02:26 +00:00
|
|
|
typedef boost::iterator_facade<DirIterator<Const>, FileRecordData, boost::random_access_traversal_tag> base_class;
|
|
|
|
typedef typename base_class::difference_type difference_type;
|
|
|
|
typedef typename base_class::reference reference;
|
2016-02-16 17:12:26 +00:00
|
|
|
struct enabler {};
|
2016-02-09 20:35:13 +00:00
|
|
|
public:
|
2016-02-12 20:02:26 +00:00
|
|
|
typedef typename std::conditional<
|
|
|
|
Const,
|
|
|
|
std::vector<mchlib::FileRecordData>::const_iterator,
|
|
|
|
std::vector<mchlib::FileRecordData>::iterator
|
|
|
|
>::type VecIterator;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-12 20:02:26 +00:00
|
|
|
DirIterator ( DirIterator<Const>&& parOther );
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool OtherConst>
|
|
|
|
DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() );
|
2016-02-09 20:35:13 +00:00
|
|
|
DirIterator ( VecIterator parBegin, VecIterator parEnd, std::unique_ptr<PathName>&& parBasePath );
|
|
|
|
~DirIterator ( void ) noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void increment ( void );
|
2016-02-12 20:02:26 +00:00
|
|
|
difference_type distance_to ( const DirIterator<Const>& parOther ) const;
|
2016-02-16 17:12:26 +00:00
|
|
|
template <bool OtherConst>
|
|
|
|
bool equal ( const DirIterator<OtherConst>& parOther ) const;
|
|
|
|
|
2016-02-09 20:35:13 +00:00
|
|
|
reference dereference ( void ) const;
|
|
|
|
bool is_end ( void ) const;
|
|
|
|
|
|
|
|
VecIterator m_current;
|
|
|
|
VecIterator m_end;
|
|
|
|
std::unique_ptr<PathName> m_base_path;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool Const>
|
2016-02-12 19:14:12 +00:00
|
|
|
class SetListingView {
|
2016-02-16 17:51:17 +00:00
|
|
|
struct enabler {};
|
2016-02-09 20:35:13 +00:00
|
|
|
public:
|
2016-02-12 20:02:26 +00:00
|
|
|
typedef implem::DirIterator<true> const_iterator;
|
2016-02-16 17:51:17 +00:00
|
|
|
typedef implem::DirIterator<false> iterator;
|
|
|
|
typedef typename implem::DirIterator<Const>::VecIterator list_iterator;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
2016-02-16 17:51:17 +00:00
|
|
|
explicit SetListingView ( const implem::DirIterator<Const>& parIter );
|
2016-02-12 19:14:12 +00:00
|
|
|
SetListingView ( list_iterator parBeg, list_iterator parEnd );
|
|
|
|
SetListingView ( SetListingView&& ) = default;
|
|
|
|
~SetListingView ( void ) noexcept = default;
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
const_iterator begin ( void ) const;
|
|
|
|
const_iterator cbegin ( void ) const;
|
|
|
|
const_iterator end ( void ) const;
|
|
|
|
const_iterator cend ( void ) const;
|
2016-02-16 17:51:17 +00:00
|
|
|
template <bool B=not Const, typename R=typename std::enable_if<B, iterator>::type>
|
|
|
|
R begin ( void );
|
|
|
|
template <bool B=not Const, typename R=typename std::enable_if<B, iterator>::type>
|
|
|
|
R end ( void );
|
2016-02-09 20:35:13 +00:00
|
|
|
|
|
|
|
private:
|
2016-02-12 19:14:12 +00:00
|
|
|
list_iterator m_begin;
|
|
|
|
list_iterator m_end;
|
2016-02-09 20:35:13 +00:00
|
|
|
};
|
2016-02-09 22:23:50 +00:00
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
class SetListing {
|
2016-02-09 22:23:50 +00:00
|
|
|
public:
|
2016-02-12 19:14:12 +00:00
|
|
|
typedef std::vector<FileRecordData> ListType;
|
2016-02-16 17:51:17 +00:00
|
|
|
typedef implem::DirIterator<true> const_iterator;
|
2016-02-09 22:23:50 +00:00
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
explicit SetListing ( ListType&& parList, bool parSort=true );
|
|
|
|
~SetListing ( void ) noexcept;
|
2016-02-09 22:23:50 +00:00
|
|
|
|
|
|
|
const_iterator begin ( void ) const;
|
|
|
|
const_iterator cbegin ( void ) const;
|
|
|
|
const_iterator end ( void ) const;
|
|
|
|
const_iterator cend ( void ) const;
|
|
|
|
|
2016-02-12 19:14:12 +00:00
|
|
|
//ListType descend_copy ( const const_iterator& parItem ) const;
|
2016-02-16 17:51:17 +00:00
|
|
|
SetListingView<false> make_view ( void );
|
|
|
|
SetListingView<true> make_view ( void ) const;
|
|
|
|
SetListingView<true> make_cview ( void ) const;
|
2016-02-12 19:14:12 +00:00
|
|
|
|
2016-02-09 22:23:50 +00:00
|
|
|
private:
|
2016-02-12 19:14:12 +00:00
|
|
|
ListType m_list;
|
2016-02-09 22:23:50 +00:00
|
|
|
};
|
2016-02-16 17:51:17 +00:00
|
|
|
|
|
|
|
template <bool Const>
|
|
|
|
inline const PathName& get_pathname (const implem::DirIterator<Const>& parIter) {
|
|
|
|
return *parIter.m_base_path;
|
|
|
|
}
|
2016-02-16 19:29:50 +00:00
|
|
|
|
|
|
|
template <bool Const>
|
|
|
|
inline implem::DirIterator<Const> first_file (const SetListingView<Const>& parList) {
|
|
|
|
auto end = parList.end();
|
|
|
|
for (auto it = parList.begin(); it != end; ++it) {
|
|
|
|
if (not it->is_directory)
|
|
|
|
return std::move(it);
|
|
|
|
}
|
|
|
|
return parList.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool Const>
|
|
|
|
inline implem::DirIterator<Const> first_file (SetListingView<Const>& parList) {
|
|
|
|
auto end = parList.end();
|
|
|
|
for (auto it = parList.begin(); it != end; ++it) {
|
|
|
|
if (not it->is_directory)
|
|
|
|
return std::move(it);
|
|
|
|
}
|
|
|
|
return parList.end();
|
|
|
|
}
|
2016-02-09 20:35:13 +00:00
|
|
|
} //namespace mchlib
|
|
|
|
|
|
|
|
#endif
|