Skip to content

Commit ecac4bc

Browse files
committed
Issue #77 Add class CPropertyIndexRef
This is a reference to a CPropertyIndex with a subset of its API, and fewer dependencies.
1 parent c2ac37f commit ecac4bc

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

src/blackmisc/aviation/atcstation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,15 @@ namespace BlackMisc
435435

436436
int CAtcStation::comparePropertyByIndex(const CPropertyIndex &index, const CAtcStation &compareValue) const
437437
{
438-
if (index.isMyself()) { return this->getCallsign().comparePropertyByIndex(CPropertyIndex::empty(), compareValue.getCallsign()); }
438+
if (index.isMyself()) { return this->getCallsign().comparePropertyByIndex(CPropertyIndexRef::empty(), compareValue.getCallsign()); }
439439
const ColumnIndex i = index.frontCasted<ColumnIndex>();
440440
switch (i)
441441
{
442442
case IndexBookedFrom: return Compare::compare(this->getBookedFromUtc(), compareValue.getBookedFromUtc());
443443
case IndexBookedUntil: return Compare::compare(this->getBookedUntilUtc(), compareValue.getBookedUntilUtc());
444444
case IndexCallsignString:
445445
case IndexCallsignStringCrossCopuled:
446-
return m_callsign.comparePropertyByIndex(CPropertyIndex::empty(), compareValue.getCallsign());
446+
return m_callsign.comparePropertyByIndex(CPropertyIndexRef::empty(), compareValue.getCallsign());
447447
case IndexCallsign: return m_callsign.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getCallsign());
448448
case IndexController: return m_controller.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getController());
449449
case IndexFrequency: return m_frequency.comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getFrequency());

src/blackmisc/propertyindex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ namespace BlackMisc
3232
this->parseFromString(indexes);
3333
}
3434

35+
CPropertyIndex::operator CPropertyIndexRef() const
36+
{
37+
return CPropertyIndexRef(m_indexes);
38+
}
39+
3540
CPropertyIndex CPropertyIndex::copyFrontRemoved() const
3641
{
3742
BLACK_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");

src/blackmisc/propertyindex.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef BLACKMISC_PROPERTYINDEX_H
1212
#define BLACKMISC_PROPERTYINDEX_H
1313

14+
#include "blackmisc/propertyindexref.h"
1415
#include "blackmisc/blackmiscexport.h"
1516
#include "blackmisc/mixin/mixincompare.h"
1617
#include "blackmisc/mixin/mixindbus.h"
@@ -199,6 +200,9 @@ namespace BlackMisc
199200
//! From string
200201
CPropertyIndex(const QString &indexes);
201202

203+
//! Return a simplified non-owning reference
204+
operator CPropertyIndexRef() const;
205+
202206
//! Copy with first element removed
203207
CPropertyIndex copyFrontRemoved() const;
204208

src/blackmisc/propertyindexallclasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "blackmisc/propertyindexlist.h"
1313
#include "blackmisc/propertyindexvariantmap.h"
14+
#include "blackmisc/propertyindexref.h"
1415
#include "blackmisc/propertyindex.h"
1516

1617
#endif // guard

src/blackmisc/propertyindexref.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
#include "blackmisc/propertyindexref.h"
12+
#include "blackmisc/propertyindex.h"
13+
#include "blackmisc/verify.h"
14+
#include <QtGlobal>
15+
16+
namespace BlackMisc
17+
{
18+
CPropertyIndexRef::CPropertyIndexRef(int index) :
19+
m_begin(nullptr),
20+
m_sizeOrIndex(index)
21+
{}
22+
23+
CPropertyIndexRef::CPropertyIndexRef(const QVector<int> &indexes) :
24+
m_begin(indexes.data()),
25+
m_sizeOrIndex(indexes.size())
26+
{}
27+
28+
CPropertyIndexRef CPropertyIndexRef::copyFrontRemoved() const
29+
{
30+
BLACK_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");
31+
if (this->isEmpty() || !m_begin) { return -1; }
32+
CPropertyIndexRef copy = *this;
33+
++copy.m_begin;
34+
--copy.m_sizeOrIndex;
35+
return copy;
36+
}
37+
38+
bool CPropertyIndexRef::isNested() const
39+
{
40+
return m_begin && m_sizeOrIndex > 1;
41+
}
42+
43+
bool CPropertyIndexRef::isMyself() const
44+
{
45+
return this->isEmpty();
46+
}
47+
48+
bool CPropertyIndexRef::isEmpty() const
49+
{
50+
return m_begin ? m_sizeOrIndex < 1 : m_sizeOrIndex < 0;
51+
}
52+
53+
int CPropertyIndexRef::frontToInt() const
54+
{
55+
Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index");
56+
return m_begin ? *m_begin : m_sizeOrIndex;
57+
}
58+
59+
bool CPropertyIndexRef::startsWith(int index) const
60+
{
61+
if (this->isEmpty()) { return false; }
62+
return this->frontToInt() == index;
63+
}
64+
65+
QString CPropertyIndexRef::toQString(bool i18n) const
66+
{
67+
Q_UNUSED(i18n);
68+
QString s;
69+
if (this->isEmpty()) { return s; }
70+
71+
auto it = m_begin ? m_begin : &m_sizeOrIndex;
72+
auto end = it + (m_begin ? m_sizeOrIndex : 1);
73+
for (; it != end; ++it)
74+
{
75+
Q_ASSERT(*it >= static_cast<int>(CPropertyIndex::GlobalIndexCValueObject));
76+
if (!s.isEmpty()) { s.append(";"); }
77+
s.append(QString::number(*it));
78+
}
79+
return s;
80+
}
81+
}

src/blackmisc/propertyindexref.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)