src/format.h

changeset 1011
4fdd74a04790
parent 1010
969b48eddd6b
child 1014
f0a8ecb6a357
child 1217
314e12e23c3a
equal deleted inserted replaced
1010:969b48eddd6b 1011:4fdd74a04790
18 18
19 #pragma once 19 #pragma once
20 #include "basics.h" 20 #include "basics.h"
21 #include "colors.h" 21 #include "colors.h"
22 22
23 // 23
24 // Converts a given value into a string that can be retrieved with text(). 24 // Converts a given value into a string that can be retrieved with text().
25 // Used as the argument type to the formatting functions, hence its name. 25 // Used as the argument type to the formatting functions, hence its name.
26 //
27 class StringFormatArg 26 class StringFormatArg
28 { 27 {
29 public: 28 public:
30 StringFormatArg (const QString& a) : m_text (a) {} 29 StringFormatArg (const QString& a) : m_text (a) {}
31 StringFormatArg (char a) : m_text (a) {} 30 StringFormatArg (char a) : m_text (a) {}
69 68
70 private: 69 private:
71 QString m_text; 70 QString m_text;
72 }; 71 };
73 72
74 // 73
75 // Helper function for \c format 74 // Helper function for format()
76 //
77 template<typename Arg1, typename... Rest> 75 template<typename Arg1, typename... Rest>
78 void formatHelper (QString& str, Arg1 arg1, Rest... rest) 76 void formatHelper (QString& str, Arg1 arg1, Rest... rest)
79 { 77 {
80 str = str.arg (StringFormatArg (arg1).text()); 78 str = str.arg (StringFormatArg (arg1).text());
81 formatHelper (str, rest...); 79 formatHelper (str, rest...);
82 } 80 }
83 81
84 // 82
85 // Overload of \c formatHelper() with no template args
86 //
87 static void formatHelper (QString& str) __attribute__ ((unused)); 83 static void formatHelper (QString& str) __attribute__ ((unused));
88 static void formatHelper (QString& str) 84 static void formatHelper (QString& str)
89 { 85 {
90 (void) str; 86 (void) str;
91 } 87 }
92 88
93 // 89
94 // Format the message with the given args. 90 // Format the message with the given args.
95 // 91 //
96 // The formatting ultimately uses String's arg() method to actually format 92 // The formatting ultimately uses String's arg() method to actually format the args so the format string should be
97 // the args so the format string should be prepared accordingly, with %1 93 // prepared accordingly, with %1 referring to the first arg, %2 to the second, etc.
98 // referring to the first arg, %2 to the second, etc.
99 //
100 template<typename... Args> 94 template<typename... Args>
101 QString format (QString fmtstr, Args... args) 95 QString format (QString fmtstr, Args... args)
102 { 96 {
103 formatHelper (fmtstr, args...); 97 formatHelper (fmtstr, args...);
104 return fmtstr; 98 return fmtstr;
105 } 99 }
106 100
107 //
108 // From messageLog.cc - declared here so that I don't need to include
109 // messageLog.h here. Prints the given message to log.
110 //
111 void PrintToLog (const QString& msg);
112 101
113 // 102 // From messageLog.cc - declared here so that I don't need to include messageLog.h here.
103 void printToLog (const QString& msg);
104
105
114 // Format and print the given args to the message log. 106 // Format and print the given args to the message log.
115 //
116 template<typename... Args> 107 template<typename... Args>
117 void print (QString fmtstr, Args... args) 108 void print (QString fmtstr, Args... args)
118 { 109 {
119 formatHelper (fmtstr, args...); 110 formatHelper (fmtstr, args...);
120 PrintToLog (fmtstr); 111 printToLog (fmtstr);
121 } 112 }
122 113
123 //
124 // Format and print the given args to the given file descriptor
125 //
126 template<typename... Args> 114 template<typename... Args>
127 void fprint (FILE* fp, QString fmtstr, Args... args) 115 void fprint (FILE* fp, QString fmtstr, Args... args)
128 { 116 {
129 formatHelper (fmtstr, args...); 117 formatHelper (fmtstr, args...);
130 fprintf (fp, "%s", qPrintable (fmtstr)); 118 fprintf (fp, "%s", qPrintable (fmtstr));
131 } 119 }
132 120
133 // 121
134 // Overload of fprint with a QIODevice
135 //
136 template<typename... Args> 122 template<typename... Args>
137 void fprint (QIODevice& dev, QString fmtstr, Args... args) 123 void fprint (QIODevice& dev, QString fmtstr, Args... args)
138 { 124 {
139 formatHelper (fmtstr, args...); 125 formatHelper (fmtstr, args...);
140 dev.write (fmtstr.toUtf8()); 126 dev.write (fmtstr.toUtf8());
141 } 127 }
142 128
143 // 129
144 // Exactly like print() except no-op in release builds. 130 // Exactly like print() except no-op in release builds.
145 //
146 template<typename... Args> 131 template<typename... Args>
147 #ifndef RELEASE 132 #ifndef RELEASE
148 void dprint (QString fmtstr, Args... args) 133 void dprint (QString fmtstr, Args... args)
149 { 134 {
150 formatHelper (fmtstr, args...); 135 formatHelper (fmtstr, args...);
151 PrintToLog (fmtstr); 136 printToLog (fmtstr);
152 } 137 }
153 #else 138 #else
154 void dprint (QString, Args...) {} 139 void dprint (QString, Args...) {}
155 #endif 140 #endif

mercurial