incidental refactoring: simplify some predicates by templating their call operators

This commit is contained in:
Mathew Sutcliffe
2014-07-03 18:27:39 +01:00
parent 8d20c5e061
commit e23e418797
6 changed files with 37 additions and 37 deletions

View File

@@ -59,7 +59,7 @@ namespace BlackMiscTest
CIndexVariantMap newController; CIndexVariantMap newController;
newController.addValue(CAtcStation::IndexController, QVariant::fromValue(CUser("112233", "Jane Doe"))); newController.addValue(CAtcStation::IndexController, QVariant::fromValue(CUser("112233", "Jane Doe")));
atcList.applyIf( atcList.applyIf(
BlackMisc::Predicates::MemberEqual<CAtcStation>(&CAtcStation::getCallsign, CCallsign("eddm_twr")), BlackMisc::Predicates::MemberEqual(&CAtcStation::getCallsign, CCallsign("eddm_twr")),
newController); newController);
qDebug() << "-- after update via predicates"; qDebug() << "-- after update via predicates";
qDebug() << atcList.toQString(); qDebug() << atcList.toQString();

View File

@@ -353,7 +353,7 @@ namespace BlackCore
values.addValue(CAtcStation::IndexFrequency, frequency); values.addValue(CAtcStation::IndexFrequency, frequency);
values.addValue(CAtcStation::IndexPosition, position); values.addValue(CAtcStation::IndexPosition, position);
values.addValue(CAtcStation::IndexRange, range); values.addValue(CAtcStation::IndexRange, range);
this->m_atcStationsOnline.applyIf(BlackMisc::Predicates::MemberEqual<CAtcStation>(&CAtcStation::getCallsign, callsign), values); this->m_atcStationsOnline.applyIf(BlackMisc::Predicates::MemberEqual(&CAtcStation::getCallsign, callsign), values);
emit this->changedAtcStationsOnline(); emit this->changedAtcStationsOnline();
} }
} }
@@ -434,7 +434,7 @@ namespace BlackCore
if (!icaoDataDataFile.hasAircraftDesignator()) return; // give up! if (!icaoDataDataFile.hasAircraftDesignator()) return; // give up!
vm = CIndexVariantMap(CAircraft::IndexIcao, icaoData.toQVariant()); vm = CIndexVariantMap(CAircraft::IndexIcao, icaoData.toQVariant());
} }
this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual<CAircraft>(&CAircraft::getCallsign, callsign), vm); this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual(&CAircraft::getCallsign, callsign), vm);
emit this->changedAircraftsInRange(); emit this->changedAircraftsInRange();
} }
@@ -480,7 +480,7 @@ namespace BlackCore
vm.addValue(CAircraft::IndexTransponder, transponder); vm.addValue(CAircraft::IndexTransponder, transponder);
vm.addValue(CAircraft::IndexSituation, situation); vm.addValue(CAircraft::IndexSituation, situation);
vm.addValue(CAircraft::IndexDistance, distance); vm.addValue(CAircraft::IndexDistance, distance);
this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual<CAircraft>(&CAircraft::getCallsign, callsign), vm); this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual(&CAircraft::getCallsign, callsign), vm);
} }
emit this->changedAircraftsInRange(); emit this->changedAircraftsInRange();
@@ -497,7 +497,7 @@ namespace BlackCore
{ {
// update // update
CIndexVariantMap vm(CAircraft::IndexFrequencyCom1, frequency.toQVariant()); CIndexVariantMap vm(CAircraft::IndexFrequencyCom1, frequency.toQVariant());
this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual<CAircraft>(&CAircraft::getCallsign, callsign), vm); this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual(&CAircraft::getCallsign, callsign), vm);
emit this->changedAircraftsInRange(); emit this->changedAircraftsInRange();
} }

View File

