libquentier 0.8.0
The library for rich desktop clients of Evernote service
Loading...
Searching...
No Matches
Printable.h
1/*
2 * Copyright 2016-2024 Dmitry Ivanov
3 *
4 * This file is part of libquentier
5 *
6 * libquentier is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, version 3 of the License.
9 *
10 * libquentier is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libquentier. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <quentier/utility/Linkage.h>
22
23#include <QDebug>
24#include <QHash>
25#include <QIODevice>
26#include <QSet>
27#include <QString>
28#include <QTextStream>
29
30namespace quentier {
31
38{
39public:
40 virtual ~Printable() noexcept;
41
42 virtual QTextStream & print(QTextStream & strm) const = 0;
43
44 [[nodiscard]] QString toString() const;
45
48
50 QDebug & debug, const Printable & printable);
51};
52
53} // namespace quentier
54
55// printing operators for existing classes not inheriting from Printable
56
57template <class T>
58[[nodiscard]] QString ToString(const T & object)
59{
60 QString str;
61 QTextStream strm(&str, QIODevice::WriteOnly);
62 strm << object;
63 return str;
64}
65
66template <class TKey, class TValue>
67[[nodiscard]] QString ToString(const QHash<TKey, TValue> & object)
68{
69 QString str;
70 QTextStream strm(&str, QIODevice::WriteOnly);
71 strm << QStringLiteral("QHash: \n");
72
73 using CIter = typename QHash<TKey, TValue>::const_iterator;
74 CIter hashEnd = object.end();
75 for (CIter it = object.begin(); it != hashEnd; ++it) {
76 strm << QStringLiteral("[") << it.key() << QStringLiteral("] = ")
77 << it.value() << QStringLiteral(";\n");
78 }
79 return str;
80}
81
82template <class T>
83[[nodiscard]] QString ToString(const QSet<T> & object)
84{
85 QString str;
86 QTextStream strm(&str, QIODevice::WriteOnly);
87 strm << QStringLiteral("QSet: \n");
88
89 using CIter = typename QSet<T>::const_iterator;
90 CIter setEnd = object.end();
91 for (CIter it = object.begin(); it != setEnd; ++it) {
92 strm << QStringLiteral("[") << *it << QStringLiteral("];\n");
93 }
94 return str;
95}
96
97#define QUENTIER_DECLARE_PRINTABLE(type, ...) \
98 QUENTIER_EXPORT QTextStream & operator<<( \
99 QTextStream & strm, const type & obj); \
100 inline QDebug & operator<<(QDebug & debug, const type & obj) \
101 { \
102 debug << ToString<type, ##__VA_ARGS__>(obj); \
103 return debug; \
104 } \
105 // QUENTIER_DECLARE_PRINTABLE
The Printable class is the interface for Quentier's internal classes which should be able to write th...
Definition Printable.h:38
The Result template class represents the bare bones result monad implementation which either contains...
Definition Result.h:38