mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-25 02:05:43 +08:00
refs #584 Moved Mixin::String to stringutils.h
This commit is contained in:
@@ -64,80 +64,6 @@ 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; }
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
|
||||||
* 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 Derived>
|
|
||||||
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<const Derived *>(this); }
|
|
||||||
Derived *derived() { return static_cast<Derived *>(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<DERIVED>::toQString; \
|
|
||||||
using ::BlackMisc::Mixin::String<DERIVED>::toFormattedQString; \
|
|
||||||
using ::BlackMisc::Mixin::String<DERIVED>::toStdString; \
|
|
||||||
using ::BlackMisc::Mixin::String<DERIVED>::stringForStreaming;
|
|
||||||
|
|
||||||
} // Mixin
|
} // Mixin
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
@@ -12,7 +12,9 @@
|
|||||||
#ifndef BLACKMISC_DICTIONARY_H
|
#ifndef BLACKMISC_DICTIONARY_H
|
||||||
#define BLACKMISC_DICTIONARY_H
|
#define BLACKMISC_DICTIONARY_H
|
||||||
|
|
||||||
#include "valueobject.h"
|
#include "dbus.h"
|
||||||
|
#include "json.h"
|
||||||
|
#include "stringutils.h"
|
||||||
#include "iterator.h"
|
#include "iterator.h"
|
||||||
#include "range.h"
|
#include "range.h"
|
||||||
#include "containerbase.h"
|
#include "containerbase.h"
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include "blackmiscexport.h"
|
#include "blackmiscexport.h"
|
||||||
#include "blackmiscfreefunctions.h"
|
#include "blackmiscfreefunctions.h"
|
||||||
|
#include "stringutils.h"
|
||||||
#include "variant.h"
|
#include "variant.h"
|
||||||
#include "dbus.h"
|
#include "dbus.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
//! Free functions in BlackMisc
|
//! Free functions in BlackMisc
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
@@ -57,6 +58,81 @@ namespace BlackMisc
|
|||||||
//! Byte array from hex value string per byte, 2 digits
|
//! Byte array from hex value string per byte, 2 digits
|
||||||
BLACKMISC_EXPORT QByteArray byteArrayFromHexString(const QString &hexString);
|
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 Derived>
|
||||||
|
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<const Derived *>(this); }
|
||||||
|
Derived *derived() { return static_cast<Derived *>(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<DERIVED>::toQString; \
|
||||||
|
using ::BlackMisc::Mixin::String<DERIVED>::toFormattedQString; \
|
||||||
|
using ::BlackMisc::Mixin::String<DERIVED>::toStdString; \
|
||||||
|
using ::BlackMisc::Mixin::String<DERIVED>::stringForStreaming;
|
||||||
|
} // ns
|
||||||
} // ns
|
} // ns
|
||||||
|
|
||||||
#endif // guard
|
#endif // guard
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include "propertyindexvariantmap.h"
|
#include "propertyindexvariantmap.h"
|
||||||
#include "iconlist.h"
|
#include "iconlist.h"
|
||||||
#include "blackmiscfreefunctions.h"
|
#include "blackmiscfreefunctions.h"
|
||||||
|
#include "stringutils.h"
|
||||||
#include <QtDBus/QDBusMetaType>
|
#include <QtDBus/QDBusMetaType>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
#include "variantprivate.h"
|
#include "variantprivate.h"
|
||||||
#include "blackmiscexport.h"
|
#include "blackmiscexport.h"
|
||||||
#include "blackmiscfreefunctions.h"
|
#include "stringutils.h"
|
||||||
#include "tuple.h"
|
#include "tuple.h"
|
||||||
#include "compare.h"
|
#include "compare.h"
|
||||||
#include "dbus.h"
|
#include "dbus.h"
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "blackmisc/sequence.h"
|
#include "blackmisc/sequence.h"
|
||||||
#include "blackmisc/collection.h"
|
#include "blackmisc/collection.h"
|
||||||
#include "blackmisc/dictionary.h"
|
#include "blackmisc/dictionary.h"
|
||||||
|
#include "blackmisc/valueobject.h"
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user