@@ -80,7 +80,7 @@ namespace BlackMisc
template <class K0, class V0, class... KeysValues> template <class K0, class V0, class... KeysValues>
C<T> findBy(K0 k0, V0 v0, KeysValues... keysValues) const C<T> findBy(K0 k0, V0 v0, KeysValues... keysValues) const
{ {
return findBy(BlackMisc::Predicates::MemberEqual<T>(k0, v0, keysValues...)); return findBy(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...));
} }
/*! /*!
@@ -117,7 +117,7 @@ namespace BlackMisc
template <class K0, class V0, class... KeysValues> template <class K0, class V0, class... KeysValues>
bool contains(K0 k0, V0 v0, KeysValues... keysValues) const bool contains(K0 k0, V0 v0, KeysValues... keysValues) const
{ {
return contains(BlackMisc::Predicates::MemberEqual<T>(k0, v0, keysValues...)); return contains(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...));
} }
/*! /*!
@@ -143,7 +143,7 @@ namespace BlackMisc
template <class K0, class V0, class... KeysValues> template <class K0, class V0, class... KeysValues>
void removeIf(K0 k0, V0 v0, KeysValues... keysValues) void removeIf(K0 k0, V0 v0, KeysValues... keysValues)
{ {
removeIf(BlackMisc::Predicates::MemberEqual<T>(k0, v0, keysValues...)); removeIf(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...));
} }
public: public:

View File

@@ -151,7 +151,7 @@ namespace BlackMisc
template <class... Pairs > template <class... Pairs >
CDictionary findKeyBy(Pairs... pairs) const CDictionary findKeyBy(Pairs... pairs) const
{ {
return findKeyBy(BlackMisc::Predicates::MemberEqual<Key>(pairs...)); return findKeyBy(BlackMisc::Predicates::MemberEqual(pairs...));
} }
/*! /*!
@@ -176,7 +176,7 @@ namespace BlackMisc
template <class... Pairs > template <class... Pairs >
CDictionary findValueBy(Pairs... pairs) const CDictionary findValueBy(Pairs... pairs) const
{ {
return findValueBy(BlackMisc::Predicates::MemberEqual<Value>(pairs...)); return findValueBy(BlackMisc::Predicates::MemberEqual(pairs...));
} }
/*! /*!
@@ -193,7 +193,7 @@ namespace BlackMisc
template <class MembFunc, class ReturnValue> template <class MembFunc, class ReturnValue>
bool containsByKey(MembFunc membFunc, ReturnValue returnValue) const bool containsByKey(MembFunc membFunc, ReturnValue returnValue) const
{ {
return containsByKey(BlackMisc::Predicates::MemberEqual<Key>(membFunc, returnValue)); return containsByKey(BlackMisc::Predicates::MemberEqual(membFunc, returnValue));
} }
//! Return true if there is an element for which a given predicate returns true. //! Return true if there is an element for which a given predicate returns true.
@@ -207,7 +207,7 @@ namespace BlackMisc
template <class MembFunc, class ReturnValue> template <class MembFunc, class ReturnValue>
bool containsByValue(MembFunc membFunc, ReturnValue returnValue) const bool containsByValue(MembFunc membFunc, ReturnValue returnValue) const
{ {
return containsByValue(BlackMisc::Predicates::MemberEqual<Value>(membFunc, returnValue)); return containsByValue(BlackMisc::Predicates::MemberEqual(membFunc, returnValue));
} }
//! Remove elements for which a given predicate for value returns true. //! Remove elements for which a given predicate for value returns true.
@@ -236,14 +236,14 @@ namespace BlackMisc
template <class MembFunc, class ReturnValue> template <class MembFunc, class ReturnValue>
void removeByKeyIf(MembFunc membFunc, ReturnValue returnValue) void removeByKeyIf(MembFunc membFunc, ReturnValue returnValue)
{ {
removeByKeyIf(BlackMisc::Predicates::MemberEqual<Key>(membFunc, returnValue)); removeByKeyIf(BlackMisc::Predicates::MemberEqual(membFunc, returnValue));
} }
//! Remove elements for which value matches a particular pair. //! Remove elements for which value matches a particular pair.
template <class MembFunc, class ReturnValue> template <class MembFunc, class ReturnValue>
void removeByValueIf(MembFunc membFunc, ReturnValue returnValue) void removeByValueIf(MembFunc membFunc, ReturnValue returnValue)
{ {
removeByValueIf(BlackMisc::Predicates::MemberEqual<Value>(membFunc, returnValue)); removeByValueIf(BlackMisc::Predicates::MemberEqual(membFunc, returnValue));
} }
//! \copydoc BlackMisc::CValueObject::toQVariant //! \copydoc BlackMisc::CValueObject::toQVariant

View File

@@ -25,42 +25,42 @@ namespace BlackMisc
template <class...> struct MemberEqual; template <class...> struct MemberEqual;
//! \private //! \private
template <class T, class M, class V> struct MemberEqual<T, M, V> template <class M, class V> struct MemberEqual<M, V>
{ {
M m; M m;
V v; V v;
MemberEqual(M m_, V v_) : m(m_), v(v_) {} MemberEqual(M m_, V v_) : m(m_), v(v_) {}
bool operator()(const T &obj) const { return (obj.*m)() == v; } template <class T> bool operator()(const T &obj) const { return (obj.*m)() == v; }
}; };
//! \private //! \private
template <class T, class M, class V, class... Tail> struct MemberEqual<T, M, V, Tail...> template <class M, class V, class... Tail> struct MemberEqual<M, V, Tail...>
{ {
MemberEqual<T, M, V> head; MemberEqual<M, V> head;
MemberEqual<T, Tail...> tail; MemberEqual<Tail...> tail;
MemberEqual(M m, V v, Tail... tail_) : head(m, v), tail(tail_...) {} MemberEqual(M m, V v, Tail... tail_) : head(m, v), tail(tail_...) {}
bool operator()(const T &obj) const { return head(obj) && tail(obj); } template <class T> bool operator()(const T &obj) const { return head(obj) && tail(obj); }
}; };
//! \private //! \private
template <class...> struct MemberLess; template <class...> struct MemberLess;
//! \private //! \private
template <class T, class M> struct MemberLess<T, M> template <class M> struct MemberLess<M>
{ {
M m; M m;
MemberLess(M m_) : m(m_) {} MemberLess(M m_) : m(m_) {}
bool operator()(const T &a, const T &b) const { return (a.*m)() < (b.*m)(); } template <class T> bool operator()(const T &a, const T &b) const { return (a.*m)() < (b.*m)(); }
bool isStable(const T &a, const T &b) const { return (a.*m)() != (b.*m)(); } template <class T> bool isStable(const T &a, const T &b) const { return (a.*m)() != (b.*m)(); }
}; };
//! \private //! \private
template <class T, class M, class... Tail> struct MemberLess<T, M, Tail...> template <class M, class... Tail> struct MemberLess<M, Tail...>
{ {
MemberLess<T, M> head; MemberLess<M> head;
MemberLess<T, Tail...> tail; MemberLess<Tail...> tail;
MemberLess(M m, Tail... tail_) : head(m), tail(tail_...) {} MemberLess(M m, Tail... tail_) : head(m), tail(tail_...) {}
bool operator()(const T &a, const T &b) const { return head.isStable(a, b) ? head(a, b) : tail(a, b); } template <class T> bool operator()(const T &a, const T &b) const { return head.isStable(a, b) ? head(a, b) : tail(a, b); }
}; };
} //namespace Private } //namespace Private
@@ -70,10 +70,10 @@ namespace BlackMisc
* \param vs Pairs of { pointer to member function of T, value to compare it against }. * \param vs Pairs of { pointer to member function of T, value to compare it against }.
* \return A unary functor whose operator() which will perform the actual test. * \return A unary functor whose operator() which will perform the actual test.
*/ */
template <class T, class... Ts> template <class... Ts>
typename Private::MemberEqual<T, Ts...> MemberEqual(Ts... vs) typename Private::MemberEqual<Ts...> MemberEqual(Ts... vs)
{ {
return typename Private::MemberEqual<T, Ts...>(vs...); return typename Private::MemberEqual<Ts...>(vs...);
} }
/*! /*!
@@ -81,10 +81,10 @@ namespace BlackMisc
* \param vs Pointers to member functions of T. * \param vs Pointers to member functions of T.
* \return A binary functor whose operator() which will perform the actual test. * \return A binary functor whose operator() which will perform the actual test.
*/ */
template <class T, class... Ts> template <class... Ts>
typename Private::MemberLess<T, Ts...> MemberLess(Ts... vs) typename Private::MemberLess<Ts...> MemberLess(Ts... vs)
{ {
return typename Private::MemberLess<T, Ts...>(vs...); return typename Private::MemberLess<Ts...>(vs...);
} }
} //namespace Predicates } //namespace Predicates

