src/Format.h

changeset 115
9be16e1c1e44
parent 113
4d4c43eca4d7
equal deleted inserted replaced
114:6cbeb9f8350f 115:9be16e1c1e44
33 #include "Containers.h" 33 #include "Containers.h"
34 34
35 class FormatArgument 35 class FormatArgument
36 { 36 {
37 public: 37 public:
38 FormatArgument (const String& a) : mText (a) {} 38 FormatArgument (const String& a) : m_text (a) {}
39 FormatArgument (char a) : mText (a) {} 39 FormatArgument (char a) : m_text (a) {}
40 FormatArgument (int a) : mText (String::FromNumber (a)) {} 40 FormatArgument (int a) : m_text (String::fromNumber (a)) {}
41 FormatArgument (long a) : mText (String::FromNumber (a)) {} 41 FormatArgument (long a) : m_text (String::fromNumber (a)) {}
42 FormatArgument (const char* a) : mText (a) {} 42 FormatArgument (const char* a) : m_text (a) {}
43 43
44 FormatArgument (void* a) 44 FormatArgument (void* a)
45 { 45 {
46 mText.SPrintf ("%p", a); 46 m_text.sprintf ("%p", a);
47 } 47 }
48 48
49 FormatArgument (const void* a) 49 FormatArgument (const void* a)
50 { 50 {
51 mText.SPrintf ("%p", a); 51 m_text.sprintf ("%p", a);
52 } 52 }
53 53
54 template<class T> FormatArgument (const List<T>& list) 54 template<class T> FormatArgument (const List<T>& list)
55 { 55 {
56 if (list.IsEmpty()) 56 if (list.isEmpty())
57 { 57 {
58 mText = "{}"; 58 m_text = "{}";
59 return; 59 return;
60 } 60 }
61 61
62 mText = "{ "; 62 m_text = "{ ";
63 63
64 for (const T & a : list) 64 for (const T& a : list)
65 { 65 {
66 if (&a != &list[0]) 66 if (&a != &list[0])
67 mText += ", "; 67 m_text += ", ";
68 68
69 mText += FormatArgument (a).AsString(); 69 m_text += FormatArgument (a).text();
70 } 70 }
71 71
72 mText += " }"; 72 m_text += " }";
73 } 73 }
74 74
75 inline const String& AsString() const 75 inline const String& text() const
76 { 76 {
77 return mText; 77 return m_text;
78 } 78 }
79 79
80 private: 80 private:
81 String mText; 81 String m_text;
82 }; 82 };
83 83
84 #ifndef IN_IDE_PARSER 84 #ifndef IN_IDE_PARSER
85 # ifdef DEBUG 85 # ifdef DEBUG
86 # define devf(...) PrintTo (stderr, __VA_ARGS__) 86 # define devf(...) PrintTo (stderr, __VA_ARGS__)
103 * 103 *
104 * @param fmtstr Formatter string to process. 104 * @param fmtstr Formatter string to process.
105 * @param args Args to format with the string. 105 * @param args Args to format with the string.
106 * @see format() 106 * @see format()
107 */ 107 */
108 String FormatArgs (const String& fmtstr, const std::vector<String>& args); 108 String formatArgs (const String& fmtstr, const std::vector<String>& args);
109 109
110 /** 110 /**
111 * Expands the given arguments into a vector of strings. 111 * Expands the given arguments into a vector of strings.
112 * 112 *
113 * @param data Where to insert the strings. 113 * @param data Where to insert the strings.
114 * @param arg First argument to process 114 * @param arg First argument to process
115 * @param rest... Rest of the arguments. 115 * @param rest... Rest of the arguments.
116 */ 116 */
117 template<typename T, typename... RestTypes> 117 template<typename T, typename... RestTypes>
118 void ExpandFormatArguments (std::vector<String>& data, const T& arg, const RestTypes& ... rest) 118 void expandFormatArguments (std::vector<String>& data, const T& arg, const RestTypes& ... rest)
119 { 119 {
120 data.push_back (FormatArgument (arg).AsString()); 120 data.push_back (FormatArgument (arg).text());
121 ExpandFormatArguments (data, rest...); 121 expandFormatArguments (data, rest...);
122 } 122 }
123 123
124 /** 124 /**
125 * This is an overload of @c ExpandFormatArguments for end-of-args support. 125 * This is an overload of @c ExpandFormatArguments for end-of-args support.
126 */ 126 */
127 static void ExpandFormatArguments (std::vector<String>& data) __attribute__ ( (unused)); 127 static void expandFormatArguments (std::vector<String>& data) __attribute__ ( (unused));
128 static void ExpandFormatArguments (std::vector<String>& data) 128 static void expandFormatArguments (std::vector<String>& data)
129 { 129 {
130 (void) data; 130 (void) data;
131 } 131 }
132 132
133 /** 133 /**
159 * @return the formatted string. 159 * @return the formatted string.
160 * @see Print 160 * @see Print
161 * @see PrintTo 161 * @see PrintTo
162 */ 162 */
163 template<typename... argtypes> 163 template<typename... argtypes>
164 String Format (const String& fmtstr, const argtypes&... raw_args) 164 String format (const String& fmtstr, const argtypes&... raw_args)
165 { 165 {
166 std::vector<String> args; 166 std::vector<String> args;
167 ExpandFormatArguments (args, raw_args...); 167 expandFormatArguments (args, raw_args...);
168 assert (args.size() == sizeof... (raw_args)); 168 assert (args.size() == sizeof... (raw_args));
169 return FormatArgs (fmtstr, args); 169 return formatArgs (fmtstr, args);
170 } 170 }
171 171
172 /** 172 /**
173 * This is an overload of @c Format where no arguments are supplied. 173 * This is an overload of @c format where no arguments are supplied.
174 * @return the formatter string as-is. 174 * @return the formatter string as-is.
175 */ 175 */
176 static String Format (const String& fmtstr) __attribute__ ( (unused)); 176 static String format (const String& fmtstr) __attribute__ ( (unused));
177 static String Format (const String& fmtstr) 177 static String format (const String& fmtstr)
178 { 178 {
179 return fmtstr; 179 return fmtstr;
180 } 180 }
181 181
182 /** 182 /**
183 * Processes the given formatter string using @c Format and prints it to the 183 * Processes the given formatter string using @c format and prints it to the
184 * specified file pointer. 184 * specified file pointer.
185 * 185 *
186 * @param fp File pointer to print the formatted string to 186 * @param fp File pointer to print the formatted string to
187 * @param fmtstr Formatter string for @c Format 187 * @param fmtstr Formatter string for @c format
188 * @param args Arguments for @c fmtstr 188 * @param args Arguments for @c fmtstr
189 */ 189 */
190 template<typename... argtypes> 190 template<typename... argtypes>
191 void PrintTo (FILE* fp, const String& fmtstr, const argtypes&... args) 191 void printTo (FILE* fp, const String& fmtstr, const argtypes&... args)
192 { 192 {
193 fprintf (fp, "%s", Format (fmtstr, args...).c_str()); 193 fprintf (fp, "%s", format (fmtstr, args...).c_str());
194 } 194 }
195 195
196 /** 196 /**
197 * Processes the given formatter string using @c Format and prints the result to 197 * Processes the given formatter string using @c format and prints the result to
198 * @c stdout. 198 * @c stdout.
199 * 199 *
200 * @param fmtstr Formatter string for @c Format 200 * @param fmtstr Formatter string for @c format
201 * @param args Arguments for @c fmtstr 201 * @param args Arguments for @c fmtstr
202 */ 202 */
203 template<typename... argtypes> 203 template<typename... argtypes>
204 void Print (const String& fmtstr, const argtypes&... args) 204 void print (const String& fmtstr, const argtypes&... args)
205 { 205 {
206 PrintTo (stdout, fmtstr, args...); 206 printTo (stdout, fmtstr, args...);
207 } 207 }
208 208
209 /** 209 /**
210 * Throws an std::runtime_error with the processed formatted string. The program 210 * Throws an std::runtime_error with the processed formatted string. The program
211 * execution terminates after a call to this function as the exception is first 211 * execution terminates after a call to this function as the exception is first
214 * @param fmtstr The formatter string of the error. 214 * @param fmtstr The formatter string of the error.
215 * @param args The args to the formatter string. 215 * @param args The args to the formatter string.
216 * @see Format 216 * @see Format
217 */ 217 */
218 template<typename... argtypes> 218 template<typename... argtypes>
219 void Error (const String& fmtstr, const argtypes&... args) 219 void error (const String& fmtstr, const argtypes&... args)
220 { 220 {
221 Error (Format (fmtstr, args...)); 221 error (format (fmtstr, args...));
222 } 222 }
223 223
224 /** 224 /**
225 * An overload of @c Error with no string formatting in between. 225 * An overload of @c Error with no string formatting in between.
226 * 226 *
227 * @param msg The error message. 227 * @param msg The error message.
228 */ 228 */
229 void Error (String msg); 229 void error (String msg);
230 230
231 #endif // BOTC_FORMAT_H 231 #endif // BOTC_FORMAT_H

mercurial