refs #628 Use expression SFINAE and the void_t trick to simplify some type traits.

This commit is contained in:
Mathew Sutcliffe
2016-03-25 19:01:07 +00:00
parent f159bdb5bf
commit 7ce1b268df
8 changed files with 117 additions and 85 deletions

View File

@@ -56,6 +56,7 @@ namespace BlackMisc
} }
template <typename T> static bool baseEquals(const T *a, const T *b) { return *a == *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 void *, const void *) { return true; }
static bool baseEquals(const CEmpty *, const CEmpty *) { return true; }
}; };
/*! /*!
@@ -107,6 +108,7 @@ namespace BlackMisc
} }
template <typename T> static bool baseLess(const T *a, const T *b) { return *a < *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 void *, const void *) { return false; }
static bool baseLess(const CEmpty *, const CEmpty *) { return false; }
}; };
/*! /*!
@@ -130,6 +132,7 @@ namespace BlackMisc
} }
template <typename T> static int baseCompare(const T *a, const T *b) { return compare(*a, *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 void *, const void *) { return 0; }
static int baseCompare(const CEmpty *, const CEmpty *) { return 0; }
}; };
} // Mixin } // Mixin

View File

@@ -84,6 +84,8 @@ namespace BlackMisc
template <typename T> static void baseUnmarshall(T *base, const QDBusArgument &arg) { base->unmarshallFromDbus(arg); } template <typename T> static void baseUnmarshall(T *base, const QDBusArgument &arg) { base->unmarshallFromDbus(arg); }
static void baseMarshall(const void *, QDBusArgument &) {} static void baseMarshall(const void *, QDBusArgument &) {}
static void baseUnmarshall(void *, const QDBusArgument &) {} static void baseUnmarshall(void *, const QDBusArgument &) {}
static void baseMarshall(const CEmpty *, QDBusArgument &) {}
static void baseUnmarshall(CEmpty *, const QDBusArgument &) {}
}; };
/*! /*!

View File

@@ -18,6 +18,7 @@
#include "iterator.h" #include "iterator.h"
#include "range.h" #include "range.h"
#include "containerbase.h" #include "containerbase.h"
#include "typetraits.h"
#include <QHash> #include <QHash>
#include <utility> #include <utility>
#include <initializer_list> #include <initializer_list>
@@ -27,29 +28,6 @@ namespace BlackMisc
namespace Private namespace Private
{ {
//! \cond PRIVATE //! \cond PRIVATE
namespace ADL
{
struct NotFound {};
struct FromAny { template <class T> FromAny(const T &); };
NotFound operator <(const FromAny &, const FromAny &);
NotFound operator ==(const FromAny &, const FromAny &);
NotFound qHash(...);
using ::BlackMisc::qHash; // bring hidden name into scope
template <class Key>
constexpr static bool supportsQHash()
{
return ! std::is_same<decltype(std::declval<Key>() == std::declval<Key>()), NotFound>::value &&
! std::is_same<decltype(qHash(std::declval<Key>())), NotFound>::value;
};
template <class Key>
constexpr static bool supportsQMap()
{
return ! std::is_same<decltype(std::declval<Key>() < std::declval<Key>()), NotFound>::value;
};
}
template <bool KeySupportsQHash /* = true */, bool KeySupportsQMap> template <bool KeySupportsQHash /* = true */, bool KeySupportsQMap>
struct AssociativityTraits struct AssociativityTraits
{ {
@@ -75,7 +53,7 @@ namespace BlackMisc
//! Trait to select the appropriate default associative container type depending on what the key type supports //! Trait to select the appropriate default associative container type depending on what the key type supports
template <typename K, typename V> template <typename K, typename V>
using DefaultAssociativeType = typename Private::AssociativityTraits<Private::ADL::supportsQHash<K>(), Private::ADL::supportsQMap<K>()>::template DefaultType<K, V>; using DefaultAssociativeType = typename Private::AssociativityTraits<ModelsQHashKey<K>::value, ModelsQMapKey<K>::value>::template DefaultType<K, V>;
//! Associative container with value semantics, chooses a sensible default implementation container type //! Associative container with value semantics, chooses a sensible default implementation container type
template<class Key, class Value, template <class...> class Impl = DefaultAssociativeType> template<class Key, class Value, template <class...> class Impl = DefaultAssociativeType>
@@ -495,6 +473,7 @@ namespace BlackMisc
template <typename T> static uint baseHash(const T *base) { return qHash(*base); } template <typename T> static uint baseHash(const T *base) { return qHash(*base); }
static uint baseHash(const void *) { return 0; } static uint baseHash(const void *) { return 0; }
static uint baseHash(const CEmpty *) { return 0; }
}; };
} }

View File

