1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Use Kakoune::SafePtr to hold the PathName in DirIterator.

This commit is contained in:
King_DuckZ 2016-02-18 21:00:17 +01:00
parent 5a2fd1e25e
commit 8a97afd6bf
3 changed files with 55 additions and 28 deletions

View file

@ -19,6 +19,7 @@
#define id040FEEC20F7B4F65A3EF67BA6460E737 #define id040FEEC20F7B4F65A3EF67BA6460E737
#include "dindexer-machinery/recorddata.hpp" #include "dindexer-machinery/recorddata.hpp"
#include "kakoune/safe_ptr.hh"
#include <vector> #include <vector>
#include <boost/iterator/iterator_facade.hpp> #include <boost/iterator/iterator_facade.hpp>
#include <memory> #include <memory>
@ -58,7 +59,7 @@ namespace mchlib {
DirIterator ( DirIterator<Const>&& parOther ); DirIterator ( DirIterator<Const>&& parOther );
template <bool OtherConst> template <bool OtherConst>
DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() ); DirIterator ( DirIterator<OtherConst>&& parOther, typename std::enable_if<std::is_convertible<typename DirIterator<OtherConst>::VecIterator, VecIterator>::value, enabler>::type = enabler() );
DirIterator ( VecIterator parBegin, VecIterator parEnd, std::unique_ptr<PathName>&& parBasePath, std::size_t parLevelOffset ); DirIterator ( VecIterator parBegin, VecIterator parEnd, const PathName* parBasePath, std::size_t parLevelOffset );
~DirIterator ( void ) noexcept; ~DirIterator ( void ) noexcept;
private: private:
@ -72,7 +73,7 @@ namespace mchlib {
VecIterator m_current; VecIterator m_current;
VecIterator m_end; VecIterator m_end;
std::unique_ptr<PathName> m_base_path; Kakoune::SafePtr<const PathName> m_base_path;
std::size_t m_level_offset; std::size_t m_level_offset;
}; };
}; };
@ -87,6 +88,9 @@ namespace mchlib {
explicit SetListingView ( const implem::DirIterator<Const>& parIter ); explicit SetListingView ( const implem::DirIterator<Const>& parIter );
SetListingView ( list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset ); SetListingView ( list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset );
SetListingView ( list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset, const std::shared_ptr<PathName>& parBasePath );
template <bool B=not Const, typename Other=typename std::enable_if<B, SetListingView<not B>>::type>
SetListingView ( const Other& parOther );
SetListingView ( SetListingView&& ) = default; SetListingView ( SetListingView&& ) = default;
~SetListingView ( void ) noexcept = default; ~SetListingView ( void ) noexcept = default;
@ -102,6 +106,7 @@ namespace mchlib {
private: private:
list_iterator m_begin; list_iterator m_begin;
list_iterator m_end; list_iterator m_end;
std::shared_ptr<PathName> m_base_path;
std::size_t m_level_offset; std::size_t m_level_offset;
}; };
@ -128,6 +133,7 @@ namespace mchlib {
private: private:
ListType m_list; ListType m_list;
std::shared_ptr<PathName> m_base_path;
}; };
template <bool Const> template <bool Const>

View file

