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:
Mathew Sutcliffe
2014-07-03 19:04:13 +01:00
parent ae4413abdd
commit db453a6232
3 changed files with 134 additions and 21 deletions

View File

@@ -0,0 +1,46 @@
/* Copyright (C) 2014 VATSIM Community / authors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BLACKMISC_INDEX_SEQUENCE_H
#define BLACKMISC_INDEX_SEQUENCE_H
#include <tuple>
#include <type_traits>
namespace BlackMisc
{
namespace Private
{
// Inhibit doxygen warnings about missing documentation
//! \cond PRIVATE
// Our own implementation of std::index_sequence (because not implemented by MSVC2013)
template <size_t... Is>
struct index_sequence
{
static const size_t size = sizeof...(Is);
typedef std::tuple<std::integral_constant<size_t, Is>...> tuple_type;
};
template <size_t I, size_t C, size_t... Is>
struct GenSequence
{
typedef typename GenSequence<I + 1, C, Is..., I>::type type;
};
template <size_t C, size_t... Is>
struct GenSequence<C, C, Is...>
{
typedef index_sequence<Is...> type;
};
template <size_t C>
using make_index_sequence = typename GenSequence<0, C>::type;
//! \endcond
} // namespace Private
} // namespace BlackMisc
#endif // guard

View File

@@ -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

View File

@@ -6,6 +6,7 @@
#ifndef BLACKMISC_TUPLE_PRIVATE_H
#define BLACKMISC_TUPLE_PRIVATE_H
#include "index_sequence.h"
#include <QtGlobal>
#include <QDBusArgument>
#include <QHash>
@@ -55,26 +56,6 @@ namespace BlackMisc
return 0;
}
// Our own implementation of std::index_sequence (because not implemented by MSVC2013)
template <size_t... Is>
struct index_sequence
{
static const size_t size = sizeof...(Is);
typedef std::tuple<std::integral_constant<size_t, Is>...> tuple_type;
};
template <size_t I, size_t C, size_t... Is>
struct GenSequence
{
typedef typename GenSequence<I + 1, C, Is..., I>::type type;
};
template <size_t C, size_t... Is>
struct GenSequence<C, C, Is...>
{
typedef index_sequence<Is...> type;
};
template <size_t C>
using make_index_sequence = typename GenSequence<0, C>::type;
// Create an index_sequence containing indices which match a given predicate.
template <class P, size_t I, size_t C, bool B = false, size_t I2 = 0xDeadBeef, size_t... Is>
struct GenSequenceOnPredicate;