src/format.h

changeset 739
152b33a6d51b
parent 733
cc39df788660
child 784
f82ab4d3c7b4
equal deleted inserted replaced
738:16b63398aa1f 739:152b33a6d51b
15 * You should have received a copy of the GNU General Public License 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/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #pragma once 19 #pragma once
20 #include <QString>
21 #include "basics.h" 20 #include "basics.h"
22 21
23 //! \file Format.h 22 //! \file Format.h
24 //! Contains string formatting-related functions and classes. 23 //! Contains string formatting-related functions and classes.
25 24
28 //! Used as the argument type to the formatting functions, hence its name. 27 //! Used as the argument type to the formatting functions, hence its name.
29 //! 28 //!
30 class StringFormatArg 29 class StringFormatArg
31 { 30 {
32 public: 31 public:
33 StringFormatArg (const QString& a) : m_text (a) {} 32 StringFormatArg (const String& a) : m_text (a) {}
34 StringFormatArg (const char& a) : m_text (a) {} 33 StringFormatArg (const char& a) : m_text (a) {}
35 StringFormatArg (const uchar& a) : m_text (a) {} 34 StringFormatArg (const uchar& a) : m_text (a) {}
36 StringFormatArg (const QChar& a) : m_text (a) {} 35 StringFormatArg (const QChar& a) : m_text (a) {}
37 StringFormatArg (int a) : m_text (QString::number (a)) {} 36 StringFormatArg (int a) : m_text (String::number (a)) {}
38 StringFormatArg (long a) : m_text (QString::number (a)) {} 37 StringFormatArg (long a) : m_text (String::number (a)) {}
39 StringFormatArg (const float& a) : m_text (QString::number (a)) {} 38 StringFormatArg (const float& a) : m_text (String::number (a)) {}
40 StringFormatArg (const double& a) : m_text (QString::number (a)) {} 39 StringFormatArg (const double& a) : m_text (String::number (a)) {}
41 StringFormatArg (const Vertex& a) : m_text (a.toString()) {} 40 StringFormatArg (const Vertex& a) : m_text (a.toString()) {}
42 StringFormatArg (const Matrix& a) : m_text (a.toString()) {} 41 StringFormatArg (const Matrix& a) : m_text (a.toString()) {}
43 StringFormatArg (const char* a) : m_text (a) {} 42 StringFormatArg (const char* a) : m_text (a) {}
44 43
45 StringFormatArg (const void* a) 44 StringFormatArg (const void* a)
62 } 61 }
63 62
64 m_text += "}"; 63 m_text += "}";
65 } 64 }
66 65
67 inline QString text() const 66 inline String text() const
68 { 67 {
69 return m_text; 68 return m_text;
70 } 69 }
71 70
72 private: 71 private:
73 QString m_text; 72 String m_text;
74 }; 73 };
75 74
76 //! 75 //!
77 //! Helper function for \c format 76 //! Helper function for \c format
78 //! 77 //!
79 template<typename Arg1, typename... Rest> 78 template<typename Arg1, typename... Rest>
80 void formatHelper (QString& str, Arg1 arg1, Rest... rest) 79 void formatHelper (String& str, Arg1 arg1, Rest... rest)
81 { 80 {
82 str = str.arg (StringFormatArg (arg1).text()); 81 str = str.arg (StringFormatArg (arg1).text());
83 formatHelper (str, rest...); 82 formatHelper (str, rest...);
84 } 83 }
85 84
86 //! 85 //!
87 //! Overload of \c formatHelper() with no template args 86 //! Overload of \c formatHelper() with no template args
88 //! 87 //!
89 static void formatHelper (QString& str) __attribute__ ((unused)); 88 static void formatHelper (String& str) __attribute__ ((unused));
90 static void formatHelper (QString& str) 89 static void formatHelper (String& str)
91 { 90 {
92 (void) str; 91 (void) str;
93 } 92 }
94 93
95 //! 94 //!
96 //! @brief Format the message with the given args. 95 //! @brief Format the message with the given args.
97 //! 96 //!
98 //! The formatting ultimately uses QString's arg() method to actually format 97 //! The formatting ultimately uses String's arg() method to actually format
99 //! the args so the format string should be prepared accordingly, with %1 98 //! the args so the format string should be prepared accordingly, with %1
100 //! referring to the first arg, %2 to the second, etc. 99 //! referring to the first arg, %2 to the second, etc.
101 //! 100 //!
102 //! \param fmtstr The string to format 101 //! \param fmtstr The string to format
103 //! \param args The args to format with 102 //! \param args The args to format with
104 //! \return The formatted string 103 //! \return The formatted string
105 //! 104 //!
106 template<typename... Args> 105 template<typename... Args>
107 QString format (QString fmtstr, Args... args) 106 String format (String fmtstr, Args... args)
108 { 107 {
109 formatHelper (fmtstr, args...); 108 formatHelper (fmtstr, args...);
110 return fmtstr; 109 return fmtstr;
111 } 110 }
112 111
113 //! 112 //!
114 //! From MessageLog.cc - declared here so that I don't need to include 113 //! From MessageLog.cc - declared here so that I don't need to include
115 //! messageLog.h here. Prints the given message to log. 114 //! messageLog.h here. Prints the given message to log.
116 //! 115 //!
117 void printToLog (const QString& msg); 116 void printToLog (const String& msg);
118 117
119 //! 118 //!
120 //! Format and print the given args to the message log. 119 //! Format and print the given args to the message log.
121 //! \param fmtstr The string to format 120 //! \param fmtstr The string to format
122 //! \param args The args to format with 121 //! \param args The args to format with
123 //! 122 //!
124 template<typename... Args> 123 template<typename... Args>
125 void print (QString fmtstr, Args... args) 124 void print (String fmtstr, Args... args)
126 { 125 {
127 formatHelper (fmtstr, args...); 126 formatHelper (fmtstr, args...);
128 printToLog (fmtstr); 127 printToLog (fmtstr);
129 } 128 }
130 129
133 //! \param fp The file descriptor to print to 132 //! \param fp The file descriptor to print to
134 //! \param fmtstr The string to format 133 //! \param fmtstr The string to format
135 //! \param args The args to format with 134 //! \param args The args to format with
136 //! 135 //!
137 template<typename... Args> 136 template<typename... Args>
138 void fprint (FILE* fp, QString fmtstr, Args... args) 137 void fprint (FILE* fp, String fmtstr, Args... args)
139 { 138 {
140 formatHelper (fmtstr, args...); 139 formatHelper (fmtstr, args...);
141 fprintf (fp, "%s", qPrintable (fmtstr)); 140 fprintf (fp, "%s", qPrintable (fmtstr));
142 } 141 }
143 142
146 //! \param dev The IO device to print to 145 //! \param dev The IO device to print to
147 //! \param fmtstr The string to format 146 //! \param fmtstr The string to format
148 //! \param args The args to format with 147 //! \param args The args to format with
149 //! 148 //!
150 template<typename... Args> 149 template<typename... Args>
151 void fprint (QIODevice& dev, QString fmtstr, Args... args) 150 void fprint (QIODevice& dev, String fmtstr, Args... args)
152 { 151 {
153 formatHelper (fmtstr, args...); 152 formatHelper (fmtstr, args...);
154 dev.write (fmtstr.toUtf8()); 153 dev.write (fmtstr.toUtf8());
155 } 154 }
156 155
158 //! Exactly like print() except no-op in release builds. 157 //! Exactly like print() except no-op in release builds.
159 //! \param fmtstr The string to format 158 //! \param fmtstr The string to format
160 //! \param args The args to format with 159 //! \param args The args to format with
161 //! 160 //!
162 template<typename... Args> 161 template<typename... Args>
163 void dprint (QString fmtstr, Args... args) 162 void dprint (String fmtstr, Args... args)
164 { 163 {
165 #ifndef RELEASE 164 #ifndef RELEASE
166 formatHelper (fmtstr, args...); 165 formatHelper (fmtstr, args...);
167 printToLog (fmtstr); 166 printToLog (fmtstr);
168 #else 167 #else

mercurial