From c7a0aa2fb48e371886e56fb47e57445c4cbcee37 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Mon, 8 Feb 2016 00:24:28 +0000 Subject: [PATCH] refs #584 Moved Mixin::String to stringutils.h --- src/blackmisc/blackmiscfreefunctions.h | 74 ------------------------- src/blackmisc/dictionary.h | 4 +- src/blackmisc/propertyindex.h | 1 + src/blackmisc/stringutils.h | 76 ++++++++++++++++++++++++++ src/blackmisc/valueobject.h | 1 + src/blackmisc/variant.h | 2 +- tests/blackmisc/testvalueobject.h | 1 + 7 files changed, 83 insertions(+), 76 deletions(-) diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index 34f174e03..3a18cdf6d 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -64,80 +64,6 @@ namespace BlackMisc template static uint baseHash(const T *base) { return qHash(*base); } static uint baseHash(const void *) { return 0; } }; - - /*! - * CRTP class template from which a derived class can inherit string streaming operations. - * - * \tparam Derived Must implement a public method QString convertToQString(bool i18n = false) const. - * - * \see BLACKMISC_DECLARE_USING_MIXIN_STRING - */ - template - class String - { - public: - //! Stream << overload to be used in debugging messages - friend QDebug operator<<(QDebug debug, const Derived &obj) - { - debug << obj.stringForStreaming(); - return debug; - } - - //! Operator << when there is no debug stream - friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj) - { - Q_UNUSED(obj); - return nodebug; - } - - //! Operator << based on text stream - friend QTextStream &operator<<(QTextStream &stream, const Derived &obj) - { - stream << obj.stringForStreaming(); - return stream; - } - - //! Operator << for QDataStream - friend QDataStream &operator<<(QDataStream &stream, const Derived &obj) - { - stream << obj.stringForStreaming(); - return stream; - } - - //! Stream operator << for std::cout - friend std::ostream &operator<<(std::ostream &ostr, const Derived &obj) - { - ostr << obj.stringForStreaming().toStdString(); - return ostr; - } - - //! Cast as QString - QString toQString(bool i18n = false) const { return derived()->convertToQString(i18n); } - - //! Cast to pretty-printed QString - QString toFormattedQString(bool i18n = false) const { return derived()->toQString(i18n); } - - //! To std string - std::string toStdString(bool i18n = false) const { return derived()->convertToQString(i18n).toStdString(); } - - //! String for streaming operators - QString stringForStreaming() const { return derived()->convertToQString(); } - - private: - const Derived *derived() const { return static_cast(this); } - Derived *derived() { return static_cast(this); } - }; - - /*! - * When a derived class and a base class both inherit from Mixin::String, - * the derived class uses this macro to disambiguate the inherited members. - */ -# define BLACKMISC_DECLARE_USING_MIXIN_STRING(DERIVED) \ - using ::BlackMisc::Mixin::String::toQString; \ - using ::BlackMisc::Mixin::String::toFormattedQString; \ - using ::BlackMisc::Mixin::String::toStdString; \ - using ::BlackMisc::Mixin::String::stringForStreaming; - } // Mixin /*! diff --git a/src/blackmisc/dictionary.h b/src/blackmisc/dictionary.h index def73ccbc..e91248cf7 100644 --- a/src/blackmisc/dictionary.h +++ b/src/blackmisc/dictionary.h @@ -12,7 +12,9 @@ #ifndef BLACKMISC_DICTIONARY_H #define BLACKMISC_DICTIONARY_H -#include "valueobject.h" +#include "dbus.h" +#include "json.h" +#include "stringutils.h" #include "iterator.h" #include "range.h" #include "containerbase.h" diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index 92aa0b094..0fc96ebe2 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -14,6 +14,7 @@ #include "blackmiscexport.h" #include "blackmiscfreefunctions.h" +#include "stringutils.h" #include "variant.h" #include "dbus.h" #include "json.h" diff --git a/src/blackmisc/stringutils.h b/src/blackmisc/stringutils.h index 5af4a390d..d9ea95902 100644 --- a/src/blackmisc/stringutils.h +++ b/src/blackmisc/stringutils.h @@ -16,6 +16,7 @@ #include #include #include +#include //! Free functions in BlackMisc namespace BlackMisc @@ -57,6 +58,81 @@ namespace BlackMisc //! Byte array from hex value string per byte, 2 digits BLACKMISC_EXPORT QByteArray byteArrayFromHexString(const QString &hexString); + namespace Mixin + { + /*! + * CRTP class template from which a derived class can inherit string streaming operations. + * + * \tparam Derived Must implement a public method QString convertToQString(bool i18n = false) const. + * + * \see BLACKMISC_DECLARE_USING_MIXIN_STRING + */ + template + class String + { + public: + //! Stream << overload to be used in debugging messages + friend QDebug operator<<(QDebug debug, const Derived &obj) + { + debug << obj.stringForStreaming(); + return debug; + } + + //! Operator << when there is no debug stream + friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj) + { + Q_UNUSED(obj); + return nodebug; + } + + //! Operator << based on text stream + friend QTextStream &operator<<(QTextStream &stream, const Derived &obj) + { + stream << obj.stringForStreaming(); + return stream; + } + + //! Operator << for QDataStream + friend QDataStream &operator<<(QDataStream &stream, const Derived &obj) + { + stream << obj.stringForStreaming(); + return stream; + } + + //! Stream operator << for std::cout + friend std::ostream &operator<<(std::ostream &ostr, const Derived &obj) + { + ostr << obj.stringForStreaming().toStdString(); + return ostr; + } + + //! Cast as QString + QString toQString(bool i18n = false) const { return derived()->convertToQString(i18n); } + + //! Cast to pretty-printed QString + QString toFormattedQString(bool i18n = false) const { return derived()->toQString(i18n); } + + //! To std string + std::string toStdString(bool i18n = false) const { return derived()->convertToQString(i18n).toStdString(); } + + //! String for streaming operators + QString stringForStreaming() const { return derived()->convertToQString(); } + + private: + const Derived *derived() const { return static_cast(this); } + Derived *derived() { return static_cast(this); } + }; + + /*! + * When a derived class and a base class both inherit from Mixin::String, + * the derived class uses this macro to disambiguate the inherited members. + */ +# define BLACKMISC_DECLARE_USING_MIXIN_STRING(DERIVED) \ + using ::BlackMisc::Mixin::String::toQString; \ + using ::BlackMisc::Mixin::String::toFormattedQString; \ + using ::BlackMisc::Mixin::String::toStdString; \ + using ::BlackMisc::Mixin::String::stringForStreaming; + } // ns } // ns #endif // guard diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index f0b525e34..4fd3401ae 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -21,6 +21,7 @@ #include "propertyindexvariantmap.h" #include "iconlist.h" #include "blackmiscfreefunctions.h" +#include "stringutils.h" #include #include #include diff --git a/src/blackmisc/variant.h b/src/blackmisc/variant.h index 12b8fed35..421ed1579 100644 --- a/src/blackmisc/variant.h +++ b/src/blackmisc/variant.h @@ -14,7 +14,7 @@ #include "variantprivate.h" #include "blackmiscexport.h" -#include "blackmiscfreefunctions.h" +#include "stringutils.h" #include "tuple.h" #include "compare.h" #include "dbus.h" diff --git a/tests/blackmisc/testvalueobject.h b/tests/blackmisc/testvalueobject.h index 050476e9f..cab738f08 100644 --- a/tests/blackmisc/testvalueobject.h +++ b/tests/blackmisc/testvalueobject.h @@ -16,6 +16,7 @@ #include "blackmisc/sequence.h" #include "blackmisc/collection.h" #include "blackmisc/dictionary.h" +#include "blackmisc/valueobject.h" namespace BlackMisc {