@ -19,6 +19,7 @@
#define id279E04E31E2C4D98B8C902781A3CE018 #define id279E04E31E2C4D98B8C902781A3CE018
#include "stringpool.hpp" #include "stringpool.hpp"
#include "kakoune/safe_ptr.hh"
#include <vector> #include <vector>
#include <string> #include <string>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
@ -26,7 +27,7 @@
#include <iostream> #include <iostream>
namespace mchlib { namespace mchlib {
class PathName { class PathName : public Kakoune::SafeCountable {
public: public:
PathName ( PathName&& ) = default; PathName ( PathName&& ) = default;
PathName ( const PathName& ) = default; PathName ( const PathName& ) = default;

View file

@ -22,7 +22,6 @@
#include <cassert> #include <cassert>
#include <algorithm> #include <algorithm>
namespace mchlib { namespace mchlib {
namespace { namespace {
bool file_record_data_lt (const FileRecordData& parLeft, const FileRecordData& parRight) { bool file_record_data_lt (const FileRecordData& parLeft, const FileRecordData& parRight) {
@ -65,12 +64,13 @@ namespace mchlib {
} }
template <bool Const> template <bool Const>
DirIterator<Const>::DirIterator (VecIterator parBegin, VecIterator parEnd, std::unique_ptr<PathName>&& parBasePath, std::size_t parLevelOffset) : DirIterator<Const>::DirIterator (VecIterator parBegin, VecIterator parEnd, const PathName* parBasePath, std::size_t parLevelOffset) :
m_current(parBegin), m_current(parBegin),
m_end(parEnd), m_end(parEnd),
m_base_path(std::move(parBasePath)), m_base_path(parBasePath),
m_level_offset(parLevelOffset) m_level_offset(parLevelOffset)
{ {
assert(parBasePath);
assert(m_base_path or m_current == m_end); assert(m_base_path or m_current == m_end);
assert(m_current == m_end or m_base_path->atom_count() == PathName(m_current->abs_path).atom_count()); assert(m_current == m_end or m_base_path->atom_count() == PathName(m_current->abs_path).atom_count());
assert(m_current == m_end or m_base_path->atom_count() == m_current->level + m_level_offset); assert(m_current == m_end or m_base_path->atom_count() == m_current->level + m_level_offset);
@ -146,11 +146,15 @@ namespace mchlib {
} //namespace implem } //namespace implem
SetListing::SetListing (ListType&& parList, bool parSort) : SetListing::SetListing (ListType&& parList, bool parSort) :
m_list(std::move(parList)) m_list(std::move(parList)),
m_base_path()
{ {
if (parSort) { if (parSort) {
std::sort(m_list.begin(), m_list.end(), &file_record_data_lt); std::sort(m_list.begin(), m_list.end(), &file_record_data_lt);
} }
if (not m_list.empty()) {
m_base_path.reset(new PathName(m_list.front().abs_path));
}
} }
SetListing::~SetListing() noexcept { SetListing::~SetListing() noexcept {
@ -161,11 +165,8 @@ namespace mchlib {
} }
auto SetListing::cbegin() const -> const_iterator { auto SetListing::cbegin() const -> const_iterator {
std::unique_ptr<PathName> base_path; const auto atom_count = (m_base_path ? m_base_path->atom_count() : 0);
if (m_list.begin() != m_list.end()) { return const_iterator(m_list.begin(), m_list.end(), m_base_path.get(), atom_count);
base_path.reset(new PathName(m_list.front().abs_path));
}
return const_iterator(m_list.begin(), m_list.end(), std::move(base_path), base_path->atom_count());
} }
auto SetListing::end() const -> const_iterator { auto SetListing::end() const -> const_iterator {
@ -173,37 +174,64 @@ namespace mchlib {
} }
auto SetListing::cend() const -> const_iterator { auto SetListing::cend() const -> const_iterator {
return const_iterator(m_list.end(), m_list.end(), std::unique_ptr<PathName>(), 0); return const_iterator(m_list.end(), m_list.end(), m_base_path.get(), 0);
} }
SetListingView<false> SetListing::make_view() { SetListingView<false> SetListing::make_view() {
const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count()); const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count());
return SetListingView<false>(m_list.begin(), m_list.end(), offs); return SetListingView<false>(m_list.begin(), m_list.end(), offs, m_base_path);
} }
SetListingView<true> SetListing::make_view() const { SetListingView<true> SetListing::make_view() const {
const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count()); const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count());
return SetListingView<true>(m_list.begin(), m_list.end(), offs); return SetListingView<true>(m_list.begin(), m_list.end(), offs, m_base_path);
} }
SetListingView<true> SetListing::make_cview() const { SetListingView<true> SetListing::make_cview() const {
const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count()); const auto offs = (m_list.empty() ? 0 : PathName(m_list.front().abs_path).atom_count());
return SetListingView<true>(m_list.begin(), m_list.end(), offs); return SetListingView<true>(m_list.begin(), m_list.end(), offs, m_base_path);
} }
template <bool Const> template <bool Const>
SetListingView<Const>::SetListingView (const implem::DirIterator<Const>& parIter) : SetListingView<Const>::SetListingView (const implem::DirIterator<Const>& parIter) :
m_begin(parIter.m_current), m_begin(parIter.m_current),
m_end(parIter.m_end), m_end(parIter.m_end),
m_base_path(),
m_level_offset(parIter.m_level_offset) m_level_offset(parIter.m_level_offset)
{ {
if (m_begin != m_end) {
m_base_path.reset(new PathName(m_begin->abs_path));
}
} }
template <bool Const> template <bool Const>
SetListingView<Const>::SetListingView (list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset) : SetListingView<Const>::SetListingView (list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset) :
m_begin(std::move(parBeg)), m_begin(std::move(parBeg)),
m_end(std::move(parEnd)), m_end(std::move(parEnd)),
m_base_path(),
m_level_offset(parLevelOffset) m_level_offset(parLevelOffset)
{
if (m_begin != m_end) {
m_base_path.reset(new PathName(m_begin->abs_path));
}
}
template <bool Const>
SetListingView<Const>::SetListingView (list_iterator parBeg, list_iterator parEnd, std::size_t parLevelOffset, const std::shared_ptr<PathName>& parBasePath) :
m_begin(std::move(parBeg)),
m_end(std::move(parEnd)),
m_base_path(parBasePath),
m_level_offset(parLevelOffset)
{
}
template <bool Const>
template <bool B, typename Other>
SetListingView<Const>::SetListingView (const Other& parOther) :
m_begin(parOther.m_begin),
m_end(parOther.m_end),
m_base_path(parOther.m_base_path),
m_level_offset(parOther.m_level_offset)
{ {
} }
@ -214,11 +242,7 @@ namespace mchlib {
template <bool Const> template <bool Const>
auto SetListingView<Const>::cbegin() const -> const_iterator { auto SetListingView<Const>::cbegin() const -> const_iterator {
std::unique_ptr<PathName> base_path; return const_iterator(m_begin, m_end, m_base_path.get(), m_level_offset);
if (m_begin != m_end) {
base_path.reset(new PathName(m_begin->abs_path));
}
return const_iterator(m_begin, m_end, std::move(base_path), m_level_offset);
} }
template <bool Const> template <bool Const>
@ -228,23 +252,19 @@ namespace mchlib {
template <bool Const> template <bool Const>
auto SetListingView<Const>::cend() const -> const_iterator { auto SetListingView<Const>::cend() const -> const_iterator {
return const_iterator(m_end, m_end, std::unique_ptr<PathName>(), m_level_offset); return const_iterator(m_end, m_end, m_base_path.get(), m_level_offset);
} }
template <bool Const> template <bool Const>
template <bool B, typename R> template <bool B, typename R>
R SetListingView<Const>::begin() { R SetListingView<Const>::begin() {
std::unique_ptr<PathName> base_path; return iterator(m_begin, m_end, m_base_path.get(), m_level_offset);
if (m_begin != m_end) {
base_path.reset(new PathName(m_begin->abs_path));
}
return iterator(m_begin, m_end, std::move(base_path), m_level_offset);
} }
template <bool Const> template <bool Const>
template <bool B, typename R> template <bool B, typename R>
R SetListingView<Const>::end() { R SetListingView<Const>::end() {
return iterator(m_end, m_end, std::unique_ptr<PathName>(), m_level_offset); return iterator(m_end, m_end, m_base_path.get(), m_level_offset);
} }
template class SetListingView<true>; template class SetListingView<true>;