From 3bdcd7e3862563e5039c2fe413c87c8b61a67380 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Mon, 8 Feb 2016 00:29:08 +0000 Subject: [PATCH] refs #584 Moved hash related stuff to dictionary.h This also means moving Mixin::Icon from CContainerBase one step down the inheritance hierarchy to resolve what would otherwise have been a circular include dependency. --- src/blackmisc/blackmiscfreefunctions.cpp | 59 ----------------------- src/blackmisc/blackmiscfreefunctions.h | 39 --------------- src/blackmisc/collection.h | 5 +- src/blackmisc/containerbase.h | 4 +- src/blackmisc/dictionary.cpp | 47 ++++++++++++++++++ src/blackmisc/dictionary.h | 39 +++++++++++++++ src/blackmisc/icon.h | 1 + src/blackmisc/pq/physicalquantity.cpp | 2 +- src/blackmisc/propertyindex.h | 2 +- src/blackmisc/propertyindexvariantmap.cpp | 2 +- src/blackmisc/sequence.h | 5 +- src/blackmisc/valueobject.h | 2 +- 12 files changed, 100 insertions(+), 107 deletions(-) create mode 100644 src/blackmisc/dictionary.cpp diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 00629a9b8..34517d570 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -8,67 +8,8 @@ */ #include "blackmiscfreefunctions.h" -#include "math/math.h" -#include "geo/geo.h" -#include "audio/audio.h" -#include "input/input.h" -#include "propertyindexlist.h" -#include "propertyindexvariantmap.h" -#include "namevariantpairlist.h" -#include "variantlist.h" -#include "variantmap.h" -#include "valuecache.h" -#include "rgbcolor.h" -#include "countrylist.h" -#include "statusmessagelist.h" -#include "pixmap.h" -#include "iconlist.h" -#include "identifierlist.h" -#include "logpattern.h" -#include -#include -#include -#include -#include void BlackMisc::initResources() { initBlackMiscResources(); } - -uint BlackMisc::calculateHash(const QList &values, const char *className) -{ - // http://stackoverflow.com/questions/113511/hash-code-implementation/113600#113600 - if (values.isEmpty()) return 0; - uint hash = values.first(); - for (int i = 1; i < values.size(); i++) - { - hash = 37 * hash + values.at(i); - } - - // same values, but different class? - if (className) - { - hash = 37 * hash + qHash(QString(className)); - } - return hash; -} - -uint BlackMisc::calculateHash(const QList &values, const char *className) -{ - QList list; - uint s = 0; - foreach(int i, values) - { - if (i >= 0) - { - list.append(static_cast(i)); - } - else - { - list.append(static_cast(i)); - list.append(s++); - } - } - return calculateHash(list, className); -} diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index 3a18cdf6d..d119362b7 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -38,45 +38,6 @@ namespace BlackMisc //! Init resources BLACKMISC_EXPORT void initResources(); - namespace Mixin - { - /*! - * CRTP class template from which a derived class can inherit common methods dealing with hashing instances by metatuple. - * - * \tparam Derived Must be registered with BLACK_DECLARE_TUPLE_CONVERSION. - */ - template - class HashByTuple : private Private::EncapsulationBreaker - { - public: - //! qHash overload, needed for storing value in a QSet. - friend uint qHash(const Derived &value, uint seed = 0) - { - return ::qHash(hashImpl(value), seed); - } - - private: - static uint hashImpl(const Derived &value) - { - return BlackMisc::qHash(toMetaTuple(value)) ^ baseHash(static_cast *>(&value)); - } - - template static uint baseHash(const T *base) { return qHash(*base); } - static uint baseHash(const void *) { return 0; } - }; - } // Mixin - - /*! - * \brief Calculate a single hash value based on a list of individual hash values - * \param values - * \param className add a hash value for class name on top - * \return - */ - BLACKMISC_EXPORT uint calculateHash(const QList &values, const char *className); - - //! Hash value, but with int list - BLACKMISC_EXPORT uint calculateHash(const QList &values, const char *className); - //! Own implementation of std::make_unique, a C++14 feature not provided by GCC in C++11 mode template std::unique_ptr make_unique(Args &&... args) diff --git a/src/blackmisc/collection.h b/src/blackmisc/collection.h index 9dc8b4538..e11d1489e 100644 --- a/src/blackmisc/collection.h +++ b/src/blackmisc/collection.h @@ -14,6 +14,7 @@ #include "iterator.h" #include "containerbase.h" +#include "icon.h" #include #include #include @@ -55,7 +56,9 @@ namespace BlackMisc * Can take any suitable container class as its implementation at runtime. */ template - class CCollection : public CContainerBase> + class CCollection : + public CContainerBase>, + public Mixin::Icon> { public: //! \brief STL compatibility diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index f5ad93fa1..00a75a26d 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -18,7 +18,6 @@ #include "json.h" #include "variant.h" #include "dbus.h" -#include "icon.h" #include #include @@ -56,8 +55,7 @@ namespace BlackMisc public Mixin::MetaType>, public Mixin::DBusOperators>, public Mixin::JsonOperators>, - public Mixin::String>, - public Mixin::Icon> + public Mixin::String> { public: diff --git a/src/blackmisc/dictionary.cpp b/src/blackmisc/dictionary.cpp new file mode 100644 index 000000000..7520b80a6 --- /dev/null +++ b/src/blackmisc/dictionary.cpp @@ -0,0 +1,47 @@ +/* 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. + */ + +#include "dictionary.h" + +uint BlackMisc::calculateHash(const QList &values, const char *className) +{ + // http://stackoverflow.com/questions/113511/hash-code-implementation/113600#113600 + if (values.isEmpty()) return 0; + uint hash = values.first(); + for (int i = 1; i < values.size(); i++) + { + hash = 37 * hash + values.at(i); + } + + // same values, but different class? + if (className) + { + hash = 37 * hash + qHash(QString(className)); + } + return hash; +} + +uint BlackMisc::calculateHash(const QList &values, const char *className) +{ + QList list; + uint s = 0; + foreach(int i, values) + { + if (i >= 0) + { + list.append(static_cast(i)); + } + else + { + list.append(static_cast(i)); + list.append(s++); + } + } + return calculateHash(list, className); +} diff --git a/src/blackmisc/dictionary.h b/src/blackmisc/dictionary.h index e91248cf7..805587f70 100644 --- a/src/blackmisc/dictionary.h +++ b/src/blackmisc/dictionary.h @@ -468,6 +468,45 @@ namespace BlackMisc } } + namespace Mixin + { + /*! + * CRTP class template from which a derived class can inherit common methods dealing with hashing instances by metatuple. + * + * \tparam Derived Must be registered with BLACK_DECLARE_TUPLE_CONVERSION. + */ + template + class HashByTuple : private Private::EncapsulationBreaker + { + public: + //! qHash overload, needed for storing value in a QSet. + friend uint qHash(const Derived &value, uint seed = 0) + { + return ::qHash(hashImpl(value), seed); + } + + private: + static uint hashImpl(const Derived &value) + { + return BlackMisc::qHash(toMetaTuple(value)) ^ baseHash(static_cast *>(&value)); + } + + template static uint baseHash(const T *base) { return qHash(*base); } + static uint baseHash(const void *) { return 0; } + }; + } + + /*! + * \brief Calculate a single hash value based on a list of individual hash values + * \param values + * \param className add a hash value for class name on top + * \return + */ + BLACKMISC_EXPORT uint calculateHash(const QList &values, const char *className); + + //! Hash value, but with int list + BLACKMISC_EXPORT uint calculateHash(const QList &values, const char *className); + } // namespace BlackMisc #endif // BLACKMISC_DICTIONARY_H diff --git a/src/blackmisc/icon.h b/src/blackmisc/icon.h index 26fc9c6b0..5f0c0ab6e 100644 --- a/src/blackmisc/icon.h +++ b/src/blackmisc/icon.h @@ -17,6 +17,7 @@ #include "variant.h" #include "tuple.h" #include "inheritancetraits.h" +#include "dictionary.h" #include namespace BlackMisc diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index 12ca9dc2c..32a3fe1ee 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -9,7 +9,7 @@ #include "blackmisc/pq/pq.h" #include "blackmisc/comparefunctions.h" -#include "blackmisc/blackmiscfreefunctions.h" +#include "blackmisc/dictionary.h" #include namespace BlackMisc diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index 0fc96ebe2..2a22bf4c5 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -13,7 +13,7 @@ #define BLACKMISC_PROPERTYINDEX_H #include "blackmiscexport.h" -#include "blackmiscfreefunctions.h" +#include "dictionary.h" #include "stringutils.h" #include "variant.h" #include "dbus.h" diff --git a/src/blackmisc/propertyindexvariantmap.cpp b/src/blackmisc/propertyindexvariantmap.cpp index fa4109e76..1320793cb 100644 --- a/src/blackmisc/propertyindexvariantmap.cpp +++ b/src/blackmisc/propertyindexvariantmap.cpp @@ -9,7 +9,7 @@ #include "propertyindexvariantmap.h" #include "propertyindexlist.h" -#include "blackmiscfreefunctions.h" +#include "dictionary.h" namespace BlackMisc { diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index c50011924..16ea75bb9 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -14,6 +14,7 @@ #include "iterator.h" #include "containerbase.h" +#include "icon.h" #include #include #include @@ -31,7 +32,9 @@ namespace BlackMisc * Can take any suitable container class as its implementation at runtime. */ template - class CSequence : public CContainerBase> + class CSequence : + public CContainerBase>, + public Mixin::Icon> { public: //! \brief STL compatibility diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index 4fd3401ae..9a471d219 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -20,7 +20,7 @@ #include "variant.h" #include "propertyindexvariantmap.h" #include "iconlist.h" -#include "blackmiscfreefunctions.h" +#include "dictionary.h" #include "stringutils.h" #include #include