libquentier  0.5.0
The library for rich desktop clients of Evernote service
Printable.h
1 /*
2  * Copyright 2016-2020 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 #ifndef LIB_QUENTIER_UTILITY_PRINTABLE_H
20 #define LIB_QUENTIER_UTILITY_PRINTABLE_H
21 
22 #include <quentier/utility/Linkage.h>
23 
24 #include <QDebug>
25 #include <QHash>
26 #include <QSet>
27 #include <QString>
28 #include <QTextStream>
29 
30 namespace quentier {
31 
37 class QUENTIER_EXPORT Printable
38 {
39 public:
40  virtual QTextStream & print(QTextStream & strm) const = 0;
41 
42  virtual const QString toString() const;
43 
44  friend QUENTIER_EXPORT QTextStream & operator<<(
45  QTextStream & strm, const Printable & printable);
46 
47  friend QUENTIER_EXPORT QDebug & operator<<(
48  QDebug & debug, const Printable & printable);
49 
50 protected:
51  Printable();
52  Printable(const Printable & other);
53  Printable & operator=(const Printable & other);
54  virtual ~Printable();
55 };
56 
57 } // namespace quentier
58 
59 // printing operators for existing classes not inheriting from Printable
60 
61 template <class T>
62 const QString ToString(const T & object)
63 {
64  QString str;
65  QTextStream strm(&str, QIODevice::WriteOnly);
66  strm << object;
67  return str;
68 }
69 
70 template <class TKey, class TValue>
71 const QString ToString(const QHash<TKey, TValue> & object)
72 {
73  QString str;
74  QTextStream strm(&str, QIODevice::WriteOnly);
75  strm << QStringLiteral("QHash: \n");
76 
77  typedef typename QHash<TKey, TValue>::const_iterator CIter;
78  CIter hashEnd = object.end();
79  for (CIter it = object.begin(); it != hashEnd; ++it) {
80  strm << QStringLiteral("[") << it.key() << QStringLiteral("] = ")
81  << it.value() << QStringLiteral(";\n");
82  }
83  return str;
84 }
85 
86 template <class T>
87 const QString ToString(const QSet<T> & object)
88 {
89  QString str;
90  QTextStream strm(&str, QIODevice::WriteOnly);
91  strm << QStringLiteral("QSet: \n");
92 
93  typedef typename QSet<T>::const_iterator CIter;
94  CIter setEnd = object.end();
95  for (CIter it = object.begin(); it != setEnd; ++it) {
96  strm << QStringLiteral("[") << *it << QStringLiteral("];\n");
97  }
98  return str;
99 }
100 
101 #define QUENTIER_DECLARE_PRINTABLE(type, ...) \
102  QUENTIER_EXPORT QTextStream & operator<<( \
103  QTextStream & strm, const type & obj); \
104  inline QDebug & operator<<(QDebug & debug, const type & obj) \
105  { \
106  debug << ToString<type, ##__VA_ARGS__>(obj); \
107  return debug; \
108  } \
109  // QUENTIER_DECLARE_PRINTABLE
110 
111 #endif // LIB_QUENTIER_UTILITY_PRINTABLE_H
Definition: DecryptedTextManager.h:26
The Printable class is the interface for Quentier&#39;s internal classes which should be able to write th...
Definition: Printable.h:37