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