1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Santeri Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program 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 General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #pragma once |
|
20 #include <QString> |
|
21 #include "Types.h" |
|
22 |
|
23 //! \file Format.h |
|
24 //! Contains string formatting-related functions and classes. |
|
25 |
|
26 //! |
|
27 //! Converts a given value into a string that can be retrieved with text(). |
|
28 //! Used as the argument type to the formatting functions, hence its name. |
|
29 //! |
|
30 class StringFormatArg |
|
31 { |
|
32 public: |
|
33 StringFormatArg (const QString& a) : m_text (a) {} |
|
34 StringFormatArg (const char& a) : m_text (a) {} |
|
35 StringFormatArg (const uchar& a) : m_text (a) {} |
|
36 StringFormatArg (const QChar& a) : m_text (a) {} |
|
37 StringFormatArg (int a) : m_text (QString::number (a)) {} |
|
38 StringFormatArg (long a) : m_text (QString::number (a)) {} |
|
39 StringFormatArg (const float& a) : m_text (QString::number (a)) {} |
|
40 StringFormatArg (const double& a) : m_text (QString::number (a)) {} |
|
41 StringFormatArg (const Vertex& a) : m_text (a.toString (false)) {} |
|
42 StringFormatArg (const Matrix& a) : m_text (a.toString()) {} |
|
43 StringFormatArg (const char* a) : m_text (a) {} |
|
44 |
|
45 StringFormatArg (const void* a) |
|
46 { |
|
47 m_text.sprintf ("%p", a); |
|
48 } |
|
49 |
|
50 template<typename T> |
|
51 StringFormatArg (const QList<T>& a) |
|
52 { |
|
53 m_text = "{"; |
|
54 |
|
55 for (const T& it : a) |
|
56 { |
|
57 if (&it != &a.first()) |
|
58 m_text += ", "; |
|
59 |
|
60 StringFormatArg arg (it); |
|
61 m_text += arg.text(); |
|
62 } |
|
63 |
|
64 m_text += "}"; |
|
65 } |
|
66 |
|
67 inline QString text() const |
|
68 { |
|
69 return m_text; |
|
70 } |
|
71 |
|
72 private: |
|
73 QString m_text; |
|
74 }; |
|
75 |
|
76 //! |
|
77 //! Helper function for \c format |
|
78 //! |
|
79 template<typename Arg1, typename... Rest> |
|
80 void formatHelper (QString& str, Arg1 arg1, Rest... rest) |
|
81 { |
|
82 str = str.arg (StringFormatArg (arg1).text()); |
|
83 formatHelper (str, rest...); |
|
84 } |
|
85 |
|
86 //! |
|
87 //! Overload of \c formatHelper() with no template args |
|
88 //! |
|
89 static void formatHelper (QString& str) __attribute__ ((unused)); |
|
90 static void formatHelper (QString& str) |
|
91 { |
|
92 (void) str; |
|
93 } |
|
94 |
|
95 //! |
|
96 //! @brief Format the message with the given args. |
|
97 //! |
|
98 //! The formatting ultimately uses QString's arg() method to actually format |
|
99 //! the args so the format string should be prepared accordingly, with %1 |
|
100 //! referring to the first arg, %2 to the second, etc. |
|
101 //! |
|
102 //! \param fmtstr The string to format |
|
103 //! \param args The args to format with |
|
104 //! \return The formatted string |
|
105 //! |
|
106 template<typename... Args> |
|
107 QString format (QString fmtstr, Args... args) |
|
108 { |
|
109 formatHelper (fmtstr, args...); |
|
110 return fmtstr; |
|
111 } |
|
112 |
|
113 //! |
|
114 //! From MessageLog.cc - declared here so that I don't need to include |
|
115 //! MessageLog.h here. Prints the given message to log. |
|
116 //! |
|
117 void printToLog (const QString& msg); |
|
118 |
|
119 //! |
|
120 //! Format and print the given args to the message log. |
|
121 //! \param fmtstr The string to format |
|
122 //! \param args The args to format with |
|
123 //! |
|
124 template<typename... Args> |
|
125 void print (QString fmtstr, Args... args) |
|
126 { |
|
127 formatHelper (fmtstr, args...); |
|
128 printToLog (fmtstr); |
|
129 } |
|
130 |
|
131 //! |
|
132 //! Format and print the given args to the given file descriptor |
|
133 //! \param fp The file descriptor to print to |
|
134 //! \param fmtstr The string to format |
|
135 //! \param args The args to format with |
|
136 //! |
|
137 template<typename... Args> |
|
138 void fprint (FILE* fp, QString fmtstr, Args... args) |
|
139 { |
|
140 formatHelper (fmtstr, args...); |
|
141 fprintf (fp, "%s", qPrintable (fmtstr)); |
|
142 } |
|
143 |
|
144 //! |
|
145 //! Overload of \c fprint with a QIODevice |
|
146 //! \param dev The IO device to print to |
|
147 //! \param fmtstr The string to format |
|
148 //! \param args The args to format with |
|
149 //! |
|
150 template<typename... Args> |
|
151 void fprint (QIODevice& dev, QString fmtstr, Args... args) |
|
152 { |
|
153 formatHelper (fmtstr, args...); |
|
154 dev.write (fmtstr.toUtf8()); |
|
155 } |
|
156 |
|
157 //! |
|
158 //! Exactly like print() except no-op in release builds. |
|
159 //! \param fmtstr The string to format |
|
160 //! \param args The args to format with |
|
161 //! |
|
162 template<typename... Args> |
|
163 void dprint (QString fmtstr, Args... args) |
|
164 { |
|
165 #ifndef RELEASE |
|
166 formatHelper (fmtstr, args...); |
|
167 printToLog (fmtstr); |
|
168 #else |
|
169 (void) fmtstr; |
|
170 (void) args; |
|
171 #endif |
|
172 } |
|