View File

@@ -308,7 +308,7 @@ namespace BlackMisc
template <class K1, class V1> template <class K1, class V1>
void applyIf(K1 key1, V1 value1, const CIndexVariantMap &newValues) void applyIf(K1 key1, V1 value1, const CIndexVariantMap &newValues)
{ {
applyIf(BlackMisc::Predicates::MemberEqual<T>(key1, value1), newValues); applyIf(BlackMisc::Predicates::MemberEqual(key1, value1), newValues);
} }
/*! /*!
@@ -354,7 +354,7 @@ namespace BlackMisc
template <class K1, class V1> template <class K1, class V1>
void replaceIf(K1 key1, V1 value1, const T &replacement) void replaceIf(K1 key1, V1 value1, const T &replacement)
{ {
replaceIf(BlackMisc::Predicates::MemberEqual<T>(key1, value1), replacement); replaceIf(BlackMisc::Predicates::MemberEqual(key1, value1), replacement);
} }
/*! /*!
@@ -407,7 +407,7 @@ namespace BlackMisc
*/ */
template <class K1, class... Keys> void sortBy(K1 key1, Keys... keys) template <class K1, class... Keys> void sortBy(K1 key1, Keys... keys)
{ {
sort(BlackMisc::Predicates::MemberLess<T>(key1, keys...)); sort(BlackMisc::Predicates::MemberLess(key1, keys...));
} }
/*! /*!
@@ -429,7 +429,7 @@ namespace BlackMisc
template <class K1, class... Keys> template <class K1, class... Keys>
CSequence sortedBy(K1 key1, Keys... keys) const CSequence sortedBy(K1 key1, Keys... keys) const
{ {
return sorted(BlackMisc::Predicates::MemberLess<T>(key1, keys...)); return sorted(BlackMisc::Predicates::MemberLess(key1, keys...));
} }
/*! /*!