mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
refs #290 added some new predicates and transform functions to use in combination with the iterator adaptors.
This required refactoring index_sequence out of tuple_private.h so it could be used by the predicates.
This commit is contained in:
@@ -10,8 +10,10 @@
|
||||
#ifndef BLACKMISC_PREDICATES_H
|
||||
#define BLACKMISC_PREDICATES_H
|
||||
|
||||
#include <functional>
|
||||
#include "index_sequence.h"
|
||||
#include <QObject>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -63,6 +65,39 @@ namespace BlackMisc
|
||||
template <class T> bool operator()(const T &a, const T &b) const { return head.isStable(a, b) ? head(a, b) : tail(a, b); }
|
||||
};
|
||||
|
||||
//! \private
|
||||
template <class T, class M> struct MemberTransform
|
||||
{
|
||||
M m;
|
||||
MemberTransform(M m_) : m(m_) {}
|
||||
auto operator()(const T &v) const -> decltype((v.*std::declval<M>())()) { return (v.*m)(); }
|
||||
};
|
||||
|
||||
//! \private
|
||||
template <class T, class M> struct MemberValid
|
||||
{
|
||||
M m;
|
||||
MemberValid(M m_) : m(m_) {}
|
||||
bool operator()(const T &v) const { return (v.*m)().isValid(); }
|
||||
};
|
||||
|
||||
//! \private
|
||||
template <class T, class M, class C> struct MemberIsAnyOf
|
||||
{
|
||||
M m;
|
||||
const C &c;
|
||||
MemberIsAnyOf(M m_, const C &c_) : m(m_), c(c_) {}
|
||||
bool operator()(const T &v) const { return c.contains((v.*m)()); }
|
||||
};
|
||||
|
||||
//! \private
|
||||
template <class T> struct Equals
|
||||
{
|
||||
const T m_value;
|
||||
template <class U> Equals(U &&value) : m_value(std::forward<U>(value)) {}
|
||||
template <class U> bool operator ()(const U &other) const { return other == m_value; }
|
||||
};
|
||||
|
||||
} //namespace Private
|
||||
|
||||
/*!
|
||||
@@ -87,6 +122,57 @@ namespace BlackMisc
|
||||
return typename Private::MemberLess<Ts...>(vs...);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Transformation function object which returns the value returned by one of it's argument member functions.
|
||||
*
|
||||
* A lambda would usually be easier, but it is difficult to directly return a lambda from a function
|
||||
* without C++14 deduced return types.
|
||||
*/
|
||||
template <class T, class R>
|
||||
auto MemberTransform(R(T::*memberFunc)() const) -> Private::MemberTransform<T, decltype(memberFunc)>
|
||||
{
|
||||
return { memberFunc };
|
||||
}
|
||||
|
||||
/*!
|
||||
* Predicate which is true if the isValid() method of the value returned from one of its member functions returns true.
|
||||
*
|
||||
* A lambda would usually be easier, but it is difficult to directly return a lambda from a function
|
||||
* without C++14 deduced return types.
|
||||
*/
|
||||
template <class T, class R>
|
||||
auto MemberValid(R(T::*memberFunc)() const) -> Private::MemberValid<T, decltype(memberFunc)>
|
||||
{
|
||||
return { memberFunc };
|
||||
}
|
||||
|
||||
/*!
|
||||
* Predicate which is true if the value returned by its argument's member function can be found in a captured container.
|
||||
*
|
||||
* A lambda would usually be easier, but it is difficult to directly return a lambda from a function
|
||||
* without C++14 deduced return types.
|
||||
*
|
||||
* \warning The container is captured by reference, so be careful that it remains valid for the lifetime of the predicate.
|
||||
*/
|
||||
template <class T, class R, class C>
|
||||
auto MemberIsAnyOf(R(T::*memberFunc)() const, const C &container) -> Private::MemberIsAnyOf<T, decltype(memberFunc), C>
|
||||
{
|
||||
return { memberFunc, container };
|
||||
}
|
||||
|
||||
/*!
|
||||
* Predicate which is true if its argument compares equal with another, captured value.
|
||||
*
|
||||
* A lambda would usually be easier, but it is difficult to directly return a lambda from a function
|
||||
* without C++14 deduced return types. It is also a generic function object, which is only possible
|
||||
* with C++14 generic lambdas.
|
||||
*/
|
||||
template <class T>
|
||||
auto Equals(T &&value) -> Private::Equals<typename std::decay<T>::type>
|
||||
{
|
||||
return { std::forward<T>(value) };
|
||||
}
|
||||
|
||||
} //namespace Predicates
|
||||
|
||||
} //namespace BlackMisc
|
||||
|
||||
Reference in New Issue
Block a user