swift
Loading...
Searching...
No Matches
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
18namespace 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
67
68 private:
69 const Derived *derived() const;
70 Derived *derived();
71 };
72
73 template <class Derived>
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
107#define SWIFT_MISC_DECLARE_USING_MIXIN_STRING(DERIVED) \
108 using ::swift::misc::mixin::String<DERIVED>::toQString; \
109 using ::swift::misc::mixin::String<DERIVED>::toStdString; \
110 using ::swift::misc::mixin::String<DERIVED>::stringForStreaming;
111} // namespace swift::misc::mixin
112
113#endif // SWIFT_MISC_MIXIN_MIXINSTRING_H
CRTP class template from which a derived class can inherit string streaming operations.
Definition mixinstring.h:29
friend QTextStream & operator<<(QTextStream &stream, const Derived &obj)
Operator << based on text stream.
Definition mixinstring.h:46
QString stringForStreaming() const
String for streaming operators.
Definition mixinstring.h:86
friend std::ostream & operator<<(std::ostream &ostr, const Derived &obj)
Stream operator << for std::cout.
Definition mixinstring.h:53
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
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.
std::string toStdString() const const