@@ -12,35 +12,36 @@
#ifndef BLACKMISC_INHERITANCE_TRAITS_H #ifndef BLACKMISC_INHERITANCE_TRAITS_H
#define BLACKMISC_INHERITANCE_TRAITS_H #define BLACKMISC_INHERITANCE_TRAITS_H
#include "typetraits.h"
#include <QMetaType> #include <QMetaType>
#include <type_traits> #include <type_traits>
namespace BlackMisc namespace BlackMisc
{ {
class CEmpty; class CPropertyIndex;
/*! /*!
* If T has a member typedef base_type, this trait will obtain it, otherwise void. * If T has a member typedef base_type, this trait will obtain it, otherwise void.
*/ */
template <class T> template <typename T, typename = void_t<>>
class BaseOf struct BaseOf
{ {
//template <typename U> static typename U::base_type *test(int); using type = void; //!< void
template <typename U> static typename U::base_type *test(std::enable_if_t<! std::is_same<typename U::base_type, CEmpty>::value, int>);
template <typename U> static void *test(...);
public:
//! The declared base_type of T, or void if there is none.
using type = std::remove_pointer_t<decltype(test<T>(0))>;
}; };
//! \cond
template <typename T>
struct BaseOf<T, void_t<typename T::base_type>>
{
using type = typename T::base_type; //!< T::base_type
};
//! \endcond
/*! /*!
* It T has a member typedef base_type which is a registered metatype, this trait will obtain it, otherwise void. * It T has a member typedef base_type which is a registered metatype, this trait will obtain it, otherwise void.
*/ */
template <class T> template <class T>
class MetaBaseOf struct MetaBaseOf
{ {
public:
//! Type of T::base_type, or void if not declared. //! Type of T::base_type, or void if not declared.
using type = std::conditional_t<QMetaTypeId<typename BaseOf<T>::type>::Defined, typename BaseOf<T>::type, void>; using type = std::conditional_t<QMetaTypeId<typename BaseOf<T>::type>::Defined, typename BaseOf<T>::type, void>;
}; };
@@ -48,22 +49,18 @@ namespace BlackMisc
/*! /*!
* If T has a member typedef base_type which has a member propertyByIndex, this trait will obtain it, otherwise void. * If T has a member typedef base_type which has a member propertyByIndex, this trait will obtain it, otherwise void.
*/ */
template <class T> template <typename T, typename = void_t<>>
class IndexBaseOf struct IndexBaseOf
{ {
// http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector using type = void; //!< void
struct Empty {};
struct Fallback { int propertyByIndex; };
template <int Fallback:: *> struct int_t { using type = int; };
template <typename U> struct Derived : public Fallback, public std::conditional_t<std::is_void<U>::value, Empty, U> {};
template <typename U> static void test(typename int_t<&Derived<U>::propertyByIndex>::type);
template <typename U> static U test(...);
public:
//! Type of T::base_type, or void if not declared.
using type = decltype(test<typename BaseOf<T>::type>(0));
}; };
//! \cond
template <typename T>
struct IndexBaseOf<T, void_t<decltype(std::declval<typename T::base_type>().propertyByIndex(std::declval<CPropertyIndex>()))>>
{
using type = typename T::base_type; //!< T::base_type
};
//! \endcond
/*! /*!
* Alias for typename BaseOf<T>::type. * Alias for typename BaseOf<T>::type.

View File

@@ -282,6 +282,8 @@ namespace BlackMisc
template <typename T> static void baseConvertFromJson(T *base, const QJsonObject &json) { base->convertFromJson(json); } template <typename T> static void baseConvertFromJson(T *base, const QJsonObject &json) { base->convertFromJson(json); }
static QJsonObject baseToJson(const void *) { return {}; } static QJsonObject baseToJson(const void *) { return {}; }
static void baseConvertFromJson(void *, const QJsonObject &) {} static void baseConvertFromJson(void *, const QJsonObject &) {}
static QJsonObject baseToJson(const CEmpty *) { return {}; }
static void baseConvertFromJson(CEmpty *, const QJsonObject &) {}
}; };
/*! /*!

View File

@@ -16,6 +16,7 @@
#include "logcategory.h" #include "logcategory.h"
#include "sequence.h" #include "sequence.h"
#include "collection.h" #include "collection.h"
#include "typetraits.h"
#include <QObject> #include <QObject>
#include <QThreadStorage> #include <QThreadStorage>
#include <type_traits> #include <type_traits>
@@ -23,21 +24,6 @@
namespace BlackMisc namespace BlackMisc
{ {
namespace Private
{
//! \private Trait to detect whether a class T has a member function called getLogCategories
template <class T> class HasGetLogCategories
{
struct Base { int getLogCategories; };
struct Derived : T, Base {};
template <typename U, U> struct TypeCheck {};
template <typename U> static std::false_type test(TypeCheck<decltype(&Base::getLogCategories), &U::getLogCategories> *);
template <typename U> static std::true_type test(...);
public:
using type = decltype(test<Derived>(nullptr));
};
}
/*! /*!
* A sequence of log categories. * A sequence of log categories.
*/ */
@@ -117,7 +103,7 @@ namespace BlackMisc
static QThreadStorage<CLogCategoryList> list; static QThreadStorage<CLogCategoryList> list;
if (! list.hasLocalData()) if (! list.hasLocalData())
{ {
list.localData().appendCategoriesFromMemberFunction(tag<T>(), typename Private::HasGetLogCategories<T>::type()); list.localData().appendCategoriesFromMemberFunction(tag<T>(), HasGetLogCategories<T>());
list.localData().appendCategoriesFromMetaType(tag<T>(), std::integral_constant<bool, QMetaTypeId<T>::Defined>()); list.localData().appendCategoriesFromMetaType(tag<T>(), std::integral_constant<bool, QMetaTypeId<T>::Defined>());
list.localData().appendCategoriesFromMetaObject(tag<T>(), std::is_base_of<QObject, T>()); list.localData().appendCategoriesFromMetaObject(tag<T>(), std::is_base_of<QObject, T>());
if (list.localData().isEmpty()) { list.localData().push_back(CLogCategory::uncategorized()); } if (list.localData().isEmpty()) { list.localData().push_back(CLogCategory::uncategorized()); }

View File

@@ -17,6 +17,7 @@
#include "propertyindex.h" #include "propertyindex.h"
#include "logcategorylist.h" #include "logcategorylist.h"
#include "timestampbased.h" #include "timestampbased.h"
#include "typetraits.h"
#include <QReadWriteLock> #include <QReadWriteLock>
namespace BlackMisc namespace BlackMisc
@@ -29,25 +30,6 @@ namespace BlackMisc
BLACKMISC_EXPORT QString arg(const QString &format, const QStringList &args); BLACKMISC_EXPORT QString arg(const QString &format, const QStringList &args);
} }
/*!
* Trait to detect whether T contains a member toQString.
*/
template <typename T>
class HasToQString
{
// http://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
struct Fallback { int toQString; };
template <int Fallback:: *> struct int_t { using type = int; };
template <typename U, bool = std::is_class<U>::value> struct Derived : public U, public Fallback {};
template <typename U> struct Derived<U, false> : public Fallback {};
template <typename U> static char test(typename int_t<&Derived<U>::toQString>::type);
template <typename U> static int test(...);
public:
//! True if T contains a member toQString.
static const bool value = sizeof(test<T>(0)) > 1;
};
/*! /*!
* Status severities * Status severities
*/ */

View File

@@ -0,0 +1,81 @@
/* Copyright (C) 2016
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_TYPETRAITS_H
#define BLACKMISC_TYPETRAITS_H
#if defined(Q_CC_CLANG) || (defined(Q_CC_GNU) && __GNUC__ >= 5)
#define BLACK_HAS_FIXED_CWG1558
#endif
namespace BlackMisc
{
//! \cond PRIVATE
#ifdef BLACK_HAS_FIXED_CWG1558
// Own implementation of C++17 std::void_t, simple variadic alias
// template which is always void. Useful for expression SFINAE.
template <typename...>
using void_t = void;
#else // Work around defect in the C++ standard
namespace Private
{
template <typename...>
struct make_void { using type = void; };
}
template <typename... Ts>
using void_t = typename Private::make_void<Ts...>::type;
#endif
//! \endcond
/*!
* Trait to detect whether T contains a member function toQString.
*/
template <typename T, typename = void_t<>>
struct HasToQString : public std::false_type {};
//! \cond
template <typename T>
struct HasToQString<T, void_t<decltype(std::declval<T>().toQString())>> : public std::true_type {};
//! \endcond
/*!
* Trait to detect whether a class T has a static member function named getLogCategories.
*/
template <typename T, typename = void_t<>>
struct HasGetLogCategories : public std::false_type {};
//! \cond
template <typename T>
struct HasGetLogCategories<T, void_t<decltype(T::getLogCategories())>> : public std::true_type {};
//! \endcond
/*!
* Trait to detect whether a class T can be used as a key in a QHash.
*/
template <typename T, typename = void_t<>>
struct ModelsQHashKey : public std::false_type {};
//! \cond
template <typename T>
struct ModelsQHashKey<T, void_t<decltype(std::declval<T>() == std::declval<T>(), qHash(std::declval<T>()))>> : public std::true_type {};
//! \endcond
/*!
* Trait to detect whether a class T can be used as a key in a QMap.
*/
template <typename T, typename = void_t<>>
struct ModelsQMapKey : public std::false_type {};
//! \cond
template <typename T>
struct ModelsQMapKey<T, void_t<decltype(std::declval<T>() < std::declval<T>())>> : public std::true_type {};
//! \endcond
}
#endif