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 <QTextStream>
16 #include <QtGlobal>
17 
18 namespace swift::misc::mixin
19 {
27  template <class Derived>
28  class String
29  {
30  public:
32  friend QDebug operator<<(QDebug debug, const Derived &obj)
33  {
34  debug << obj.stringForStreaming();
35  return debug;
36  }
37 
39  friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj)
40  {
41  Q_UNUSED(obj)
42  return nodebug;
43  }
44 
46  friend QTextStream &operator<<(QTextStream &stream, const Derived &obj)
47  {
48  stream << obj.stringForStreaming();
49  return stream;
50  }
51 
53  friend std::ostream &operator<<(std::ostream &ostr, const Derived &obj)
54  {
55  ostr << obj.stringForStreaming().toStdString();
56  return ostr;
57  }
58 
60  QString toQString(bool i18n = false) const;
61 
63  std::string toStdString(bool i18n = false) const;
64 
66  QString stringForStreaming() const;
67 
68  private:
69  const Derived *derived() const;
70  Derived *derived();
71  };
72 
73  template <class Derived>
74  QString String<Derived>::toQString(bool i18n) const
75  {
76  return derived()->convertToQString(i18n);
77  }
78 
79  template <class Derived>
80  std::string String<Derived>::toStdString(bool i18n) const
81  {
82  return derived()->convertToQString(i18n).toStdString();
83  }
84 
85  template <class Derived>
87  {
88  return derived()->convertToQString();
89  }
90 
91  template <class Derived>
92  const Derived *String<Derived>::derived() const
93  {
94  return static_cast<const Derived *>(this);
95  }
96 
97  template <class Derived>
98  Derived *String<Derived>::derived()
99  {
100  return static_cast<Derived *>(this);
101  }
102 
103  // *INDENT-OFF*
108 #define SWIFT_MISC_DECLARE_USING_MIXIN_STRING(DERIVED) \
109  using ::swift::misc::mixin::String<DERIVED>::toQString; \
110  using ::swift::misc::mixin::String<DERIVED>::toStdString; \
111  using ::swift::misc::mixin::String<DERIVED>::stringForStreaming;
112  // *INDENT-ON*
113 } // namespace swift::misc::mixin
114 
115 #endif // SWIFT_MISC_MIXIN_MIXINSTRING_H
CRTP class template from which a derived class can inherit string streaming operations.
Definition: mixinstring.h:29
friend std::ostream & operator<<(std::ostream &ostr, const Derived &obj)
Stream operator << for std::cout.
Definition: mixinstring.h:53
QString stringForStreaming() const
String for streaming operators.
Definition: mixinstring.h:86
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:74
friend QNoDebug operator<<(QNoDebug nodebug, const Derived &obj)
Operator << when there is no debug stream.
Definition: mixinstring.h:39
friend QTextStream & operator<<(QTextStream &stream, const Derived &obj)
Operator << based on text stream.
Definition: mixinstring.h:46
std::string toStdString(bool i18n=false) const
To std string.
Definition: mixinstring.h:80
friend QDebug operator<<(QDebug debug, const Derived &obj)
Stream << overload to be used in debugging messages.
Definition: mixinstring.h:32
Mixin classes which implement common operations for value classes.