|
| 1 | +/* Copyright (C) 2013 |
| 2 | + * swift project Community / Contributors |
| 3 | + * |
| 4 | + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level |
| 5 | + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, |
| 6 | + * or distributed except according to the terms contained in the LICENSE file. |
| 7 | + */ |
| 8 | + |
| 9 | +//! \file |
| 10 | + |
| 11 | +#ifndef BLACKMISC_PROPERTYINDEXREF_H |
| 12 | +#define BLACKMISC_PROPERTYINDEXREF_H |
| 13 | + |
| 14 | +#include "blackmisc/blackmiscexport.h" |
| 15 | +#include "blackmisc/typetraits.h" |
| 16 | +#include <QVector> |
| 17 | + |
| 18 | +namespace BlackMisc |
| 19 | +{ |
| 20 | + class CPropertyIndexRef; |
| 21 | + |
| 22 | + namespace Private |
| 23 | + { |
| 24 | + //! \private |
| 25 | + template <class T, class X> |
| 26 | + int compareByProperty(const T &a, const T &b, const CPropertyIndexRef &index, std::true_type, X) |
| 27 | + { |
| 28 | + return a.comparePropertyByIndex(index, b); |
| 29 | + } |
| 30 | + //! \private |
| 31 | + template <class T> |
| 32 | + int compareByProperty(const T &a, const T &b, const CPropertyIndexRef &index, std::false_type, std::true_type) |
| 33 | + { |
| 34 | + return compare(a.propertyByIndex(index), b.propertyByIndex(index)); |
| 35 | + } |
| 36 | + //! \private |
| 37 | + template <class T> |
| 38 | + int compareByProperty(const T &, const T &, const CPropertyIndexRef &, std::false_type, std::false_type) |
| 39 | + { |
| 40 | + qFatal("Not implemented"); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /*! |
| 46 | + * Non-owning reference to a CPropertyIndex with a subset of its features. |
| 47 | + */ |
| 48 | + class BLACKMISC_EXPORT CPropertyIndexRef |
| 49 | + { |
| 50 | + public: |
| 51 | + //! Construct from a single index. |
| 52 | + CPropertyIndexRef(int index); |
| 53 | + |
| 54 | + //! Construct from the data of a CPropertyIndex. |
| 55 | + explicit CPropertyIndexRef(const QVector<int> &indexes); |
| 56 | + |
| 57 | + //! Forbid accidental constructor from an rvalue. |
| 58 | + explicit CPropertyIndexRef(QVector<int> &&) = delete; |
| 59 | + |
| 60 | + //! Copy with first element removed |
| 61 | + CPropertyIndexRef copyFrontRemoved() const; |
| 62 | + |
| 63 | + //! Is nested index? |
| 64 | + bool isNested() const; |
| 65 | + |
| 66 | + //! Myself index, used with nesting |
| 67 | + bool isMyself() const; |
| 68 | + |
| 69 | + //! Empty? |
| 70 | + bool isEmpty() const; |
| 71 | + |
| 72 | + //! Front to integer |
| 73 | + int frontToInt() const; |
| 74 | + |
| 75 | + //! Starts with given index? |
| 76 | + bool startsWith(int index) const; |
| 77 | + |
| 78 | + //! \copydoc BlackMisc::Mixin::String::toQString |
| 79 | + QString toQString(bool i18n = false) const; |
| 80 | + |
| 81 | + //! First element casted to given type, usually the PropertIndex enum |
| 82 | + template<class CastType> CastType frontCasted() const |
| 83 | + { |
| 84 | + static_assert(std::is_enum<CastType>::value || std::is_integral<CastType>::value, "CastType must be an enum or integer"); |
| 85 | + return static_cast<CastType>(frontToInt()); |
| 86 | + } |
| 87 | + |
| 88 | + //! Compare with index given by enum |
| 89 | + template<class EnumType> bool startsWithPropertyIndexEnum(EnumType ev) const |
| 90 | + { |
| 91 | + static_assert(std::is_enum<EnumType>::value, "Argument must be an enum"); |
| 92 | + return this->startsWith(static_cast<int>(ev)); |
| 93 | + } |
| 94 | + |
| 95 | + //! Return a predicate function which can compare two objects based on this index |
| 96 | + auto comparator() const |
| 97 | + { |
| 98 | + return [index = *this](const auto & a, const auto & b) |
| 99 | + { |
| 100 | + using T = std::decay_t<decltype(a)>; |
| 101 | + return Private::compareByProperty(a, b, index, THasComparePropertyByIndex<T>(), THasPropertyByIndex<T>()); |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + //! an empty property index |
| 106 | + static CPropertyIndexRef empty() { return -1; } |
| 107 | + |
| 108 | + private: |
| 109 | + const int *m_begin = nullptr; |
| 110 | + int m_sizeOrIndex = -1; |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +#endif |
0 commit comments