mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +08:00
Removed type erasure in containers
Summary: Refs T196 Using QVector as this is Qt's recommended container type. Reviewers: #swift_developers, rwinklmeier Reviewed By: #swift_developers, rwinklmeier Subscribers: rwinklmeier, jenkins Tags: #swift_pilot_client Maniphest Tasks: T196 Differential Revision: https://dev.swift-project.org/D61
This commit is contained in:
@@ -14,7 +14,6 @@
|
||||
|
||||
#include "optional.h"
|
||||
#include "typetraits.h"
|
||||
#include <QScopedPointer>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include <iterator>
|
||||
@@ -357,509 +356,6 @@ namespace BlackMisc
|
||||
return { std::move(iterators) };
|
||||
}
|
||||
|
||||
/*!
|
||||
* Generic type-erased const forward iterator with value semantics.
|
||||
* \tparam T the value_type of the container being iterated over.
|
||||
*
|
||||
* Can take any suitable iterator type as its implementation at runtime.
|
||||
*/
|
||||
template <class T> class ConstForwardIterator
|
||||
{
|
||||
public:
|
||||
//! STL compatibility
|
||||
//! @{
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef T value_type;
|
||||
typedef const T *pointer;
|
||||
typedef const T &reference;
|
||||
typedef const T *const_pointer;
|
||||
typedef const T &const_reference;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
//! @}
|
||||
|
||||
//! Default constructor.
|
||||
ConstForwardIterator() {}
|
||||
|
||||
//! Copy constructor.
|
||||
ConstForwardIterator(const ConstForwardIterator &other) : m_pimpl(other.pimpl() ? other.pimpl()->clone() : nullptr) {}
|
||||
|
||||
//! Move constructor.
|
||||
ConstForwardIterator(ConstForwardIterator &&other) noexcept : m_pimpl(other.m_pimpl.take()) {}
|
||||
|
||||
//! Copy assignment.
|
||||
ConstForwardIterator &operator =(const ConstForwardIterator &other) { m_pimpl.reset(other.pimpl() ? other.pimpl()->clone() : nullptr); return *this; }
|
||||
|
||||
//! Move assignment.
|
||||
ConstForwardIterator &operator =(ConstForwardIterator &&other) noexcept { m_pimpl.reset(other.m_pimpl.take()); return *this; }
|
||||
|
||||
//! Destructor.
|
||||
~ConstForwardIterator() = default;
|
||||
|
||||
//! Create a new iterator with a specific implementation type.
|
||||
//! \tparam I Becomes the iterator's implementation type.
|
||||
//! \param i Initial value for the iterator. The value is copied.
|
||||
template <class I> static ConstForwardIterator fromImpl(I i) { return ConstForwardIterator(new Pimpl<I>(std::move(i))); }
|
||||
|
||||
//! Returns a reference to the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_reference operator *() const { Q_ASSERT(m_pimpl); return **pimpl(); }
|
||||
|
||||
//! Arrow operator provides access to members of the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_pointer operator ->() const { Q_ASSERT(m_pimpl); return &**pimpl(); }
|
||||
|
||||
//! Prefix increment operator advances the iterator.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstForwardIterator &operator ++() { Q_ASSERT(m_pimpl); ++*pimpl(); return *this; }
|
||||
|
||||
//! Postfix increment operator advances the iterator.
|
||||
//! \return Copy of the iterator in the old position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstForwardIterator operator ++(int) { Q_ASSERT(m_pimpl); auto copy = *this; ++*pimpl(); return copy; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstForwardIterator operator +=(difference_type n) { Q_ASSERT(m_pimpl); *pimpl() += n; return *this; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Copy of the iterator in its new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstForwardIterator operator +(difference_type n) const { auto copy = *this; return copy += n; }
|
||||
|
||||
//! Test for equality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator ==(const ConstForwardIterator &other) const { return (pimpl() && other.pimpl()) ? *pimpl() == *other.pimpl() : pimpl() == other.pimpl(); }
|
||||
|
||||
//! Test for inequality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator !=(const ConstForwardIterator &other) const { return !(*this == other); }
|
||||
|
||||
//! Return opaque pointer to underlying implementation iterator object.
|
||||
//! \pre The iterator must have been initialized.
|
||||
template <typename U> U &getImpl() { pimpl()->assertType(typeid(std::decay_t<U>)); return *static_cast<U*>(pimpl()->impl()); }
|
||||
|
||||
private:
|
||||
class PimplBase
|
||||
{
|
||||
public:
|
||||
PimplBase() {}
|
||||
PimplBase(const PimplBase &) = default;
|
||||
PimplBase &operator =(const PimplBase &) = delete;
|
||||
virtual ~PimplBase() {}
|
||||
virtual PimplBase *clone() const = 0;
|
||||
virtual const_reference operator *() const = 0;
|
||||
virtual void operator ++() = 0;
|
||||
virtual void operator +=(difference_type) = 0;
|
||||
virtual bool operator ==(const PimplBase &) const = 0;
|
||||
virtual void *impl() = 0;
|
||||
virtual void assertType(std::type_index) const = 0;
|
||||
};
|
||||
|
||||
template <class I> class Pimpl : public PimplBase
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_same<T, typename std::iterator_traits<I>::value_type>::value,
|
||||
"ConstForwardIterator must be initialized from an iterator with the same value_type.");
|
||||
Pimpl(I &&i) : m_impl(std::move(i)) {}
|
||||
virtual PimplBase *clone() const override { return new Pimpl(*this); }
|
||||
virtual const_reference operator *() const override { return *m_impl; }
|
||||
virtual void operator ++() override { ++m_impl; }
|
||||
virtual void operator +=(difference_type n) override { std::advance(m_impl, n); }
|
||||
virtual bool operator ==(const PimplBase &other) const override { return m_impl == static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual void *impl() override { return &m_impl; }
|
||||
virtual void assertType(std::type_index ti) const override { Q_ASSERT(ti == typeid(I)); Q_UNUSED(ti); }
|
||||
private:
|
||||
I m_impl;
|
||||
};
|
||||
|
||||
using PimplPtr = QScopedPointer<PimplBase>;
|
||||
PimplPtr m_pimpl;
|
||||
|
||||
explicit ConstForwardIterator(PimplBase *pimpl) : m_pimpl(pimpl) {} // private ctor used by fromImpl()
|
||||
|
||||
// using these methods to access m_pimpl.data() eases the cognitive burden of correctly forwarding const
|
||||
PimplBase *pimpl() { return m_pimpl.data(); }
|
||||
const PimplBase *pimpl() const { return m_pimpl.data(); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Generic type-erased const random access iterator with value semantics.
|
||||
* \tparam T the value_type of the container being iterated over.
|
||||
*
|
||||
* Can take any suitable iterator type as its implementation at runtime.
|
||||
*/
|
||||
template <class T> class ConstRandomAccessIterator
|
||||
{
|
||||
public:
|
||||
//! STL compatibility
|
||||
//! @{
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef T value_type;
|
||||
typedef const T *pointer;
|
||||
typedef const T &reference;
|
||||
typedef const T *const_pointer;
|
||||
typedef const T &const_reference;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
//! @}
|
||||
|
||||
//! Default constructor.
|
||||
ConstRandomAccessIterator() {}
|
||||
|
||||
//! Copy constructor.
|
||||
ConstRandomAccessIterator(const ConstRandomAccessIterator &other) : m_pimpl(other.pimpl() ? other.pimpl()->clone() : nullptr) {}
|
||||
|
||||
//! Move constructor.
|
||||
ConstRandomAccessIterator(ConstRandomAccessIterator &&other) noexcept : m_pimpl(other.m_pimpl.take()) {}
|
||||
|
||||
//! Copy assignment.
|
||||
ConstRandomAccessIterator &operator =(const ConstRandomAccessIterator &other) { m_pimpl.reset(other.pimpl() ? other.pimpl()->clone() : nullptr); return *this; }
|
||||
|
||||
//! Move assignment.
|
||||
ConstRandomAccessIterator &operator =(ConstRandomAccessIterator &&other) noexcept { m_pimpl.reset(other.m_pimpl.take()); return *this; }
|
||||
|
||||
//! Destructor.
|
||||
~ConstRandomAccessIterator() = default;
|
||||
|
||||
//! Create a new iterator with a specific implementation type.
|
||||
//! \tparam I Becomes the iterator's implementation type.
|
||||
//! \param i Initial value for the iterator. The value is copied.
|
||||
template <class I> static ConstRandomAccessIterator fromImpl(I i) { return ConstRandomAccessIterator(new Pimpl<I>(std::move(i))); }
|
||||
|
||||
//! Returns a reference to the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_reference operator *() const { Q_ASSERT(m_pimpl); return **pimpl(); }
|
||||
|
||||
//! Arrow operator provides access to members of the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_pointer operator ->() const { Q_ASSERT(m_pimpl); return &**pimpl(); }
|
||||
|
||||
//! Prefix increment operator advances the iterator.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator &operator ++() { Q_ASSERT(m_pimpl); ++*pimpl(); return *this; }
|
||||
|
||||
//! Postfix increment operator advances the iterator.
|
||||
//! \return Copy of the iterator in the old position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator operator ++(int) { Q_ASSERT(m_pimpl); auto copy = *this; ++*pimpl(); return copy; }
|
||||
|
||||
//! Prefix decrement operator backtracks the iterator.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator &operator --() { Q_ASSERT(m_pimpl); --*pimpl(); return *this; }
|
||||
|
||||
//! Postfix decrement operator backtracks the iterator.
|
||||
//! \return Copy of the iterator at the old position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator operator --(int) { Q_ASSERT(m_pimpl); auto copy = *this; --*pimpl(); return copy; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator operator +=(difference_type n) { Q_ASSERT(m_pimpl); *pimpl() += n; return *this; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Copy of the iterator in its new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
//! @{
|
||||
friend ConstRandomAccessIterator operator +(const ConstRandomAccessIterator &i, difference_type n) { auto copy = i; return copy += n; }
|
||||
friend ConstRandomAccessIterator operator +(difference_type n, const ConstRandomAccessIterator &i) { auto copy = i; return copy += n; }
|
||||
//! @}
|
||||
|
||||
//! Backtrack the iterator by a certain amount.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator operator -=(difference_type n) { Q_ASSERT(m_pimpl); *pimpl() -= n; return *this; }
|
||||
|
||||
//! Backtrack the iterator by a certain amount.
|
||||
//! \return Copy of the iterator in its new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
ConstRandomAccessIterator operator -(difference_type n) const { auto copy = *this; return copy -= n; }
|
||||
|
||||
//! Return the distance between two iterators.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
difference_type operator -(const ConstRandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() - *other.pimpl(); }
|
||||
|
||||
//! Test for equality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator ==(const ConstRandomAccessIterator &other) const { return (pimpl() && other.pimpl()) ? *pimpl() == *other.pimpl() : pimpl() == other.pimpl(); }
|
||||
|
||||
//! Test for inequality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator !=(const ConstRandomAccessIterator &other) const { return !(*this == other); }
|
||||
|
||||
//! For sorting.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
//! @{
|
||||
bool operator <(const ConstRandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() < *other.pimpl(); }
|
||||
bool operator >(const ConstRandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() > *other.pimpl(); }
|
||||
bool operator <=(const ConstRandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() <= *other.pimpl(); }
|
||||
bool operator >=(const ConstRandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() >= *other.pimpl(); }
|
||||
//! @}
|
||||
|
||||
//! Subscript operator.
|
||||
//! \pre `(*this + n)` must be dereferenceable.
|
||||
reference operator [](difference_type n) const { return *(*this + n); }
|
||||
|
||||
//! Return opaque pointer to underlying implementation iterator object.
|
||||
//! \pre The iterator must have been initialized.
|
||||
template <typename U> U &getImpl() { pimpl()->assertType(typeid(std::decay_t<U>)); return *static_cast<U*>(pimpl()->impl()); }
|
||||
|
||||
private:
|
||||
class PimplBase
|
||||
{
|
||||
public:
|
||||
PimplBase() {}
|
||||
PimplBase(const PimplBase &) = default;
|
||||
PimplBase &operator =(const PimplBase &) = delete;
|
||||
virtual ~PimplBase() {}
|
||||
virtual PimplBase *clone() const = 0;
|
||||
virtual const_reference operator *() const = 0;
|
||||
virtual void operator ++() = 0;
|
||||
virtual void operator --() = 0;
|
||||
virtual void operator +=(difference_type) = 0;
|
||||
virtual void operator -=(difference_type) = 0;
|
||||
virtual difference_type operator -(const PimplBase &) const = 0;
|
||||
virtual bool operator ==(const PimplBase &) const = 0;
|
||||
virtual bool operator <(const PimplBase &) const = 0;
|
||||
virtual bool operator >(const PimplBase &) const = 0;
|
||||
virtual bool operator <=(const PimplBase &) const = 0;
|
||||
virtual bool operator >=(const PimplBase &) const = 0;
|
||||
virtual void *impl() = 0;
|
||||
virtual void assertType(std::type_index) const = 0;
|
||||
};
|
||||
|
||||
template <class I> class Pimpl : public PimplBase
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_same<T, typename std::iterator_traits<I>::value_type>::value,
|
||||
"ConstRandomAccessIterator must be initialized from an iterator with the same value_type.");
|
||||
static_assert(std::is_same<typename std::iterator_traits<I>::iterator_category, std::random_access_iterator_tag>::value,
|
||||
"ConstRandomAccessIterator must be initialized from a random access iterator.");
|
||||
Pimpl(I &&i) : m_impl(std::move(i)) {}
|
||||
virtual PimplBase *clone() const override { return new Pimpl(*this); }
|
||||
virtual const_reference operator *() const override { return *m_impl; }
|
||||
virtual void operator ++() override { ++m_impl; }
|
||||
virtual void operator --() override { --m_impl; }
|
||||
virtual void operator +=(difference_type n) override { m_impl += n; }
|
||||
virtual void operator -=(difference_type n) override { m_impl -= n; }
|
||||
virtual difference_type operator -(const PimplBase &other) const override { return m_impl - static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator ==(const PimplBase &other) const override { return m_impl == static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator <(const PimplBase &other) const override { return m_impl < static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator >(const PimplBase &other) const override { return m_impl > static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator <=(const PimplBase &other) const override { return m_impl <= static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator >=(const PimplBase &other) const override { return m_impl >= static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual void *impl() override { return &m_impl; }
|
||||
virtual void assertType(std::type_index ti) const override { Q_ASSERT(ti == typeid(I)); Q_UNUSED(ti); }
|
||||
private:
|
||||
I m_impl;
|
||||
};
|
||||
|
||||
using PimplPtr = QScopedPointer<PimplBase>;
|
||||
PimplPtr m_pimpl;
|
||||
|
||||
explicit ConstRandomAccessIterator(PimplBase *pimpl) : m_pimpl(pimpl) {} // private ctor used by fromImpl()
|
||||
|
||||
// using these methods to access m_pimpl.data() eases the cognitive burden of correctly forwarding const
|
||||
PimplBase *pimpl() { return m_pimpl.data(); }
|
||||
const PimplBase *pimpl() const { return m_pimpl.data(); }
|
||||
};
|
||||
|
||||
/*!
|
||||
* Generic type-erased non-const bidirectional iterator with value semantics.
|
||||
* \tparam T the value_type of the container being iterated over.
|
||||
*
|
||||
* Can take any suitable iterator type as its implementation at runtime.
|
||||
*/
|
||||
template <class T> class RandomAccessIterator
|
||||
{
|
||||
public:
|
||||
//! STL compatibility
|
||||
//! @{
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef T value_type;
|
||||
typedef T *pointer;
|
||||
typedef T &reference;
|
||||
typedef const T *const_pointer;
|
||||
typedef const T &const_reference;
|
||||
typedef std::random_access_iterator_tag iterator_category;
|
||||
//! @}
|
||||
|
||||
//! Default constructor.
|
||||
RandomAccessIterator() {}
|
||||
|
||||
//! Copy constructor.
|
||||
RandomAccessIterator(const RandomAccessIterator &other) : m_pimpl(other.pimpl() ? other.pimpl()->clone() : nullptr) {}
|
||||
|
||||
//! Move constructor.
|
||||
RandomAccessIterator(RandomAccessIterator &&other) noexcept : m_pimpl(other.m_pimpl.take()) {}
|
||||
|
||||
//! Copy assignment.
|
||||
RandomAccessIterator &operator =(const RandomAccessIterator &other) { m_pimpl.reset(other.pimpl() ? other.pimpl()->clone() : nullptr); return *this; }
|
||||
|
||||
//! Move assignment.
|
||||
RandomAccessIterator &operator =(RandomAccessIterator &&other) noexcept { m_pimpl.reset(other.m_pimpl.take()); return *this; }
|
||||
|
||||
//! Destructor.
|
||||
~RandomAccessIterator() = default;
|
||||
|
||||
//! Create a new iterator with a specific implementation type.
|
||||
//! \tparam I Becomes the iterator's implementation type.
|
||||
//! \param i Initial value for the iterator. The value is copied.
|
||||
template <class I> static RandomAccessIterator fromImpl(I i) { return RandomAccessIterator(new Pimpl<I>(std::move(i))); }
|
||||
|
||||
//! Returns a reference to the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_reference operator *() const { Q_ASSERT(m_pimpl); return **pimpl(); }
|
||||
|
||||
//! Returns a reference to the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
reference operator *() { Q_ASSERT(m_pimpl); return **pimpl(); }
|
||||
|
||||
//! Arrow operator provides access to members of the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
const_pointer operator ->() const { Q_ASSERT(m_pimpl); return &**pimpl(); }
|
||||
|
||||
//! Arrow operator provides access to members of the object pointed to.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
pointer operator ->() { Q_ASSERT(m_pimpl); return &**pimpl(); }
|
||||
|
||||
//! Prefix increment operator advances the iterator.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator &operator ++() { Q_ASSERT(m_pimpl); ++*pimpl(); return *this; }
|
||||
|
||||
//! Postfix increment operator advances the iterator.
|
||||
//! \return Copy of the iterator in the old position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator operator ++(int) { Q_ASSERT(m_pimpl); auto copy = *this; ++*pimpl(); return copy; }
|
||||
|
||||
//! Prefix decrement operator backtracks the iterator.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator &operator --() { Q_ASSERT(m_pimpl); --*pimpl(); return *this; }
|
||||
|
||||
//! Postfix decrement operator backtracks the iterator.
|
||||
//! \return Copy of the iterator at the old position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator operator --(int) { Q_ASSERT(m_pimpl); auto copy = *this; --*pimpl(); return copy; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator operator +=(difference_type n) { Q_ASSERT(m_pimpl); *pimpl() += n; return *this; }
|
||||
|
||||
//! Advance the iterator by a certain amount.
|
||||
//! \return Copy of the iterator in its new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
//! @{
|
||||
friend RandomAccessIterator operator +(const RandomAccessIterator &i, difference_type n) { auto copy = i; return copy += n; }
|
||||
friend RandomAccessIterator operator +(difference_type n, const RandomAccessIterator &i) { auto copy = i; return copy += n; }
|
||||
//! @}
|
||||
|
||||
//! Backtrack the iterator by a certain amount.
|
||||
//! \return Reference to the iterator at the new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator operator -=(difference_type n) { Q_ASSERT(m_pimpl); *pimpl() -= n; return *this; }
|
||||
|
||||
//! Backtrack the iterator by a certain amount.
|
||||
//! \return Copy of the iterator in its new position.
|
||||
//! \pre The iterator must be initialized and valid.
|
||||
RandomAccessIterator operator -(difference_type n) const { auto copy = *this; return copy -= n; }
|
||||
|
||||
//! Return the distance between two iterators.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
difference_type operator -(const RandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() - *other.pimpl(); }
|
||||
|
||||
//! Test for equality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator ==(const RandomAccessIterator &other) const { return (pimpl() && other.pimpl()) ? *pimpl() == *other.pimpl() : pimpl() == other.pimpl(); }
|
||||
|
||||
//! Test for inequality.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
bool operator !=(const RandomAccessIterator &other) const { return !(*this == other); }
|
||||
|
||||
//! For sorting.
|
||||
//! \pre Both iterators must originate from the same collection, and not mix begin/end with cbegin/cend.
|
||||
//! @{
|
||||
bool operator <(const RandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() < *other.pimpl(); }
|
||||
bool operator >(const RandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() > *other.pimpl(); }
|
||||
bool operator <=(const RandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() <= *other.pimpl(); }
|
||||
bool operator >=(const RandomAccessIterator &other) const { Q_ASSERT(m_pimpl && other.m_pimpl); return *pimpl() >= *other.pimpl(); }
|
||||
//! @}
|
||||
|
||||
//! Subscript operator.
|
||||
//! \pre `(*this + n)` must be dereferenceable.
|
||||
reference operator [](difference_type n) const { return *(*this + n); }
|
||||
|
||||
//! Return opaque pointer to underlying implementation iterator object.
|
||||
//! \pre The iterator must have been initialized.
|
||||
template <typename U> U &getImpl() { pimpl()->assertType(typeid(std::decay_t<U>)); return *static_cast<U*>(pimpl()->impl()); }
|
||||
|
||||
private:
|
||||
class PimplBase
|
||||
{
|
||||
public:
|
||||
PimplBase() {}
|
||||
PimplBase(const PimplBase &) = default;
|
||||
PimplBase &operator =(const PimplBase &) = delete;
|
||||
virtual ~PimplBase() {}
|
||||
virtual PimplBase *clone() const = 0;
|
||||
virtual const_reference operator *() const = 0;
|
||||
virtual reference operator *() = 0;
|
||||
virtual void operator ++() = 0;
|
||||
virtual void operator --() = 0;
|
||||
virtual void operator +=(difference_type) = 0;
|
||||
virtual void operator -=(difference_type) = 0;
|
||||
virtual difference_type operator -(const PimplBase &) const = 0;
|
||||
virtual bool operator ==(const PimplBase &) const = 0;
|
||||
virtual bool operator <(const PimplBase &) const = 0;
|
||||
virtual bool operator >(const PimplBase &) const = 0;
|
||||
virtual bool operator <=(const PimplBase &) const = 0;
|
||||
virtual bool operator >=(const PimplBase &) const = 0;
|
||||
virtual void *impl() = 0;
|
||||
virtual void assertType(std::type_index) const = 0;
|
||||
};
|
||||
|
||||
template <class I> class Pimpl : public PimplBase
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_same<T, typename std::iterator_traits<I>::value_type>::value,
|
||||
"RandomAccessIterator must be initialized from an iterator with the same value_type.");
|
||||
static_assert(std::is_same<typename std::iterator_traits<I>::iterator_category, std::random_access_iterator_tag>::value,
|
||||
"RandomAccessIterator must be initialized from a random access iterator.");
|
||||
Pimpl(I &&i) : m_impl(std::move(i)) {}
|
||||
virtual PimplBase *clone() const override { return new Pimpl(*this); }
|
||||
virtual const_reference operator *() const override { return *m_impl; }
|
||||
virtual reference operator *() override { return *m_impl; }
|
||||
virtual void operator ++() override { ++m_impl; }
|
||||
virtual void operator --() override { --m_impl; }
|
||||
virtual void operator +=(difference_type n) override { m_impl += n; }
|
||||
virtual void operator -=(difference_type n) override { m_impl -= n; }
|
||||
virtual difference_type operator -(const PimplBase &other) const override { return m_impl - static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator ==(const PimplBase &other) const override { return m_impl == static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator <(const PimplBase &other) const override { return m_impl < static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator >(const PimplBase &other) const override { return m_impl > static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator <=(const PimplBase &other) const override { return m_impl <= static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual bool operator >=(const PimplBase &other) const override { return m_impl >= static_cast<const Pimpl&>(other).m_impl; }
|
||||
virtual void *impl() override { return &m_impl; }
|
||||
virtual void assertType(std::type_index ti) const override { Q_ASSERT(ti == typeid(I)); Q_UNUSED(ti); }
|
||||
private:
|
||||
I m_impl;
|
||||
};
|
||||
|
||||
using PimplPtr = QScopedPointer<PimplBase>;
|
||||
PimplPtr m_pimpl;
|
||||
|
||||
explicit RandomAccessIterator(PimplBase *pimpl) : m_pimpl(pimpl) {} // private ctor used by fromImpl()
|
||||
|
||||
// using these methods to access m_pimpl.data() eases the cognitive burden of correctly forwarding const
|
||||
PimplBase *pimpl() { return m_pimpl.data(); }
|
||||
const PimplBase *pimpl() const { return m_pimpl.data(); }
|
||||
};
|
||||
|
||||
} //namespace Iterators
|
||||
|
||||
} //namespace BlackMisc
|
||||
|
||||
Reference in New Issue
Block a user