mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-18 03:15:34 +08:00
CDictionary: fixed, enabled, and tested automatic selection of appropriate default implementation container.
refs #281
This commit is contained in:
committed by
Roland Winklmeier
parent
482ddf2081
commit
ef93477a82
@@ -13,14 +13,67 @@
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace 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(...);
|
||||
|
||||
template <class Key>
|
||||
struct SupportsQHash : std::integral_constant<bool,
|
||||
! 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>
|
||||
struct SupportsQMap : std::integral_constant<bool,
|
||||
! std::is_same<decltype(std::declval<Key>() < std::declval<Key>()), NotFound>::value
|
||||
> {};
|
||||
}
|
||||
|
||||
template <bool KeySupportsQHash /* = true */, bool KeySupportsQMap>
|
||||
struct AssociativityTraits
|
||||
{
|
||||
template <class Key, class Value>
|
||||
using DefaultType = QHash<Key, Value>;
|
||||
};
|
||||
template <>
|
||||
struct AssociativityTraits<false, true>
|
||||
{
|
||||
template <class Key, class Value>
|
||||
using DefaultType = QMap<Key, Value>;
|
||||
};
|
||||
template <>
|
||||
struct AssociativityTraits<false, false>
|
||||
{
|
||||
template <class Key, class>
|
||||
struct DefaultType { static_assert((std::is_void<Key>::value, false), "Key does not support either QHash or QMap"); };
|
||||
};
|
||||
|
||||
//! \endcond
|
||||
} // namespace Private
|
||||
|
||||
|
||||
//! Trait to select the appropriate default associative container type depending on what the key type supports
|
||||
template <class Key>
|
||||
struct AssociativityTraits : public Private::AssociativityTraits<Private::ADL::SupportsQHash<Key>::value, Private::ADL::SupportsQMap<Key>::value>
|
||||
{};
|
||||
|
||||
//! Associative container with value semantics, chooses a sensible default implementation container type
|
||||
template<class Key, class Value, template <class...> class Impl>
|
||||
template<class Key, class Value, template <class...> class Impl = AssociativityTraits<Key>::template DefaultType>
|
||||
class CDictionary : public CValueObject
|
||||
{
|
||||
|
||||
public:
|
||||
//! The implementation container
|
||||
typedef Impl<Key,Value> impl_type;
|
||||
|
||||
//! \brief STL compatibility
|
||||
//! STL compatibility
|
||||
//! @{
|
||||
typedef Key key_type;
|
||||
typedef Value value_type;
|
||||
|
||||
@@ -106,6 +106,19 @@ namespace BlackMisc
|
||||
QString m_description;
|
||||
};
|
||||
|
||||
//! \cond NO_DOXYGEN
|
||||
struct CNotHashable
|
||||
{
|
||||
int n;
|
||||
bool operator <(const CNotHashable &other) const { return n < other.n; }
|
||||
QString toQString(bool = false) const { return {}; }
|
||||
};
|
||||
inline QJsonArray &operator <<(QJsonArray &a, const CNotHashable &) { return a; }
|
||||
inline const QJsonValueRef &operator >>(const QJsonValueRef &v, CNotHashable &) { return v; }
|
||||
inline QDBusArgument &operator <<(QDBusArgument &a, const CNotHashable &) { return a; }
|
||||
inline const QDBusArgument &operator >>(const QDBusArgument &a, const CNotHashable &) { return a; }
|
||||
//! \endcond
|
||||
|
||||
} // namespace
|
||||
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CTestValueObject, (o.m_name, o.m_description))
|
||||
@@ -113,9 +126,25 @@ Q_DECLARE_METATYPE(BlackMisc::CTestValueObject)
|
||||
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::CTestValueObject>)
|
||||
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::CTestValueObject>)
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::CNotHashable)
|
||||
|
||||
// We need to typedef because 'commas' confuse the Q_DECLARE_METATYPE macro
|
||||
// https://bugreports.qt-project.org/browse/QTBUG-11485
|
||||
typedef BlackMisc::CDictionary<BlackMisc::CTestValueObject, BlackMisc::CTestValueObject, QMap> CValueObjectDictionary;
|
||||
typedef BlackMisc::CDictionary<BlackMisc::CTestValueObject, BlackMisc::CTestValueObject> CValueObjectDictionary;
|
||||
typedef BlackMisc::CDictionary<BlackMisc::CTestValueObject, BlackMisc::CTestValueObject, QHash> CValueObjectHashDictionary;
|
||||
typedef BlackMisc::CDictionary<BlackMisc::CNotHashable, QString> CNotHashableDictionary;
|
||||
typedef BlackMisc::CDictionary<BlackMisc::CNotHashable, QString, QMap> CNotHashableMapDictionary;
|
||||
Q_DECLARE_METATYPE(CValueObjectDictionary)
|
||||
Q_DECLARE_METATYPE(CValueObjectHashDictionary)
|
||||
Q_DECLARE_METATYPE(CNotHashableDictionary)
|
||||
Q_DECLARE_METATYPE(CNotHashableMapDictionary)
|
||||
|
||||
// MSVC has trouble with these checks
|
||||
#if !defined(Q_CC_MSVC)
|
||||
static_assert(std::is_same<CValueObjectDictionary::impl_type, CValueObjectHashDictionary::impl_type>::value,
|
||||
"Expected CValueObjectDictionary to use QHash");
|
||||
static_assert(std::is_same<CNotHashableDictionary::impl_type, CNotHashableMapDictionary::impl_type>::value,
|
||||
"Expected CDictionary<CNotHashableDictionary, Value> to use QMap");
|
||||
#endif // ! Q_CC_MSVC
|
||||
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user