refs #628 Remove the old tuple system.

This commit is contained in:
Mathew Sutcliffe
2016-04-02 19:18:08 +01:00
parent 4bd4baa535
commit 0acb302c63
14 changed files with 23 additions and 1066 deletions

View File

@@ -12,13 +12,14 @@
#ifndef BLACKMISC_COMPARE_H
#define BLACKMISC_COMPARE_H
#include "blackmisc/tuple.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/inheritancetraits.h"
#include "blackmisc/typetraits.h"
namespace BlackMisc
{
class CEmpty;
//! \cond PRIVATE
namespace Private
{
@@ -61,10 +62,9 @@ namespace BlackMisc
* Lexicographically compare two tuples and return negative, positive, or zero,
* if a is less than, greater than, or equal to b.
* Each element is compared with compare(a', b') if supported by the element type, operator less-than otherwise.
* \todo Rename to compare() after removing tuple.h, which contains a function with the same name and signature.
*/
template <typename... Ts, typename... Us>
int compareTuples(const std::tuple<Ts...> &a, const std::tuple<Us...> &b)
int compare(const std::tuple<Ts...> &a, const std::tuple<Us...> &b)
{
static_assert(sizeof...(Ts) == sizeof...(Us), "tuples must be same size");
return Private::CompareHelper<sizeof...(Ts)>::compareTuples(a, b);
@@ -89,29 +89,6 @@ namespace BlackMisc
friend bool operator !=(const Derived &a, const Derived &b) { return compare(a, b) != 0; }
};
/*!
* CRTP class template from which a derived class can inherit operator== implemented by metatuple.
*/
template <class Derived>
class EqualsByTuple : private Private::EncapsulationBreaker
{
public:
//! Equals
friend bool operator ==(const Derived &a, const Derived &b) { return equals(a, b); }
//! Not equal
friend bool operator !=(const Derived &a, const Derived &b) { return ! equals(a, b); }
private:
static bool equals(const Derived &a, const Derived &b)
{
return toMetaTuple(a) == toMetaTuple(b) && baseEquals(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b));
}
template <typename T> static bool baseEquals(const T *a, const T *b) { return *a == *b; }
static bool baseEquals(const void *, const void *) { return true; }
static bool baseEquals(const CEmpty *, const CEmpty *) { return true; }
};
/*!
* CRTP class template from which a derived class can inherit operator== implemented by metaclass.
*/
@@ -158,36 +135,6 @@ namespace BlackMisc
friend bool operator >=(const Derived &a, const Derived &b) { return compare(a, b) >= 0; }
};
/*!
* CRTP class template from which a derived class can inherit operator< implemented by metatuple.
*/
template <class Derived>
class LessThanByTuple : private Private::EncapsulationBreaker
{
public:
//! Less than
friend bool operator <(const Derived &a, const Derived &b) { return less(a, b); }
//! Greater than
friend bool operator >(const Derived &a, const Derived &b) { return less(b, a); }
//! Less than or equal
friend bool operator <=(const Derived &a, const Derived &b) { return ! less(b, a); }
//! Greater than or equal
friend bool operator >=(const Derived &a, const Derived &b) { return ! less(a, b); }
private:
static bool less(const Derived &a, const Derived &b)
{
if (baseLess(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b))) { return true; }
return toMetaTuple(a) < toMetaTuple(b);
}
template <typename T> static bool baseLess(const T *a, const T *b) { return *a < *b; }
static bool baseLess(const void *, const void *) { return false; }
static bool baseLess(const CEmpty *, const CEmpty *) { return false; }
};
/*!
* CRTP class template from which a derived class can inherit operator< implemented by metaclass.
*/
@@ -219,30 +166,6 @@ namespace BlackMisc
static bool baseLess(const CEmpty *, const CEmpty *) { return false; }
};
/*!
* CRTP class template from which a derived class can inherit non-member compare() implemented by metatuple.
*
* \tparam Derived Must be registered with BLACK_DECLARE_TUPLE_CONVERSION.
*/
template <class Derived>
class CompareByTuple : private Private::EncapsulationBreaker
{
public:
//! Return negative, zero, or positive if a is less than, equal to, or greater than b.
friend int compare(const Derived &a, const Derived &b) { return compareImpl(a, b); }
private:
static int compareImpl(const Derived &a, const Derived &b)
{
int baseCmp = baseCompare(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b));
if (baseCmp) { return baseCmp; }
return BlackMisc::compare(toMetaTuple(a), toMetaTuple(b));
}
template <typename T> static int baseCompare(const T *a, const T *b) { return compare(*a, *b); }
static int baseCompare(const void *, const void *) { return 0; }
static int baseCompare(const CEmpty *, const CEmpty *) { return 0; }
};
/*!
* CRTP class template from which a derived class can inherit non-member compare() implemented by metaclass.
*/
@@ -259,7 +182,7 @@ namespace BlackMisc
int baseCmp = baseCompare(static_cast<const BaseOfT<Derived> *>(&a), static_cast<const BaseOfT<Derived> *>(&b));
if (baseCmp) { return baseCmp; }
auto meta = introspect<Derived>().without(MetaFlags<DisabledForComparison>());
return BlackMisc::compareTuples(meta.toCaseAwareTuple(a), meta.toCaseAwareTuple(b));
return BlackMisc::compare(meta.toCaseAwareTuple(a), meta.toCaseAwareTuple(b));
}
template <typename T> static int baseCompare(const T *a, const T *b) { return compare(*a, *b); }
static int baseCompare(const void *, const void *) { return 0; }