sources/format.h

changeset 69
eb4c25284a19
parent 66
bd28a5730fd0
child 70
0e947b487b18
equal deleted inserted replaced
68:202e74157de5 69:eb4c25284a19
32 #include "mystring.h" 32 #include "mystring.h"
33 #include "basics.h" 33 #include "basics.h"
34 #include "geometry.h" 34 #include "geometry.h"
35 35
36 #define FORMAT_OVERLOAD(...) \ 36 #define FORMAT_OVERLOAD(...) \
37 inline FUNCTION make_format_argument (__VA_ARGS__ a) -> String 37 inline String make_format_argument (__VA_ARGS__ a)
38 38
39 // ------------------------------------------------------------------------------------------------- 39 // -------------------------------------------------------------------------------------------------
40 // 40 //
41 FORMAT_OVERLOAD (String) { return a; } 41 FORMAT_OVERLOAD (String) { return a; }
42 FORMAT_OVERLOAD (char) { return String (a); } 42 FORMAT_OVERLOAD (char) { return String (a); }
95 return colorstrings[int (a)]; 95 return colorstrings[int (a)];
96 96
97 return "???"; 97 return "???";
98 } 98 }
99 99
100 FORMAT_OVERLOAD (Position) { return String ("(") + a.x + ", " + a.y + ")"; } 100 FORMAT_OVERLOAD (Position)
101 FORMAT_OVERLOAD (Size) { return String ("(") + a.width + "x" + a.height + ")"; } 101 {
102 return String ("(") + a.x + ", " + a.y + ")";
103 }
104
105 FORMAT_OVERLOAD (Size)
106 {
107 return String ("(") + a.width + "x" + a.height + ")";
108 }
102 109
103 FORMAT_OVERLOAD (Rectangle) 110 FORMAT_OVERLOAD (Rectangle)
104 { 111 {
105 String result; 112 String result;
106 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height); 113 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height);
115 122
116 // ------------------------------------------------------------------------------------------------- 123 // -------------------------------------------------------------------------------------------------
117 // 124 //
118 // Expands the given arguments into a vector of strings. 125 // Expands the given arguments into a vector of strings.
119 // 126 //
120 template<typename T, typename... RestTypes> FUNCTION 127 template<typename T, typename... RestTypes>
121 expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void 128 void expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest)
122 { 129 {
123 data.append (make_format_argument (arg)); 130 data.append (make_format_argument (arg));
124 expand_format_arguments (data, rest...); 131 expand_format_arguments (data, rest...);
125 } 132 }
126 133
150 // - x: The numeric argument will be represented in hexadecimal notation. This 157 // - x: The numeric argument will be represented in hexadecimal notation. This
151 // will work if the argument is a string representing a number. If the 158 // will work if the argument is a string representing a number. If the
152 // argument did not expand into a number in the first place, 0 is used 159 // argument did not expand into a number in the first place, 0 is used
153 // and 0x0 is printed. 160 // and 0x0 is printed.
154 // 161 //
155 template<typename... argtypes> FUNCTION 162 template<typename... argtypes>
156 format (const String& fmtstr, const argtypes&... raw_args) -> String 163 String format (const String& fmtstr, const argtypes&... raw_args)
157 { 164 {
158 Vector<String> args; 165 Vector<String> args;
159 expand_format_arguments (args, raw_args...); 166 expand_format_arguments (args, raw_args...);
160 return format_args (fmtstr, args); 167 return format_args (fmtstr, args);
161 } 168 }
164 // 171 //
165 // This is an overload of format() where no arguments are supplied. 172 // This is an overload of format() where no arguments are supplied.
166 // It returns the formatter string as-is. 173 // It returns the formatter string as-is.
167 // 174 //
168 static String format (const String& fmtstr) __attribute__ ((unused)); 175 static String format (const String& fmtstr) __attribute__ ((unused));
169 static String // FUNCTION 176 static String format (const String& fmtstr) // -> String
170 format (const String& fmtstr) // -> String
171 { 177 {
172 return fmtstr; 178 return fmtstr;
173 } 179 }
174 180
175 // ------------------------------------------------------------------------------------------------- 181 // -------------------------------------------------------------------------------------------------
176 // 182 //
177 // Prints the formatting result to the given file handle 183 // Prints the formatting result to the given file handle
178 // 184 //
179 template<typename... Args> FUNCTION 185 template<typename... Args>
180 print_to (FILE* fp, const String& fmtstr, Args const& ...args) -> void 186 void print_to (FILE* fp, const String& fmtstr, Args const& ...args)
181 { 187 {
182 std::fprintf (fp, "%s", format (fmtstr, args...).chars()); 188 std::fprintf (fp, "%s", format (fmtstr, args...).chars());
183 } 189 }
184 190
185 // ------------------------------------------------------------------------------------------------- 191 // -------------------------------------------------------------------------------------------------

mercurial