swift
mixinstring.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_MIXIN_MIXINSTRING_H
7 #define SWIFT_MISC_MIXIN_MIXINSTRING_H
8 
9 #include <ostream>
10 #include <string>
11 
12 #include <QByteArray>
13 #include <QDebug>
14 #include <QString>
15 #include <QStringRef>
16 #include <QStringView>
17 #include <QTextStream>
18 #include <QtGlobal>
19 
20 namespace swift::misc::mixin
21 {
29  template <class Derived>
30  class String
31  {
32  public:
34  friend QDebug operator<<(QDebug debug, const Derived &obj)
35  {
36  debug << obj.stringForStreaming();
37  return debug;
38  }
39 
41  friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj)
42  {
43  Q_UNUSED(obj)
44  return nodebug;
45  }
46 
48  friend QTextStream &operator<<(QTextStream &stream, const Derived &obj)
49  {
50  stream << obj.stringForStreaming();
51  return stream;
52  }
53 
55  friend std::ostream &operator<<(std::ostream &ostr, const Derived &obj)
56  {
57  ostr << obj.stringForStreaming().toStdString();
58  return ostr;
59  }
60 
62  QString toQString(bool i18n = false) const;
63 
65  std::string toStdString(bool i18n = false) const;
66 
68  QString stringForStreaming() const;
69 
70  private:
71  const Derived *derived() const;
72  Derived *derived();
73  };
74 
75  template <class Derived>
76  QString String<Derived>::toQString(bool i18n) const
77  {
78  return derived()->convertToQString(i18n);
79  }
80 
81  template <class Derived>
82  std::string String<Derived>::toStdString(bool i18n) const
83  {
84  return derived()->convertToQString(i18n).toStdString();
85  }
86 
87  template <class Derived>
89  {
90  return derived()->convertToQString();
91  }
92 
93  template <class Derived>
94  const Derived *String<Derived>::derived() const
95  {
96  return static_cast<const Derived *>(this);
97  }
98 
99  template <class Derived>
100  Derived *String<Derived>::derived()
101  {
102  return static_cast<Derived *>(this);
103  }
104 
105  // *INDENT-OFF*
110 #define SWIFT_MISC_DECLARE_USING_MIXIN_STRING(DERIVED) \
111  using ::swift::misc::mixin::String<DERIVED>::toQString; \
112  using ::swift::misc::mixin::String<DERIVED>::toStdString; \
113  using ::swift::misc::mixin::String<DERIVED>::stringForStreaming;
114  // *INDENT-ON*
115 } // namespace swift::misc::mixin
116 
117 #endif // SWIFT_MISC_MIXIN_MIXINSTRING_H
CRTP class template from which a derived class can inherit string streaming operations.
Definition: mixinstring.h:31
friend std::ostream & operator<<(std::ostream &ostr, const Derived &obj)
Stream operator << for std::cout.
Definition: mixinstring.h:55
QString stringForStreaming() const
String for streaming operators.
Definition: mixinstring.h:88
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj)
Operator << when there is no debug stream.
Definition: mixinstring.h:41
friend QTextStream & operator<<(QTextStream &stream, const Derived &obj)
Operator << based on text stream.
Definition: mixinstring.h:48
std::string toStdString(bool i18n=false) const
To std string.
Definition: mixinstring.h:82
friend QDebug operator<<(QDebug debug, const Derived &obj)
Stream << overload to be used in debugging messages.
Definition: mixinstring.h:34
Mixin classes which implement common operations for value classes.