37 public: |
37 public: |
38 FormatArgument (const String& a) : m_text (a) {} |
38 FormatArgument (const String& a) : m_text (a) {} |
39 FormatArgument (char a) : m_text (a) {} |
39 FormatArgument (char a) : m_text (a) {} |
40 FormatArgument (int a) : m_text (String::fromNumber (a)) {} |
40 FormatArgument (int a) : m_text (String::fromNumber (a)) {} |
41 FormatArgument (long a) : m_text (String::fromNumber (a)) {} |
41 FormatArgument (long a) : m_text (String::fromNumber (a)) {} |
|
42 FormatArgument (size_t a) : m_text (String::fromNumber ((long) a)) {} |
42 FormatArgument (const char* a) : m_text (a) {} |
43 FormatArgument (const char* a) : m_text (a) {} |
43 |
44 |
44 FormatArgument (void* a) |
45 FormatArgument (void* a) |
45 { |
46 { |
46 m_text.sprintf ("%p", a); |
47 m_text.sprintf ("%p", a); |
146 template<typename... argtypes> |
150 template<typename... argtypes> |
147 String format (const String& fmtstr, const argtypes&... raw_args) |
151 String format (const String& fmtstr, const argtypes&... raw_args) |
148 { |
152 { |
149 std::vector<String> args; |
153 std::vector<String> args; |
150 expandFormatArguments (args, raw_args...); |
154 expandFormatArguments (args, raw_args...); |
151 assert (args.size() == sizeof... (raw_args)); |
155 ASSERT_EQ (args.size(), sizeof... (raw_args)) |
152 return formatArgs (fmtstr, args); |
156 return formatArgs (fmtstr, args); |
153 } |
157 } |
154 |
158 |
155 // |
159 // |
156 // This is an overload of format() where no arguments are supplied. |
160 // This is an overload of format() where no arguments are supplied. |
203 // Throws an std::runtime_error with the processed formatted string. The program |
207 // Throws an std::runtime_error with the processed formatted string. The program |
204 // execution terminates after a call to this function as the exception is first |
208 // execution terminates after a call to this function as the exception is first |
205 // caught in main() which prints the error to stderr and then exits. |
209 // caught in main() which prints the error to stderr and then exits. |
206 // |
210 // |
207 template<typename... argtypes> |
211 template<typename... argtypes> |
208 void error (const String& fmtstr, const argtypes&... args) |
212 void error (const char* fmtstr, const argtypes&... args) |
209 { |
213 { |
210 error (format (fmtstr, args...)); |
214 error (format (String (fmtstr), args...)); |
211 } |
215 } |
212 |
216 |
213 // |
217 // |
214 // An overload of error() with no string formatting in between. |
218 // An overload of error() with no string formatting in between. |
215 // |
219 // |