console.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Kenneth Gangstoe
28 ** Mark Page
29 */
30 
31 
32 #pragma once
33 
34 // 'kbhit' was declared deprecated
35 #ifdef WIN32
36 #pragma warning(disable: 4996)
37 #endif
38 
39 #include "../api_core.h"
40 #include "string_format.h"
41 #include "string_help.h"
42 #ifdef WIN32
43 #include <conio.h>
44 #else
45 #include <unistd.h>
46 #endif
47 
48 namespace clan
49 {
52 
54 class Console
55 {
58 
59 public:
61  static void write(const std::string &text);
62 
63  template <class Arg1>
64 
69  static void write(const std::string &format, Arg1 arg1)
70  {
71  StringFormat f(format);
72  f.set_arg(1, arg1);
73  write(f.get_result());
74  }
75 
76  template <class Arg1, class Arg2>
77 
83  static void write(const std::string &format, Arg1 arg1, Arg2 arg2)
84  {
85  StringFormat f(format);
86  f.set_arg(1, arg1);
87  f.set_arg(2, arg2);
88  write(f.get_result());
89  }
90 
91  template <class Arg1, class Arg2, class Arg3>
92 
99  static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
100  {
101  StringFormat f(format);
102  f.set_arg(1, arg1);
103  f.set_arg(2, arg2);
104  f.set_arg(3, arg3);
105  write(f.get_result());
106  }
107 
108  template <class Arg1, class Arg2, class Arg3, class Arg4>
109 
117  static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
118  {
119  StringFormat f(format);
120  f.set_arg(1, arg1);
121  f.set_arg(2, arg2);
122  f.set_arg(3, arg3);
123  f.set_arg(4, arg4);
124  write(f.get_result());
125  }
126 
127  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
128 
137  static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
138  {
139  StringFormat f(format);
140  f.set_arg(1, arg1);
141  f.set_arg(2, arg2);
142  f.set_arg(3, arg3);
143  f.set_arg(4, arg4);
144  f.set_arg(arg5);
145  write(f.get_result());
146  }
147 
148  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
149 
159  static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
160  {
161  StringFormat f(format);
162  f.set_arg(1, arg1);
163  f.set_arg(2, arg2);
164  f.set_arg(3, arg3);
165  f.set_arg(4, arg4);
166  f.set_arg(arg5);
167  f.set_arg(arg6);
168  write(f.get_result());
169  }
170 
171  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
172 
183  static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
184  {
185  StringFormat f(format);
186  f.set_arg(1, arg1);
187  f.set_arg(2, arg2);
188  f.set_arg(3, arg3);
189  f.set_arg(4, arg4);
190  f.set_arg(arg5);
191  f.set_arg(arg6);
192  f.set_arg(arg7);
193  write(f.get_result());
194  }
195 
197  static void write_line(const std::string &text)
198  {
199  write(text);
200  #ifdef WIN32
201  write("\r\n");
202  #else
203  write("\n");
204  #endif
205  }
206 
207  template <class Arg1>
208 
213  static void write_line(const std::string &format, Arg1 arg1)
214  {
215  StringFormat f(format);
216  f.set_arg(1, arg1);
217  write_line(f.get_result());
218  }
219 
220  template <class Arg1, class Arg2>
221 
227  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2)
228  {
229  StringFormat f(format);
230  f.set_arg(1, arg1);
231  f.set_arg(2, arg2);
232  write_line(f.get_result());
233  }
234 
235  template <class Arg1, class Arg2, class Arg3>
236 
243  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
244  {
245  StringFormat f(format);
246  f.set_arg(1, arg1);
247  f.set_arg(2, arg2);
248  f.set_arg(3, arg3);
249  write_line(f.get_result());
250  }
251 
252  template <class Arg1, class Arg2, class Arg3, class Arg4>
253 
261  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
262  {
263  StringFormat f(format);
264  f.set_arg(1, arg1);
265  f.set_arg(2, arg2);
266  f.set_arg(3, arg3);
267  f.set_arg(4, arg4);
268  write_line(f.get_result());
269  }
270 
271  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
272 
281  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
282  {
283  StringFormat f(format);
284  f.set_arg(1, arg1);
285  f.set_arg(2, arg2);
286  f.set_arg(3, arg3);
287  f.set_arg(4, arg4);
288  f.set_arg(5, arg5);
289  write_line(f.get_result());
290  }
291 
292  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
293 
303  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
304  {
305  StringFormat f(format);
306  f.set_arg(1, arg1);
307  f.set_arg(2, arg2);
308  f.set_arg(3, arg3);
309  f.set_arg(4, arg4);
310  f.set_arg(5, arg5);
311  f.set_arg(6, arg6);
312  write_line(f.get_result());
313  }
314 
315  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
316 
327  static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
328  {
329  StringFormat f(format);
330  f.set_arg(1, arg1);
331  f.set_arg(2, arg2);
332  f.set_arg(3, arg3);
333  f.set_arg(4, arg4);
334  f.set_arg(5, arg5);
335  f.set_arg(6, arg6);
336  f.set_arg(7, arg7);
337  write_line(f.get_result());
338  }
339 
343  static void wait_for_key();
344 
346 };
347 
348 }
349 
static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
Write.
Definition: console.h:137
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2)
Write line.
Definition: console.h:227
static void write_line(const std::string &format, Arg1 arg1)
Write line.
Definition: console.h:213
static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
Write.
Definition: console.h:183
static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
Write.
Definition: console.h:117
void set_arg(int index, const std::string &text)
Set arg.
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
Write line.
Definition: console.h:281
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
Write line.
Definition: console.h:303
static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
Write.
Definition: console.h:99
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
Write line.
Definition: console.h:243
static void write_line(const std::string &text)
Writes text to the console window and then advances to a new line.
Definition: console.h:197
static void write(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
Write.
Definition: console.h:159
static void wait_for_key()
Block until a key is pressed in the console window.
static void write(const std::string &format, Arg1 arg1, Arg2 arg2)
Write.
Definition: console.h:83
Console access helper class.
Definition: console.h:54
static void write(const std::string &text)
Writes text to the console window.
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
Write line.
Definition: console.h:261
static void write(const std::string &format, Arg1 arg1)
Write.
Definition: console.h:69
static void write_line(const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
Write line.
Definition: console.h:327
String formatting class.
Definition: string_format.h:41
const std::string & get_result() const